Motor driver doesn't work after i plug in in2 and in4

The motor a doesn't work when in4 is plugged in, and motor b doesn't work when in2 is plugged in. The code honestly worked 1 month ago, but when I connected it again, it didn't work. Is this problem normal or not? how do i fix this?

This is from this video how to make arduino obstacle avoiding car using L298N motor driver - YouTube

#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 = 8;
const int LeftMotorBackward = 9;
const int RightMotorForward = 10;
const int RightMotorBackward = 11;
 
//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A0 //analog input 2
 
#define maximum_distance 100
boolean goesForward = false;
int distance = 50;
 
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(7); //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);

  Serial.begin(2000000); // Starts the serial communication
  
}
 
void loop(){
 
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);
 
  if (distance < 30){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

Serial.print("Distance: ");
Serial.println(distance);
 
    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}
 
int lookRight(){  
  servo_motor.write(100);
  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){
 
    goesForward=true;
     
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
   
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}
 
void moveBackward(){
 
  goesForward=false;
 
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
   
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
   
}
 
void 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(){
 
  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);
}
  1. Ask your question in the right forum section.
  2. Give a link to the data sheet of your motor driver board.
  3. Show a circuit diagram of your project.
2 Likes

Have any of the libraries changed? What Arduino are you using and did use? If you get more parts get a replacement for the L298N it is burning a large percentage of your battery power as heat. Get one that uses MOSFET outputs.

Pololu DC motor drivers.

Following the video's web site to here;

You will see your wiring is different.

You can also see your sketch is different.

Your code is corrupted. For example, this does not make your project work:

  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);

  Serial.begin(2000000); // Starts the serial communication

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