L298N motor driver with Arduino

Hi, In this article, you will learn, how you can control DC motors with Arduino using an L298N motor driver module

L298N motor driver module.

The L298N H-bridge motor driver module is used to control two DC motors or a single bipolor stepper motor. By using this module you can control the direction and the speed of DC motors. This module support from 5 to 35 volt DC.

In this module also have an onboard 5 volt regulator. so if you are using a 6 volt to 12 volt power supply then you have also have 5 volt output from the board.

Control DC motors with Arduino

When you want to drive a DC motor with Arduino, you need some sort of motor driver module that can run the motor and control the direction and speed of the motor. The L298N driver module is compact and best for this. In this post, I have attached step by step demonstration video tutorial to control the speed and direction of a DC motor with the motor driver module, manually as well as with Arduino.

Arduino code for L298N motor driver module.

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup()
{
  // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}
void demoOne()
{
  // this function will run the motors in both directions at a fixed speed
  // turn on motor A
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enA, 200);
  // turn on motor B
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  // set speed to 200 out of possible range 0~255
  analogWrite(enB, 200);
  delay(2000);
  // now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH); 
  delay(2000);
  // now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}
void demoTwo()
{
  // this function will run the motors across the range of possible speeds
  // note that maximum speed is determined by the motor itself and the operating voltage
  // the PWM values sent by analogWrite() are fractions of the maximum speed possible 
  // by your hardware
  // turn on motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH); 
  // accelerate from zero to maximum speed

 for (int i = 0; i < 256; i++) { 
analogWrite(enA, i); 
analogWrite(enB, i); 
delay(20); 
} 
// decelerate from maximum speed to zero 
for (int i = 255; i > 0; --i)
  {
    analogWrite(enA, i);
    analogWrite(enB, i);
    delay(20);
  } 

 
  // now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);  
}
void loop()
{
  demoOne();
  delay(1000);
  demoTwo();
  delay(1000);
}

Bonus links – For the Arduino and Motor drivers

DC Motor Control with NodeMCU and Driver shield

Here are some other articles link which are related to DC Motor Driver and Arduino.

Hope this article is useful for you. If you have any question or doubt, please comment and let us know. Thanks

12 Comments
  1. CrystalRunner 6 years ago

    Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: “Arduino/Genuino Uno”

    C:\Users\User\Desktop\SKETCHES\DualHBridgeDCMotor_Sketch\DualHBridgeDCMotor_Sketch.ino: In function ‘void demoTwo()’:

    DualHBridgeDCMotor_Sketch:61: error: ‘i’ was not declared in this scope

    analogWrite(enA, i);

    ^

    exit status 1
    ‘i’ was not declared in this scope

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

    • Author
      admin 6 years ago

      Hi, Thanks for informing me. I have update the code, Please copy and past the code again it will work now. And Please let me know if you are still facing any problem.

      • subham mohanty 6 years ago

        yep its showing errors in code
        how can i correct it..//

        • Abhishek Gupta 4 years ago

          remove the spaces behind the scope or in other words remove the indentations. to verify the keywords should be colored, check that

  2. Matey Ankit 6 years ago

    I want to run 24v dc motor having 10kg torque, 30 rpm with the help of aurdino uno
    The motor must run only in one direction continuously
    Plz suggests me what to use and how to do programing

  3. md.intishar ul islam 6 years ago

    how to contro??
    by joystic???

  4. Shashi Kiran 5 years ago

    My DC motor spins one direction only when connected thru the L298. I have adapted the code above. I dont think the problem is with the code or the motor as I tested the motor by connecting + and – to 12V directly and changed polarities and it spins both directions. But when I connect thru the L298 then only 1 direction works.
    How can I debug the L298. At this time I have only 1 DC geared 100 rpm motor. Have not connected another motor.
    The Power is thru a 12V DC wall adapter. The controls to the L298 are thru the Arduino Pins 6 and 7. Pin 9 is set to analogWrite of PWM 255. ( of course the jumper on EN-A Enable A is removed and controlled thru the Arduino.

    • Vinod 5 years ago

      I also have the same problem if you get the solution please let me know

  5. Emon 5 years ago

    How can I do the same thing with Arduino & L293D motor shield ?

  6. Vinod 5 years ago

    I also have the same problem if you get the solution please let me know

  7. Harshala 4 years ago

    hi
    I am not getting any error but the motor is not running please help

  8. Abhishek Gupta 4 years ago

    that’s some awesome code, nice variations. Loved it !!!

Leave a reply

Your email address will not be published. Required fields are marked *

*

or

Log in with your credentials

or    

Forgot your details?

or

Create Account