Arduino car abnormal behavior

I made 2 wheel Arduino car, with L298N motor driver module. I used 4 AA 1.5V batteries to supply power to motors, supported with 5V from the Arduino UNO. I used 9V battery to supply power to the Arduino. Downloaded a sketch for Obstacle avoidance.

The problem is: when I connect the Arduino to laptop via USB the car works fine and everything is OK. but when I use it with battery something goes wrong , it behaves strange as a new sketch is running. the servo and ultrasonic sensor act different.

Could You please post your code?

If that is a PP3 battery then it will not last long

9V smoke alarm batteries can not supply much current. You need a better power supply than the 9V batteries, especially if you are using a servo. A 3.7V LiPo battery with a step up (to 5V) converter is what I use.

What else is connected to the Uno 5V rail? Post a schematic.

More details of "behaves strange" and "act different" please. What EXACTLY happens?

Steve

I used a premade sketch from a tutorial:

#define trig 3
#define echo 2
int thresholddistance = 30;

long duration;
float forwarddistance;
float distancefront;
float distanceArr[6];

#include <Servo.h>
#define pinServo 4

//----------------------------------------------------- Initialization: Setting up Servo Angles
int servoArray[6] = {0, 25, 50, 110, 145, 180};
Servo myServo;
//----------------------------------------------------- Initialization: Motors
int motor_left = {9, 6};
int motor_right = {11, 10};
//----------------------------------------------------- Initialization: Setup Module
void setup() {
Serial.begin(9600);
myServo.attach(pinServo);
//Setup Motors
for (int i = 0; i < 2; i++)
{
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
//Setup Sensor
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);

}
//----------------------------------------------------- Initialization: Motor Module
// Following Module is for the Robot to brake
void brake()
{
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
}
// Following Module is for the Robot to go forward
void drive_forward()
{
digitalWrite(motor_left[0], LOW);
delayMicroseconds(400);
digitalWrite(motor_left[1], HIGH);
delayMicroseconds(1000 - 400);
digitalWrite(motor_right[0], LOW);
delayMicroseconds(1000 - 400);
digitalWrite(motor_right[1], HIGH);
delayMicroseconds(400);
}
// Following Module is for the Robot to slightly go towards left
void slight_left()
{
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(600);
}
// Following Module is for the Robot to slightly go towards right
void slight_right()
{
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
delay(600);
}
// Following Module is for the Robot to turn left
void turn_left()
{
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(900);
}
// Following Module is for the Robot to turn right
void turn_right()
{
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
delay(900);
}
// Following Module is to make a uturn if there are obstacles to the right, to the left and in front
void turn_around()
{
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
delay(600);
}
//----------------------------------------------------- Sight Forward
float distance(int angle) {
myServo.write(angle);
delay(500);
digitalWrite(trig, LOW);
delayMicroseconds(20);
digitalWrite(trig, HIGH);
delayMicroseconds(20);
digitalWrite(trig, LOW);

duration = pulseIn(echo, HIGH);
forwarddistance = duration / 58;
return forwarddistance;

}
//----------------------------------------------------- Sensor Array Module
void scan_around() {
for (int i = 0; i < 6; i++) {
myServo.write(servoArray[i]);
delay(100);
digitalWrite(trig, LOW);
delayMicroseconds(20);
digitalWrite(trig, HIGH);
delayMicroseconds(20);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distanceArr[i] = duration / 58;
Serial.print("distanceArr[");
Serial.print(i);
Serial.print("] = ");
Serial.println(distanceArr[i]);
delay(100);
}
}
//----------------------------------------------------- Loop Module

void loop(){
myServo.write(75);
distancefront = distance(75);
Serial.println(distancefront);
// Serial.println(thresholddistance);
if ((distancefront > thresholddistance) && (distancefront < 1000))
{
drive_forward();
delay(1000);
Serial.println("1");
}
else {
brake();
scan_around();
if ((distanceArr[1] > 1000) && (distanceArr[2] > 1000) && (distanceArr[3] > 1000) && (distanceArr[4] > 1000) && (distanceArr[5] > 1000) && (distanceArr[6] > 1000))
{
return;
Serial.println("Restarted the Loop");
delay(100);
}
else if (((distanceArr[1]) > 30.0) && ((distanceArr[2]) > 30.0))
{
slight_right();
drive_forward();
delay(1000);
Serial.println("2");
}
else if (((distanceArr[0]) > 30.0) && ((distanceArr[1]) > 30.0))
{
turn_right();
drive_forward();
delay(1000);
Serial.println("3");
}
else if (((distanceArr[3]) > 30.0) && ((distanceArr[4]) > 30.0))
{
slight_left();
drive_forward();
delay(1000);
Serial.println("4");
}
else if (((distanceArr[4]) > 30.0) && ((distanceArr[5]) > 30.0))
{
turn_left();
drive_forward();
delay(1000);
Serial.println("5");
}
else
{
turn_around();
brake();
delay(1000);
slight_left();
drive_forward();
delay(1000);
Serial.println("6");
}
}
// delay(100);
}

I change the battery that supplies the Arduino board with a better quality one. Performance slightly improved.

I use these connections with a Servo attached to Pin 4. The only difference is I'm using an external 9v battery to supply power to Arduino, and use the 5V from the arduino to supply extra power to L298N.

Servo should rotate on both sides in 6 level movement when ultra sonic detects an object, it only goes left then back to front. Sometimes motor stop working, sometimes only one motor works (it's not because it's turning).

You're using 2 power supplies, 1 for the Arduino and 1 for the h-bridge. Which means you are just sending signal to the h-bridge with no shared ground?

There is a wire connecting both ground pins on Arduino and L298N.

Ok, cool.

I figured it out. The problem was I put the Arduino board on the battery holder. I think this made an Electromagnetic field, or something, and cause the board to produce wrong signals to motors. As I rearranged the components, everything goes fine.

Thank you all for your help.