Arduino Uno and L298N

Hello, I'm trying to build a car using an Arduino Uno, an L298N motor driver board, and an HC-05 Bluetooth module, but no matter what I do, I can't get the motors to turn. All the motors are working fine individually. I'm sharing the wiring diagram and code below.

// L298N Connection
const int motorA1  = 5;  // L298N's IN3 Input
const int motorA2  = 6;  // L298N's IN1 Input
const int motorB1  = 10; // L298N's IN2 Input
const int motorB2  = 9;  // L298N's IN4 Input
int i = 0; // A random variable assigned for loops
int j = 0; // A random variable assigned for loops
int state; // Variable for the signal coming from the Bluetooth device
int vSpeed = 255; // Standard speed, can be a value between 0-255

void setup() {
    // Define our pins
    pinMode(motorA1, OUTPUT);
    pinMode(motorA2, OUTPUT);
    pinMode(motorB1, OUTPUT);
    pinMode(motorB2, OUTPUT);    
    // Open a serial port at 9600 baud
    Serial.begin(9600);
}

void loop() {
  /* Stop the car when the Bluetooth connection is lost or disconnected.
     (To activate, remove the "//" from the following line.) */
//     if(digitalRead(BTState) == LOW) { state = 'S'; }

  // Save the incoming data to the 'state' variable
    if(Serial.available() > 0) {     
      state = Serial.read();   
    }

  /* 4 speed levels adjustable from the app (values should be between 0-255) */
    if (state == '0') {
      vSpeed = 0;
    } else if (state == '1') {
      vSpeed = 100;
    } else if (state == '2') {
      vSpeed = 180;
    } else if (state == '3') {
      vSpeed = 200;
    } else if (state == '4') {
      vSpeed = 255;
    }

  /***********************Forward****************************/
  // If incoming data is 'F', the car moves forward.
    if (state == 'F') {
      analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
      analogWrite(motorB1, vSpeed); analogWrite(motorB2, 0); 
    }
  /**********************Forward Left************************/
  // If incoming data is 'G', the car moves forward left (diagonal).
    else if (state == 'G') {
      analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);  
      analogWrite(motorB1, 100); analogWrite(motorB2, 0);
    }
  /**********************Forward Right************************/
  // If incoming data is 'I', the car moves forward right (diagonal).
    else if (state == 'I') {
      analogWrite(motorA1, 100); analogWrite(motorA2, 0); 
      analogWrite(motorB1, vSpeed); analogWrite(motorB2, 0); 
    }
  /***********************Backward****************************/
  // If incoming data is 'B', the car moves backward.
    if (state == 'B') {
      analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed); 
      analogWrite(motorB1, 0); analogWrite(motorB2, vSpeed); 
    }
  /**********************Backward Left************************/
  // If incoming data is 'H', the car moves backward left (diagonal).
    else if (state == 'H') {
      analogWrite(motorA1, 0); analogWrite(motorA2, 100); 
      analogWrite(motorB1, 0); analogWrite(motorB2, vSpeed); 
    }
  /**********************Backward Right************************/
  // If incoming data is 'J', the car moves backward right (diagonal).
    else if (state == 'J') {
      analogWrite(motorA1, 0); analogWrite(motorA2, vSpeed); 
      analogWrite(motorB1, 0); analogWrite(motorB2, 100); 
    }
  /***************************Left*****************************/
  // If incoming data is 'L', the car turns left.
    else if (state == 'L') {
      analogWrite(motorA1, vSpeed); analogWrite(motorA2, 150); 
      analogWrite(motorB1, 0); analogWrite(motorB2, 0); 
    }
  /***************************Right*****************************/
  // If incoming data is 'R', the car turns right.
    else if (state == 'R') {
      analogWrite(motorA1, 0); analogWrite(motorA2, 0); 
      analogWrite(motorB1, vSpeed); analogWrite(motorB2, 150);     
    }
  
  /************************Stop*****************************/
  // If incoming data is 'S', the car stops.
    else if (state == 'S') {
      analogWrite(motorA1, 0); analogWrite(motorA2, 0); 
      analogWrite(motorB1, 0); analogWrite(motorB2, 0);
    }  
}

Have you checked first that you can mainatin communication between HC-05 and Android Phone?

I see you are using hardware UART Port to interface HC-05. Try using Software UART Port and leave the UART port engaged with the PC for debugging and uploading sketch.

1 Like

Hi! Welcome to the Forum.

The first tip I can give you is that 9V batteries are not a good choice to work with this project. They can't provide enough current to the motors. So, change them for 6x AA batteries (which will give you the same 9V) or 2x 18650 (7.4 V).

A second point is related to the L298N driver operation. To make it work properly you have to either:

  1. place jumpers connecting the two pair of pins on the yellow circles:

in this case, motors will run at full speed all the time; OR

  1. in order to control speed, you need two PWM pins of Arduino connected to the first pin of each side.

take a look in This Tutorial

I’ll try it once I get 2x 18650 batteries. From what I understand, these batteries are rechargeable. If I use them, how long would the charge last?

If I use two 9V batteries, would it damage the Arduino or the L298N?

Is a single 18650 battery with 3000 mAh the correct battery, I wonder?

A combination of current and voltage is necessary. 2x 9V batteries are too much voltage not much current. One 18650 is too few voltage, since the L298N driver will drop out ~1,5V.

I still suggest 2x 18650 batteries or 6x AA alkaline batteries.

That depends on how much time you´ll keep the car running and the capacity of the battery group you choose.

I connected the motors this way and am using 6 AA batteries. After connecting from my phone, when I press forward, the motors on both sides work smoothly. When I press backward, they also work fine. However, when I press right, only the right-side motors should be working, but all motors are running. The same thing happens when I press left; all motors start running.

//L298N Bağlantısı   
  const int motorA1  = 5;  // L298N'in IN3 Girişi
  const int motorA2  = 6;  // L298N'in IN1 Girişi
  const int motorB1  = 10; // L298N'in IN2 Girişi
  const int motorB2  = 9;  // L298N'in IN4 Girişi
  int i=0; //Döngüler için atanan rastgele bir değişken
  int j=0; //Döngüler için atanan rastgele bir değişken
  int state; //Bluetooth cihazından gelecek sinyalin değişkeni
  int vSpeed=255;     // Standart Hız, 0-255 arası bir değer alabilir
void setup() {
    // Pinlerimizi belirleyelim
    pinMode(motorA1, OUTPUT);
    pinMode(motorA2, OUTPUT);
    pinMode(motorB1, OUTPUT);
    pinMode(motorB2, OUTPUT);    
    // 9600 baud hızında bir seri port açalım
    Serial.begin(9600);
}
 
void loop() {
  /*Bluetooth bağlantısı koptuğunda veya kesildiğinde arabayı durdur.
 (Aktif etmek için alt satırın "//" larını kaldırın.)*/
//     if(digitalRead(BTState)==LOW) { state='S'; }
  //Gelen veriyi 'state' değişkenine kaydet
    if(Serial.available() > 0){     
      state = Serial.read();   
    }
  
  /* Uygulamadan ayarlanabilen 4 hız seviyesi.(Değerler 0-255 arasında olmalı)*/
    if (state == '0'){
      vSpeed=0;}
    else if (state == '1'){
      vSpeed=100;}
    else if (state == '2'){
      vSpeed=180;}
    else if (state == '3'){
      vSpeed=200;}
    else if (state == '4'){
      vSpeed=255;}
     
  /***********************İleri****************************/
  //Gelen veri 'F' ise araba ileri gider.
    if (state == 'F') {
      analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
        analogWrite(motorB1, vSpeed);      analogWrite(motorB2, 0); 
    }
  /**********************İleri Sol************************/
  //Gelen veri 'G' ise araba ileri sol(çapraz) gider.
    else if (state == 'G') {
        analogWrite(motorA1,vSpeed ); analogWrite(motorA2, 0);  
        analogWrite(motorB1, 100);    analogWrite(motorB2, 0);
    }
  /**********************İleri Sağ************************/
  //Gelen veri 'I' ise araba ileri sağ(çapraz) gider.    
    else if (state == 'I') {
        analogWrite(motorA1, 100); analogWrite(motorA2, 0); 
        analogWrite(motorB1, vSpeed);      analogWrite(motorB2, 0); 
    }
  /***********************Geri****************************/
  //Gelen veri 'B' ise araba geri gider.
      if (state == 'B') {
      analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, vSpeed); 
    }
  /**********************Geri Sol************************/
  //Gelen veri 'H' ise araba geri sol(çapraz) gider
    else if (state == 'H') {
      analogWrite(motorA1, 0);   analogWrite(motorA2, 100); 
        analogWrite(motorB1, 0); analogWrite(motorB2, vSpeed); 
    }
  /**********************Geri Sağ************************/
  //Gelen veri 'J' ise araba geri sağ(çapraz) gider
    else if (state == 'J') {
      analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, 100); 
    }
  /***************************Sol*****************************/
  //Gelen veri 'L' ise araba sola gider.
    else if (state == 'L') {
      analogWrite(motorA1, vSpeed);   analogWrite(motorA2, 150); 
        analogWrite(motorB1, 0); analogWrite(motorB2, 0); 
    }
  /***************************Sağ*****************************/
  //Gelen veri 'R' ise araba sağa gider
    else if (state == 'R') {
      analogWrite(motorA1, 0);   analogWrite(motorA2, 0); 
        analogWrite(motorB1, vSpeed);   analogWrite(motorB2, 150);     
    }
  
  /************************Stop*****************************/
  //Gelen veri 'S' ise arabayı durdur.
    else if (state == 'S'){
        analogWrite(motorA1, 0);  analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);  analogWrite(motorB2, 0);
    }  
}

No... In 4 wheeled vehicles, all the wheels should be rolling, otherwise the vehicle will brake. What makes the car turn is one side being faster than the other (unless you have a servo to change direction of the front wheels).

Hours. I use an LG HG2 (20A, 3,000mAh 18650). My car draws less than 1A.

I fixed all the connections. Now it’s working smoothly. I was using 6 AA batteries, but even though they hadn’t been used before, they drained very quickly. The 18650 battery is very expensive, and I would also need to buy a charger for it. This would be very costly for me.

I'm happy to hear that. If you consider your problem is solved, please mark the topic as well.

We use to say here in Brazil that "what has no solution, solved it is"... :grin:

// L298N Connections     
const int motorA1 = 6;  // L298N IN1 Input
const int motorA2 = 10; // L298N IN2 Input
const int motorB1 = 5;  // L298N IN3 Input
const int motorB2 = 9;  // L298N IN4 Input
int i = 0; // A random variable assigned for loops
int j = 0; // A random variable assigned for loops
int state; // Variable for the signal coming from the Bluetooth device
int vSpeed = 255;     // Default Speed, can be a value between 0-255

void setup() {
    // Define the pins
    pinMode(motorA1, OUTPUT);
    pinMode(motorA2, OUTPUT);
    pinMode(motorB1, OUTPUT);
    pinMode(motorB2, OUTPUT);    
    // Open a serial port at 9600 baud
    Serial.begin(9600);
}

void loop() {
    /* Stop the car if the Bluetooth connection is lost or disconnected.
    (Remove "//" from the line below to activate) */
    // if(digitalRead(BTState) == LOW) { state = 'S'; }

    // Save the incoming data into the 'state' variable
    if (Serial.available() > 0) {     
        state = Serial.read();   
    }
  
    /* Four speed levels adjustable from the application. (Values must be between 0-255) */
    if (state == '0') {
        vSpeed = 0;
    } else if (state == '1') {
        vSpeed = 100;
    } else if (state == '2') {
        vSpeed = 180;
    } else if (state == '3') {
        vSpeed = 200;
    } else if (state == '4') {
        vSpeed = 255;
    }
     
    /***********************Forward****************************/
    // If the incoming data is 'F', the car moves forward.
    if (state == 'F') {
        analogWrite(motorA1, 250);   analogWrite(motorA2, 0); 
        analogWrite(motorB1, 250); analogWrite(motorB2, 0); 
    }
    /**********************Forward Left************************/
    // If the incoming data is 'G', the car moves diagonally forward left.
    else if (state == 'G') {
        analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);  
        analogWrite(motorB1, 100);    analogWrite(motorB2, 0);
    }
    /**********************Forward Right************************/
    // If the incoming data is 'I', the car moves diagonally forward right.    
    else if (state == 'I') {
        analogWrite(motorA1, 100); analogWrite(motorA2, 0); 
        analogWrite(motorB1, vSpeed);      analogWrite(motorB2, 0); 
    }
    /***********************Backward****************************/
    // If the incoming data is 'B', the car moves backward.
    if (state == 'B') {
        analogWrite(motorA1, 0);   analogWrite(motorA2, 250); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, 250); 
    }
    /**********************Backward Left************************/
    // If the incoming data is 'H', the car moves diagonally backward left.
    else if (state == 'H') {
        analogWrite(motorA1, 0);   analogWrite(motorA2, 100); 
        analogWrite(motorB1, 0); analogWrite(motorB2, vSpeed); 
    }
    /**********************Backward Right************************/
    // If the incoming data is 'J', the car moves diagonally backward right.
    else if (state == 'J') {
        analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, 100); 
    }
    /***************************Left*****************************/
    // If the incoming data is 'L', the car moves left.
    else if (state == 'L') {
        analogWrite(motorA1, 50);   analogWrite(motorA2, 0); 
        analogWrite(motorB1, 250); analogWrite(motorB2, 0); 
    }
    /***************************Right*****************************/
    // If the incoming data is 'R', the car moves right.
    else if (state == 'R') {
        analogWrite(motorA1, 250);   analogWrite(motorA2, 0); 
        analogWrite(motorB1, 50);   analogWrite(motorB2, 0);     
    }
  
    /************************Stop*****************************/
    // If the incoming data is 'S', stop the car.
    else if (state == 'S') {
        analogWrite(motorA1, 0);  analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);  analogWrite(motorB2, 0);
    }  
}

if it doesn't work the battery is insufficient

How many mAh should two 18650 batteries have? Do you have any recommendations?

There are different models with different capacities.

Use two of the same kind and buy them from reputable sellers

I don't want it to work for a short time. Will it last for a few hours during testing?

I’m considering adding an extra robotic arm to this vehicle, which will make it even heavier. Will two 18650 3.7V 3000mAh batteries be sufficient? Do you know of a more powerful battery model?

If you get a 3000 mAh battery you'll have 3A available for an hour. If your car consumes, let's say, 1,5A, it will work continuously for 2 hours.

The higher the capacity of mAh of the chosen battery is, the more time you'll have.

There are more powerful LiPo batteries, that are commonly used with drones and RC cars. However, they need special chargers too.

How can I find the right charger? Do you know a specific model?

That depends on the battery that you'll choose. Have you already decided which battery will you get?