Arduino Modules - L298N Dual H-Bridge Motor Controller

788,872

320

112

About: Here I am!

Quick and simple start guide for using and exploring an L298N Dual H-Bridge Motor Controller module with an Arduino.

The model in the example I am using is from Ebay.

Materials needed:

  • L298N Dual H-Bridge Motor Controller module (various models will work)
  • Male to Female jumper wires
  • An Arduino, any flavor.
  • A DC power supply, 7-35v
  • A motor that is the correct voltage for your power supply used.

Step 1: Getting to Know Your L298N Dual H-Bridge Motor Controller Module:

Usage:

H-Bridge's are typically used in controlling motors speed and direction, but can be used for other projects such as driving the brightness of certain lighting projects such as high powered LED arrays.

How it works:

An H-Bridge is a circuit that can drive a current in either polarity and be controlled by *Pulse Width Modulation (PWM).

* Pulse Width Modulation is a means in controlling the duration of an electronic pulse. In motors try to imagine the brush as a water wheel and electrons as a the flowing droplets of water. The voltage would be the water flowing over the wheel at a constant rate, the more water flowing the higher the voltage. Motors are rated at certain voltages and can be damaged if the voltage is applied to heavily or if it is dropped quickly to slow the motor down. Thus PWM. Take the water wheel analogy and think of the water hitting it in pulses but at a constant flow. The longer the pulses the faster the wheel will turn, the shorter the pulses, the slower the water wheel will turn. Motors will last much longer and be more reliable if controlled through PWM.

Pins:

  • Out 1: Motor A lead out
  • Out 2: Motor A lead out
  • Out 3: Motor B lead out
  • Out 4: Mo (Can actually be from 5v-35v, just marked as 12v)
  • GND: Ground
  • 5v: 5v input (unnecessary if your power source is 7v-35v, if the power source is 7v-35v then it can act as a 5v out)
  • EnA: Enables PWM signal for Motor A (Please see the "Arduino Sketch Considerations" section)
  • In1: Enable Motor A
  • In2: Enable Motor A
  • In3: Enable Motor B
  • In4: Enable Motor B
  • EnB: Enables PWM signal for Motor B (Please see the "Arduino Sketch Considerations" section)

Specifications:

  • Double H bridge Drive Chip: L298N
  • Logical voltage: 5V Drive voltage: 5V-35V
  • Logical current: 0-36mA Drive current: 2A (MAX single bridge)
  • Max power: 25W
  • Dimensions: 43 x 43 x 26mm
  • Weight: 26g

*Built-in 5v power supply, when the driving voltage is 7v-35v

Step 2: Wiring to an Arduino:

There are several different models of these L298N Dual H-Bridge Motor Controllers. The generic wiring schematic above should do the trick for most.

Two things to mention;

  • Make sure you have all of your grounds tied together; Arduino, Power source, and the Motor controller.
  • The PWM Pins are unnecessary if you do not want to control PWM features.

Step 3: Arduino Sketch Considerations:

The Arduino code sketch is pretty straight forward. Since there isn’t a library for the L298N Dual H-Bridge Motor Controller you just have to declare which pins the controller is hooked to.

The “int dir(number)Pin(letter)”‘ pins can be connected to any available digital pin you have available, as long as you declare the correct pin in your sketch. This makes the L298N Dual H-Bridge Motor Controller very versatile if your project is using a lot of Arduino pins.

The int“speedPin(letter)” pins need to be connected to a PWM pin on the Arduino if you want to enable speed control through PWM.

As a quick cheat I have included a list of PWM pins for the main two types of Arduino's I use:

  • AT MEGA – PWM: 2 to 13 and 44 to 46. Provide 8-bit PWM output with the analogWrite() function.
  • UNO – PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.

Step 4: Arduino Sketch Example:

This code example I wrote to allow a serial monitor program such as Putty to control the L298N Dual H-Bridge Motor Controller via a keyboard with key presses.

Controls:

Key .......... Motor

1 ............... Motor 1 Forward

2 ............... Motor 1 Stop

3 ............... Motor 1 Reverse

4 ............... Motor 2 Forward

5 ............... Motor 2 Stop

6 ............... Motor 2 Reverse

Code:

  • Fixed the formatting issues (12/29/2014):

Download the attached L298N_Dual_H_Bridge_Keyboard_Control_Reichenstein7.ino

Alternate Download: http://www.mediafire.com/download/z654e8at1b21d7e/L298N_Dual_H_Bridge_Keyboard_Control_Reichenstein7.ino

2 People Made This Project!

Recommendations

  • First Time Author

    First Time Author
  • Toys Contest

    Toys Contest
  • Big and Small Contest

    Big and Small Contest

112 Discussions

0
None
YveG1Reichenstein7

Reply 3 years ago on Introduction

Why do you connect Pi's 5V to the 5V on the board??

This seems weird. Everywhere else I read that the board will supply 5V when running off 7-12V supply. But they never say you should put 5V from the Pi in it???!

0
None
Reichenstein7YveG1

Reply 3 years ago on Introduction

This step is not necessary, I like to tie together rails of equal v-value for stability of my projects (some components are heavier draw than others and v-droop is annoying). What is necessary is to make sure your projects components are always grounded together.

0
None
IgorS32Reichenstein7

Reply 3 years ago

That's not a good idea. In the best case it won't do a thing, in the worst (most likely) case, you are shorting 2 voltage sources that are *not* identical, hence generating a voltage delta across a very small resistor.

0
None
shoikat

Question 4 months ago on Introduction

How much motor use in L298N H Bridge motor driver

0
None
alraed_engs

Question 7 months ago

how much voltage and electric current in Blue L298N Dual H Bridge Drive Controller Module?

0
None
surajb26

Question 10 months ago on Introduction

What is the current limit for this motor deriver module to run high amp motor e.i. more than 3 amp?

0
None
flocsy

1 year ago

Hi,

I tried this with both of the modules you have in the instructions (one with the blue connectors for the motor on the opposite side, and the other with the yellow motor connectors on the same side). I am trying to control using PWM, so I use this:

// it's value is calculated in another function, where I read a rotary encoder.

// The value is [0-100] with steps of 10

int pwmSpeed;

void setPWM() {

digitalWrite(LOCO_LEFT_PIN, direction == 1 ? HIGH : LOW);

digitalWrite(LOCO_RIGHT_PIN, direction == -1 ? HIGH : LOW);

analogWrite(LOCO_SPEED_PWM_PIN, pwmSpeed);

}

When I test the code "dry" it all looks good. I turn the rotary encoder 1 click to either side and the values of direction and pwmSpeed are printed out and are correct.

However when I put a loco (model train) on the rails, then whenever I output anything but 0 to the pwm pin, then I just hear a buzz sound from the loco's engine. Any idea why?

1 reply
0
None
flocsyflocsy

Reply 1 year ago

it turned out that it works, it's just the nature of low frequency PWM. On higher duty cycles the motor starts to spin. On higher frequency you don't hear the buzz.

0
None
Champarl

1 year ago

Hello,
According to the L298n'datasheet, their is current sensors on this chip. We can see the output on the "wiring picture" (CSA and CSB pins).
I bought this chip but in the configuration of your first picture .
Any ideas to have an acces to current sensor ?

1 reply
0
None
flocsyChamparl

Reply 1 year ago

did you figure out how to use CSA,CSB with arduino?

0
None
MuhannadS2

1 year ago

I already know the way of change the speed of motors, but i wonder how the H-Bridge control the speed ?

0
None
Packers_Fan

1 year ago

Is it possible to double up on the controllers for a moor that has a higher current draw than what is rated for this device?

1 reply
0
None
VedA1Packers_Fan

Reply 1 year ago

Yes. You could. One driver can control two motors. First parallel both outputs for double current. If that is not enough connect more drivers and parallel all outputs

0
None
Stephen RoyK

1 year ago

Can i use it as 5v power source after connecting 12v supply

0
None
fbujold

2 years ago

I have been searching for a while but i cannot find the answer.

I connected a stepper motor to A and B output, left the 5 volt and both enables jumper in place, connected the IN1-4 to arduino 8-11, feed the arduino with the bridge 5 V source, loaded the arduino "one turn" (200 pulse) stepper sample routine. Runs like a charm for about 90 seconds then thermal protection kicks in??

Any idea anyone?

4 replies
0
None
tengfoong_lamfbujold

Reply 1 year ago

I got the same problem supplying 12V to power my stepper motor. Apparently the battery was supplying too much power to the driver and after awhile, the steppers will stall, probably due to the chip overheating. Essentially, the solution is to use the EnA and EnB pins to control the power going to the stepper.

What I did was:

1) I kept the 12V Jumper ( Labelled number 3 in https://www.instructables.com/id/Control-DC-and-st... on the chip, contrary to instructions online that indicated that is should be removed if supplying >12V. I tried removing it and the entire chip would not function at all.

2) I took out the jumpers from PWM pins EnA and EnB. I connected two Arduino Pins with PWM functionality (Eg 45 and 46 on the Arduino Mega) to EnA and EnB. In the Arduino Code, I used analogWrite for the pins and adjusted the written value (should be around 50-200 depending on power source and stepper motor specs) until the power supplied to the stepper is just right so it can rotate over a long time without the driver overheating).

Note 1: Maximum value for analogWrite is 255, which is equivalent to attaching the jumpers back onto EnA and EnB pins, which means full power from the battery is being supplied to the stepper. So a value lower would reduce power to the stepper and prevent overheating.

Note 2: I use "power" in my answer because I am not sure whether the voltage or current is at fault here. Maybe an electronics expert could shed some light on this?

Yeap that's the answer I came up with based on my experimentation with this motor driver. Most tutorials on the internet disregard the En pins when teaching how to wire up stepper motors, which is a solution that will only work if your battery source is tailored to the stepper motor you are using.

0
None
fbujoldfbujold

Reply 2 years ago

Forgot, i am running the bridge from 7 to 9 VDC, with the same results

0
None
ElmoO2fbujold

Reply 2 years ago

I'd suggest a stepper driver.