L298 Dual H-Bridge Motor driver

l298

The Funduino Tracking Maze also comes with a L298 Dual H-Bridge Motor driver to control the two wheels of the robot.  It’s designed to drive inductive loads, such as relays, solenoids, DC and stepping motors. It can also let us drive two independent DC motors, controlling the speed and direction of each one .

The connections on the board are fairly simple to understand:

motorsconnections

Here we have several connectors for the motors (from left to right):

Motor A Power Motor B
VCC + GND VMS (vcc) + GND + 5v (external power) Vcc + GND
Note: To connect to arduino, we use VMS + GND. NOT 5v

channels

The PINS connections,  we have 8 (From left to right):

  • 5v
  • GND
  • ENB ( Motor B enable PIN)
  • IN4 (Motor B direction 2 PIN)
  • IN3 (Motor B direction 1 PIN)
  • IN2 (Motor A direction 2 PIN)
  • IN1 (Motor A direction 1 PIN)
  • ENA (Motor A enable PIN)

So, here’s a small resume of directions and how to control them:

Enable Motor HIGH – Enable LOW – Disable Motor
Direction 1 IN1 – HIGH IN2 – LOW
Direction 2 IN1 – LOW IN2 – HIGH
Coasting IN1 – LOW IN2 – LOW
Break IN1 – HIGH IN2 – HIGH

Bellow is a small code to test both DC motors. Here is the connections:

Arduino PIN Dual H-Bridge L298
10 ENA
9 ENB
2 IN1
3 IN2
4 IN3
5 IN4

connections1

And now, a little program with some functions defined for directions:

38 Replies to “L298 Dual H-Bridge Motor driver

  1. Thanks for the tutorial! Really clear. For the power supply to the Arduino, the motor driver has a built in 5V supply right? Do you know if you have to take off or leave on the jumper for the 5V mode while powering the driver with external source?

      1. Ok thanks. I got it work! So would this similar driver

        http://www.ebay.com/itm/2-PCS-Dual-H-Bridge-DC-Stepper-Motor-Drive-Controller-Board-Module-Arduino-L298N-/221565067299?pt=LH_DefaultDomain_0&hash=item33964ea023

        work pretty much the same way? I’m trying to figure this model out. The extra jumpers on ENA and ENB are confusing the hell out of me. I followed your tutorial for this model but it doesn’t work. No V is at motor 1 or 2. Have you worked with these models before?

        1. Hi ! I never worked with that model, but it seems similar. The ENA and ENB jumpers i really don’t get them, but you can try and remove the jumper, and try like this model and see what happens.
          ENA refers to motor A and ENB to motor B – just open the jumper and see if the correspondent motor works…

          Good luck !

    1. Hi Leo. I suppose you just have finished your project, A look here could help to clarify this point http://blog.pennybuying.com/down/f/F815A.pdf

      When the supply voltage exceeds 16V the power regulator located on the L298 could heat enough, then you need a separate 5V logic supply. In that case, you just need to take off the jumper and provide 5V externally.
      So, if you have to take of jumper or leave it will depend on your power supply voltage 🙂

  2. I tried exactly what you said and nothing happens…… hmm….. The ENA and ENB aren’t enabling the motors somehow.

  3. Wow, thanks so much for the link. I was looking everywhere for a document sheet for the model driver. But of course EBay never has anything 🙂

    So I had previously thought the jumpers on ENA and ENB just enabled the motors, without you having to worry about it. Because they just take the pins to 5V. And then the IN1, IN2, IN3, IN4 you use to control the direction. I shall try to hook it up again tonight to see where I when wrong last time.

    Thanks again.

  4. So I’m trying to make a 3 axis engraving machine from old electronics. If I only had to connect one stepper motor I would be fine. BUT, I need to connect three stepper motors.. I have three stepper motors, three “Keyes l298 drivers” and an arduino uno. I started this project 8 months ago. But I get stressed and walk away for awhile. I’m in and have been in the electronics stage… My arduino uno I have pins 2-13 to use.. But the Keyes l298 has en a-b. In 1-2-3-4. I have three l298 to connect to the aduino uno 3boards x 6wires each that is 18 wires, and I got 12 pins on the uno.. My question is how to I connect 3 Keyes l298’s to my arduino uno for thee axis’s. You help would be greatly appreciated.. Could you help me and email me at dzeima@hotmail.com.. This is driving me batty..

  5. Hi Dennis !

    That’s a problem you got there ! 🙂

    I will search if is possible, but probably you could just get :
    – Another Arduino with more input PINs
    – Another motor shield that can control more step motors

    I’ll check into that !

  6. Hi ,

    i have a problem with arduino code ,it gives me error in this line:
    “if (millis() > running)
    {”

    and error is explain in such message:
    “sketch_may27e.ino: In function ‘void loop()’:
    sketch_may27e:143: error: ‘gt’ was not declared in this scope
    sketch_may27e:143: error: expected )' before ';' token
    sketch_may27e:143: error: expected
    ;’ before ‘)’ token”

    thank you

  7. Hi Iñaki !

    Your second post is right. When I copied the code, the > sign got translated to it’s HTML code.

    My apologies.

    1. Hi Matija !

      Probably, but don’t know. I’ve seen the voltage and amps required by the step motor and everything checks out. The driver can handle it !

      This motor driver can handle step motors (bipolar such as that one) – but with that amount of torque and steps I don’t know – only one way to know and that’s testing it ! 🙂

      Check here at instructables, they have made an example using a step motor similar to that one:

      http://www.instructables.com/id/Control-DC-and-stepper-motors-with-L298N-Dual-Moto/?ALLSTEPS

      Hope it helps

    1. Hi Teo.

      Why would you want to do that? The l298 is to drive motors, not connecting LCDs… I don’t know, you’ll have to try it

      Cheers

  8. Hello feiticeir0
    Ok its pretty clear you know the stuff well. Here is my request. I have salvaged a printer head assembly from an old HP Deskjet 100.The motor is ok move the head on either directions but only when I reverse the polarity manually.Now I want to run this motor via L298 Module so that motor first start to run clockwise,when it reaches its end IT SLOWS DOWN and than reverse its direction anti-clockwise.The “end detectors” can be any photo-interrupters,but how to slow sown the speed before reaching the interrupters? I have all mechanical components ready I only need arduino sketch.Will you help me writing such a program?

    1. Hi Asif.
      Thank you for the comment, but I still have a lot to learn.
      You can probably archive that connecting L298 PINs that control the motor to PWM PINs in the Arduino. The PWM PINs allow a much refined control over the “speed” on which a motor will run.
      Bare in mind that there’s a value on which the motor will start to spin.. Lower values will just not work.

      Here’s a small example sketch were you can verify all this:
      [code]
      //Define Pins
      //Motor A
      int enableA = 9;
      int pinA1 = 10;
      int pinA2 = 11;
      int i;
      int time;

      void setup() {
      Serial.begin(9600);
      pinMode (enableA, OUTPUT);
      pinMode (pinA1, OUTPUT);
      pinMode (pinA2, OUTPUT);
      time = millis();
      }

      void loop () {
      analogWrite (enableA, HIGH);
      for (i = 130; i < = 255; i+=5) { digitalWrite (pinA1, i); digitalWrite (pinA2, LOW); Serial.print ("Speed: "); Serial.println (i); delay (2000); } } [/code] Every 2 seconds will change the speed - If you vary pinA1 and pinA2 with LOW or I, it will reverse direction.

      You can see this post and understand it better:
      https://www.bananarobotics.com/shop/How-to-use-the-L298N-Dual-H-Bridge-Motor-Driver

      Hope it helps.

      Best regards

      1. Hi Feiticeir0
        I have very little experience with arduino and only used picaxe.
        anyway I am using a Rev02 Keyes L298 driver its hooked up to an arduino uno and sensor shield for one of the ultrasonic sensor bots but i cant get motor b to run and I have zero volts at the terminals and on the underside of the board it appears the pcb tracks dont go anywhere. Can I get voltage from another place and still have control?
        Sorry for the armature approach. I couldnt walk the day I was born so no different that’s how we learn.
        Kind Regards,

        1. Hi Phil !
          Don’t be sorry nothing – We all start to learn from zero and if we don’t know, we must ask to know !
          It is just the MOTOR B that doesn’t work ? MOTOR A is spinning ?
          First, are you sure is not the motor that is bad ? Try to swap the wires and see if the motor that is running, still runs after connecting it to MOTOR B and vice-versa.

          Have you enabled it with ENB HIGH ? I know it is a stupid question, but it doesn’t hurt to ask.

          If all fails, I’ll need to check the driver and see if there’s other place to hook up some wires and drive the motor B – Otherwise, you may have a faulty board !

          Best regards

          1. Hi Feiticeiro,
            Wow that was fast. I can run the other motor strait off a battery, the A side runs well and also changes direction when the sensor is made. I can also wire the two motors together in the A side and they both run. This is the sketch I used.
            Thankyou for your help.

            #include
            int pinLB=6; // define pin6 as left back connect with IN1
            int pinLF=9; // define pin9 as left forward connect with IN2

            int pinRB=10; // define pin10 as right back connect with IN3
            int pinRF=11; // define pin11 as right back connect with IN4

            int inputPin = A0; // define ultrasonic receive pin (Echo)
            int outputPin =A1; // define ultrasonic send pin(Trig)

            int Fspeedd = 0; // forward distance
            int Rspeedd = 0; // right distance
            int Lspeedd = 0; // left distance
            int directionn = 0; //
            Servo myservo; // new myservo
            int delay_time = 250; // set stable time

            int Fgo = 8; // forward
            int Rgo = 6; // turn right
            int Lgo = 4; // turn left
            int Bgo = 2; // back

            void setup()
            {
            Serial.begin(9600);
            pinMode(pinLB,OUTPUT);
            pinMode(pinLF,OUTPUT);
            pinMode(pinRB,OUTPUT);
            pinMode(pinRF,OUTPUT);

            pinMode(inputPin, INPUT);
            pinMode(outputPin, OUTPUT);

            myservo.attach(5); // define the servo pin(PWM)
            }
            void advance(int a) // forward
            {
            digitalWrite(pinRB,LOW);
            digitalWrite(pinRF,HIGH);
            digitalWrite(pinLB,LOW);
            digitalWrite(pinLF,HIGH);
            delay(a * 100);
            }

            void turnR(int d) //turn right
            {
            digitalWrite(pinRB,LOW);
            digitalWrite(pinRF,HIGH);
            digitalWrite(pinLB,HIGH);
            digitalWrite(pinLF,LOW);
            delay(d * 100);
            }
            void turnL(int e) //turn left
            {
            digitalWrite(pinRB,HIGH);
            digitalWrite(pinRF,LOW);
            digitalWrite(pinLB,LOW);
            digitalWrite(pinLF,HIGH);
            delay(e * 100);
            }
            void stopp(int f) //stop
            {
            digitalWrite(pinRB,HIGH);
            digitalWrite(pinRF,HIGH);
            digitalWrite(pinLB,HIGH);
            digitalWrite(pinLF,HIGH);
            delay(f * 100);
            }
            void back(int g) //back
            {

            digitalWrite(pinRB,HIGH);
            digitalWrite(pinRF,LOW);
            digitalWrite(pinLB,HIGH);
            digitalWrite(pinLF,LOW);
            delay(g * 100);
            }

            void detection() //test the distance of different direction
            {
            int delay_time = 250; //
            ask_pin_F(); // read forward distance

            if(Fspeedd < 10) // if distance less then 10
            {
            stopp(1);
            back(2);
            }

            if(Fspeedd Rspeedd) //if left distance more than right distance
            {
            directionn = Rgo;
            }

            if(Lspeedd <= Rspeedd)//if left distance not more than right //distance
            {
            directionn = Lgo;
            }

            if (Lspeedd < 10 && Rspeedd < 10) //if left distance and right //distance both less than 10
            {
            directionn = Bgo;
            }
            }
            else
            {
            directionn = Fgo; // forward go
            }

            }
            void ask_pin_F() // test forward distance
            {
            myservo.write(90);
            digitalWrite(outputPin, LOW);
            delayMicroseconds(2);
            digitalWrite(outputPin, HIGH);
            delayMicroseconds(10);
            digitalWrite(outputPin, LOW);
            float Fdistance = pulseIn(inputPin, HIGH);
            Fdistance= Fdistance/5.8/10;
            Serial.print("F distance:");
            Serial.println(Fdistance);
            Fspeedd = Fdistance;
            }
            void ask_pin_L() // test left distance
            {
            myservo.write(5);
            delay(delay_time);
            digitalWrite(outputPin, LOW);
            delayMicroseconds(2);
            digitalWrite(outputPin, HIGH);
            delayMicroseconds(10);
            digitalWrite(outputPin, LOW);
            float Ldistance = pulseIn(inputPin, HIGH);
            Ldistance= Ldistance/5.8/10;
            Serial.print("L distance:");
            Serial.println(Ldistance);
            Lspeedd = Ldistance;
            }
            void ask_pin_R() // test right distance
            {
            myservo.write(177);
            delay(delay_time);
            digitalWrite(outputPin, LOW);
            delayMicroseconds(2);
            digitalWrite(outputPin, HIGH);
            delayMicroseconds(10);
            digitalWrite(outputPin, LOW);
            float Rdistance = pulseIn(inputPin, HIGH);
            Rdistance= Rdistance/5.8/10;
            Serial.print("R distance:");
            Serial.println(Rdistance);
            Rspeedd = Rdistance;
            }

            void loop()
            {
            myservo.write(90);
            detection();

            if(directionn == 2)
            {
            back(8);
            turnL(2);
            Serial.print(" Reverse ");
            }
            if(directionn == 6)
            {
            back(1);
            turnR(6);
            Serial.print(" Right ");
            }
            if(directionn == 4)
            {
            back(1);
            turnL(6);
            Serial.print(" Left ");
            }
            if(directionn == 8)
            {
            advance(1);
            Serial.print(" Advance ");
            Serial.print(" ");
            }
            }

        2. Just focus now on the motors – if you swap the wires (motor A connected on motor B terminals ) and with this same code, does the motor A still spins ? And if you connect the motor B on the motor A terminals, does it spin ? (we’re trying to figure out where the problem is – I’m guessing the motor driver – but, let’s swap those motors and check)

          1. I have swapped over the motor wires (motor A connected on motor B terminals ) same code and it doesn’t run And if I connect the motor B on the motor A terminals it does run.

        3. Hi Phil.

          Well, by your last comment, you may have a busted H-bridge. I if still in warranty, change it !

          1. I have found the problem. I had a dead short between the driver pcb and the sensor shield.
            It was a fluke but its going now.
            Thankyou so much for your help and knowledge it is much appreciated
            Kind Regards

        4. Hi Phil !

          I’m glad you sorted it out ! We’re always learning from each other !
          Best regards

    1. Hi Tkh !

      You need to program it for it to work.
      You can use a Raspberry PI if you want. Or, if you prefer, probably you can use an ATTINY, and that way, I’ll have a small footprint chip that is barely noticeable. but you have to always drive it with some coding.

      Best regards

    1. Hi Syed !
      Don’t know ! By your comment, I assumed the first one did… Are you sure is still COM3 ? Try disconnecting the USB cable and plug it in again (and check in device manager if the COM is still COM3). Does the code upload to Arduino without any problem ?

  9. Can you please help me out?Please show me the connections to the motor driver as I’m using a jack and 12V power adaptar,I’m not able to connect it…

  10. Why are you using “int” to mark pins? I use “#define”. It doesn’t take arduino memory .

    1. You are right. It’s better to use #define than to declare a int variable.. Since this program is small, I didn’t care, but you are right ! Thank you for pointing that out !
      Best regards

Leave a Reply to Michael Klishevich Cancel reply

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