Gear Motor not going backward

Hi Everyone,

I am making a simple 2w Obstacle Avoiding Robot using Arduino Uno, before assembling the Robot (car), i made a small code testing the motors and the motor driver (I used the l298n driver) and every thing worked smoothly, after assembling the robot and writing the code, i saw that the right motor isn't going backwards, I revised the code many times but didn't find anything to cause the motor not going backwards, I tried to invert the connections of the ports of the motor then it couldn't go forward. I searched in google but did't find anything that helped me in my problem, I don't know if you had or saw the same problem before but I hope you help me.

Here is my main code:

//LEFT MOTOR IS RIGHT MOTOR AND RIGHT MOTOR IS LEFT  MOTOR
#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 200
int distance = 100;

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


void setup(){

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

  servo_motor.write(90);
  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(0);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(90);
  return distance;
}

int lookLeft(){
  servo_motor.write(180);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(90);
  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(){
   
    digitalWrite(LeftMotorForward, LOW);
    digitalWrite(RightMotorForward, LOW);
  
    digitalWrite(LeftMotorBackward, HIGH);
    digitalWrite(RightMotorBackward, HIGH); 

}

void moveBackward(){


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

void turnRight(){

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

void turnLeft(){

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

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

Links For Circuit Components:
*Arduino Uno: Amazon.com

*L298N Motor: Driverhttps://www.amazon.com/Qunqi-Controller-Module-Stepper-Arduino/dp/B014KMHSW6

*Motors: Amazon.com

*Servo Motors: Amazon.com

*Ultrasonic Sensor: Amazon.com

Other Components:
*Ultrasonic Holder
*Universal Wheel (Caster Wheel)

  • Alkaline AA Batteries

Welcome to the group.


When you try the sketch examples in the library do the motors operate properly ?


Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

Ok, I will post them now

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

Serial.print(“cm = “);
Serial.println(cm);

return cm;
}

BTW
When you add some serial prints, do things work as you expect with sonar.ping ?

Yes Every thing with sonar.ping works smoothly

What is the voltage measured at the 5v pin on the UNO ?

Actually, it appears you are powering the UNO via the Vin pin, good, make sure the voltage is > 7v.

Then you are powering the sonar servo with the 5v pin on the UNO, this is bad, do not do this.


EDIT:

Do you have the 293D ‘enable’ pins 1 and 9 connected ?

12 and 13 should be GND

Where did you get that drawing from ?

Yes it is higher than 7v

I know it is not good, but in the robot it self I am powering them from different vcc pins one from the 5v pin and one from the icsp pins on the Arduino board but unfortunately they don't appear in my sketch.

But they really work good, my main question is that why the right motor can't drive (go) backwards?.

larryd:
Actually, it appears you are powering the UNO via the Vin pin, good, make sure the voltage is > 7v.

Then you are powering the sonar servo with the 5v pin on the UNO, this is bad, do not do this.


EDIT:

Do you have the 293D ‘enable’ pins 1 and 9 connected ?

12 and 13 should be GND

No i didn't connect the 'enable' pin because I think I don't need them and I dind't understand what do you mean by "12 and 13 should be GND"
I used Tinkercad(for the sketch) https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwi00oDiyMjpAhVNOhoKHYcPAtcQFjAAegQIDhAC&url=https%3A%2F%2Fwww.tinkercad.com%2F&usg=AOvVaw1JxBDMj3J4tH5_HVKfRPhY

Post an image of schematic ‘here’, that web link you gave, ‘does not’ show it.

You have Arduino 5v going to pin 8 of the 293 when pin 8 in the data sheet says:

“ Power VCC for drivers 4.5 V to 36 V”

Pin 16 on the 293 is:

“5-V supply for internal logic translation”.


pin 12 an 13 go to 0volts (GND).

To enable the two 293 drivers, pin 1 and 9 are to be HIGH, you can control these in software as needed.

I really didn't understand what you said

Attach a good image of the ‘original’ schematic this project is based on.

Pins 12 and 13 are to be connected to zero volts, similar to pins 4 and 5.

Pins 1 and 9 should be connected to Arduino 5v, this will properly enable the driver logic.

Pin 8 connects to the motor supply, ‘do not’ connect it to Arduino 5v.

Pin 16 connects to Arduino 5v.

What is the voltage going to the Arduino Vin pin ?


If you are having trouble understanding English (might not be your first language) try a different forum for help.

Thank you for your response, I figured out the problem, the block of the l298n was damaged (fried) because due to heating.

Because you wired pin 16 and 8 incorrectly ;).