[Arduino] L298 Dual H-Bridge Motor Driver

My first contact with robotics is probably the Lego Mindstorm kit that I have played with at a 2004 Youth IT Camp at Chinese University of Hong Kong and Hong Kong University of Science and Technology, which later, my mother bought my brother and I a set to play with back home. Now that I got into Arduino a bit, maybe it’s time to see if I can take up my robotic skills up a level. So I decided to build a basic robotic car, dual motor, to learn how to use the L298 Dual H-Bridge Motor Driver.

Since I got a C+ in Intro to Electrical Engineering, don’t ask me how the H-Bridge work. Maybe I will ask my Electrical Engineering friends to explain that some day. So basically, this is how to use it.

*If you are wondering this is where I ordered the L298 Dual H-Bridge and the Basic Car Kit. Price is good, roughly $11 USD, but shipping from China to US is just killing, lol.

Anyway, for my setup, I just simply taped the boards onto the base of the robotic car for now. Just for learning how to use the driver. So let take a closer look at the H-Bridge driver.

Your board might be different depends on where you get it from and the price. My is cheap, but it work as intended, so I don’t complain. The theory should apply to all anyway.

So, looking at the hardware hookup, we got 2 Motor connectors (the 2 blue exterior ones on the right side). On the board they are labeled MotorA and MotorB. On my board it didn’t tell you which one is the positive or negative terminal, so I just hook it up the same way for both. In the picture, red is the positive end and black is the negative end for the motors.

In the middle, there is a set of 3 terminals: Vms, GND, 5V. Now I think the 5V is just there to provide a 5V power source if you need it for something like an ultrasonic sensor. Vms, that is your motor’s external power source. When I was experimenting with it, you actually don’t need an external power source, the motor will still run. However, due to the limited current provided by the Arduino power source (discuss later), the motors are super weak. So for testing without a load on the motor (off the ground), you can run it without an external power source. For real testing, just slap one on. In the picture, I put a blue wire in. And GND, that’s just ground / negative terminal of the power source (black). Pretty much, I just take a battery holder (9V, 6 * 1.5V AA) and stick the wires in their respective end.

So that’s that, next is how we hook it up to the Arduino or other micro-controllers. By the way, that black shell thing in the middle is a heat sink I think. Then that other part it is holding is labeled L298N is the Full H Bridge. Whatever that means, not my field, lol.


First thing I notice is what’s with that pins sticking out? I don’t want to solder, I need a plugging in terminal! Oh well, whatever. So as you probably see in the first image, I basically stick a mini breadboard into it. Pretty ghetto but it work…

Ok, so we have 8 pins here. +5V and GND, I don’t think I need to explain that, just the power source from the Arduino. But you know what I discovered? When you plug in the external power source, this +5V pin actually can provide the energy to power the Arduino. Measured it with the super overpriced NI myDAQ that Lehigh force me to buy, it indicated 5V. Now I don’t know if it will damage the Arduino in the long run, but it works for me for the time being. It is great not having to carry 2 power source on the bot, one dedicated to the H-Bridge and one to the micro-controller.

Looking at the rest of the pins, we have: ENA, IN1, IN2, IN3, IN4, ENB.

We have 2 motors, 6 pins, best guess is that it take 3 to control each individually. So think of it this way:

Motor A: ENA, IN1, IN2
Motor B: ENB, IN3, IN4

1) Motor Enable Pin
2) Motor Direction 1 Pin
3) Motor Direction 2 Pin

Now the pins are digital pins here, so send HIGH and LOW signal to them from Arduino to control them. Think of HIGH is on and LOW as off. Here’s the logic table.

Motor Enable Pin:          HIGH = Enable Motor                     LOW = Disable Motor
Rotate Direction 1:         Motor Direction 1 Pin = HIGH        Motor Direction 2 Pin = LOW
Rotate Direction 2:         Motor Direction 1 Pin = LOW        Motor Direction 2 Pin = HIGH
Coasting:                         Motor Direction 1 Pin = LOW        Motor Direction 2 Pin = LOW
Brake:                               Motor Direction 1 Pin = HIGH        Motor Direction 2 Pin = HIGH

With that logic in mind, I wrote a sketch to do a timed movement sequence for the car. So here’s the final hook up for the car and the written codes.

*I didn’t want to cut that Arduino power adapter cable, so I just directly plug the power wire from H-Bridge into the actually battery in the battery holder.


// Yu Hin Hau
// Robotic Car via H-Bridge (L298)
// June 5, 2012

//See Low Level for Command Definitions

//Define Pins
int enableA = 2;
int pinA1 = 1;
int pinA2 = 0;

int enableB = 7;
int pinB1 = 6;
int pinB2 = 5;

//Define Run variable
boolean run;
void setup() {

 pinMode(enableA, OUTPUT);
 pinMode(pinA1, OUTPUT);
 pinMode(pinA2, OUTPUT);

 pinMode(enableB, OUTPUT);
 pinMode(pinB1, OUTPUT);
 pinMode(pinB2, OUTPUT);

 run = true;

}

//command sequence
void loop() {

 if(run)
 {

 delay(2000);

 enableMotors();

 forward(1000);
 coast(500);

 backward(1500);
 coast(500);

 forward(500);
 brake(500);

 turnLeft(500);
 turnRight(500);

 disableMotors();

 run = false;
 }

}

//Define Low Level H-Bridge Commands

//enable motors
void motorAOn()
{
 digitalWrite(enableA, HIGH);
}

void motorBOn()
{
 digitalWrite(enableB, HIGH);
}

 //disable motors
void motorAOff()
{
 digitalWrite(enableB, LOW);
}

void motorBOff()
{
 digitalWrite(enableA, LOW);
}

 //motor A controls
void motorAForward()
{
 digitalWrite(pinA1, HIGH);
 digitalWrite(pinA2, LOW);
}

void motorABackward()
{
 digitalWrite(pinA1, LOW);
 digitalWrite(pinA2, HIGH);
}

//motor B contorls
void motorBForward()
{
 digitalWrite(pinB1, HIGH);
 digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
 digitalWrite(pinB1, LOW);
 digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
 digitalWrite(pinA1, LOW);
 digitalWrite(pinA2, LOW);
}

void motorABrake()
{
 digitalWrite(pinA1, HIGH);
 digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
 digitalWrite(pinB1, LOW);
 digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
 digitalWrite(pinB1, HIGH);
 digitalWrite(pinB2, HIGH);
}

//Define High Level Commands

void enableMotors()
{
 motorAOn();
 motorBOn();
}

void disableMotors()
{
 motorAOff();
 motorBOff();
}

void forward(int time)
{
 motorAForward();
 motorBForward();
 delay(time);
}

void backward(int time)
{
 motorABackward();
 motorBBackward();
 delay(time);
}

void turnLeft(int time)
{
 motorABackward();
 motorBForward();
 delay(time);
}

void turnRight(int time)
{
 motorAForward();
 motorBBackward();
 delay(time);
}

void coast(int time)
{
 motorACoast();
 motorBCoast();
 delay(time);
}

void brake(int time)
{
 motorABrake();
 motorBBrake();
 delay(time);
}

21 thoughts on “[Arduino] L298 Dual H-Bridge Motor Driver

  1. Pingback: Ponte H L298N - RGeorgel

  2. Pingback: Ponte H L298N com Receptor IR - Arduino - RGeorgel

    • the delay function take in the value as milliseconds, I have it as forward 1000 millisecond, so 1 second forward. Change the arguments in the function and see what happen. If that doesn’t work, probably the wiring.

  3. Pingback: Le site de la famille DURY » Arduino & Rasberry PI

  4. Hello after this connection and uploading the programme. just we need to give power source to run it right? to which digital pins does the ENA and INA connected?

    • Any digital pins you want. ENA is to enable motor A, IN1 and IN2 is the direction command pins. Here’s how I defined them, digital pins.

      //Define Pins
      int enableA = 2;
      int pinA1 = 1;
      int pinA2 = 0;

      int enableB = 7;
      int pinB1 = 6;
      int pinB2 = 5;

  5. Pingback: [Tutorial #2] Line Follower Robot with Android and Arduino | Byte::Debugger();

  6. Thanks for your efforts. This page allowed me to figure out my L298 connections. I bought a couple of those $40 Chinese robots. Now I just need to figure them out. The motors was the part that was slowing me down. Thanks!

  7. Pingback: How to Build a 4WD Arduino Robot for Beginners - 0LF Translated & Grouped

  8. Pingback: How to Build a 4WD Arduino Robot for Beginners

  9. Pingback: real dragon pharma

  10. Pingback: ▷Wie man einen 4WD Arduino Roboter für Anfänger baut - virtualworldnews.de 【 2021 】

  11. Pingback: How to Build a 4WD Arduino Robot for Beginners – Päivittäin tuoretta tietoa

Leave a reply to Billwaa Cancel reply