Obstacle Avoidance Robot Not Working

This is my first attempt at robotics and so far my robot has had many difficulties. The current issue is the robot not moving at all and the motor driver letting out a small beep. I used a Arduino UNO, L298N motor driver, 2 DC motor wheels, a servo motor, and UDS sensor. It is powered by 4 AA batteries as well.


#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2

#define maximum_distance 250
boolean goesForward = false;
int distance = 150;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){

  Serial.begin(9600);

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  
  servo_motor.attach(10); //our servo pin

  servo_motor.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){

  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
  delay(100);
}

int readPing(){
  delay(70);
  int cm = sonar.ping_cm();
  if (cm==0){
    cm=250;
  }
  return cm;
}

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    Serial.println("==moveForward==");

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  Serial.println("==moveBackward==");

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
}

void turnRight(){

  Serial.println("==turnRight==");

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  
  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
 
  
  
}

void turnLeft(){

  Serial.println("==turnLeft==");

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

It could be your motor driver. It is a very old design and does not work well with lower voltages. TB6612 is an often suggested modern replacement.

Have you tested the motors by connecting them directly to the battery pack ? (Motor driver and Uno not connected)

Have you tried testing the motor driver by connecting the motors and battery pack, but connecting the control wires directly to 5V or ground ? (Uno not connected)

What type of AA cells are you using?

Do you know what the minimum voltage for the Uno's Vin pin is?

Do you have the enable pins connected to 5V on the motor driver board?

I tested the motors by connecting it directly and they worked like they were supposed to.
I tested the motor driver and the wheel did not turn or make any movement, is this what’s causing the no movement?
I ma using alkaline AA batteries.
The minimum voltage is 7V and i do not have a enable pin connected to the 5V on the arduino.

Are you connecting the 6V battery pack to the 12V terminal?

You seem to be powering the servo from the Arduino 5V pin. That usually does not work

As @PaulRB mentioned, the L298N might be too-big a load, possibly drawing the 6VDC (with four fresh batteries) down to 4VDC... which MIGHT run ONE motor (I think the motors might run with 3.5v = 6v)

In your code, comment-out all "RightMotor" pins/calls/functions/objects and disconnect your right motor from the L298N, leaving your LEFT motor getting all the remaining power. If you have a multimeter, read the output of the L298N as the LEFT motor is getting commands to move.

If your Left motor works, you could try adding two more AA batteries to your power supply and re-attach the RightMotor pins/calls/functions/objects/wires.

What is your battery pack supplying?

You seem to have one of the enable pins connected to an Arduino pin, but the other enable pin isn't connected to anything. Also, only IN1, 2 & 3 are connected. IN4 is not connected. Have you seen any tutorials for l298 where the module is connected like that?

Screenshot_20240321-190922-864

Hi, @Chanelw
Welcome to the forum.

Can you please post some pictures of your project?
So we can see your components and the layout.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

The left motor did not end up moving, could this be a problem with my motor driver?

I switched my battery pack to a 9V battery.
I’ve seen two tutorials that wired the robot i did and they seemed to work, is this not the standard?

I don’t know if these will be of any use since it’s very messy, i didn’t want to make anything permanent in case it needed changing.





This battery has 9vdc, but does not have the capacity needed to make three motors, a speaker and a microphone (ultrasonic transceiver) function all at one time. Use the AA batteries, but in a pack of six (6AA x 1.5v = 9vdc).

The wire connections need to be better. If you must twist wires together, make many twists around, not just one twist. You should solder and insulate all these connections.

You should correct what is mentioned in Post #6 and Post #4.

This link is to a simulation of using an L298N-type motor driver by using six LEDs to show Enable, Forward and Backward for two motors. It starts out by enabling both motors, then enabling one motor and driving only one motor forward, then driving that one motor backward... and repeat for the second motor... then the simulation/sketch enables and drives two motors. The simulation explains what is happening as it progresses.

You can use the code in your Arduino if you make the pin number changes in the simulation code.
YouTube video makers do not care if your attempt works or not. They copy/pasted from another bad video and only want mouse-clicks. For every video worth something, 100,000 are worth nothing.

Not a PP3 rectangular battery, I hope? Useless for driving motors. Hopefully you mean 6xAA.

Check them again and compare to your wiring, in detail.

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.