loading

You don’t have to spend a lot of money to control motors with an Arduino or compatible board. After some hunting around we found a neat motor control module based on the L298N H-bridge IC that can allows you to control the speed and direction of two DC motors, or control one bipolar stepper motor with ease.

The L298N H-bridge module can be used with motors that have a voltage of between 5 and 35V DC. With the module used in this tutorial, there is also an onboard 5V regulator, so if your supply voltage is up to 12V you can also source 5V from the board.

So let’s get started!

Step 1: Understanding the L298 Module Connections

First we’ll run through the connections, then explain how to control DC motors then a stepper motor. At this point, review the connections on the L298N H-bridge module.

Consider the image – match the numbers against the list below the image:

  1. DC motor 1 “+” or stepper motor A+
  2. DC motor 1 “-” or stepper motor A-
  3. 12V jumper – remove this if using a supply voltage greater than 12V DC. This enables power to the onboard 5V regulator

  4. Connect your motor supply voltage here, maximum of 35V DC. Remove 12V jumper if >12V DC

  5. GND

  6. 5V output if 12V jumper in place, ideal for powering your Arduino (etc)

  7. DC motor 1 enable jumper. Leave this in place when using a stepper motor. Connect to PWM output for DC motor speed control.

  8. IN1

  9. IN2

  10. IN3

  11. IN4

  12. DC motor 2 enable jumper. Leave this in place when using a stepper motor. Connect to PWM output for DC motor speed control

  13. DC motor 2 “+” or stepper motor B+

  14. DC motor 2 “-” or stepper motor B-

Step 2: Controlling DC Motors

To control one or two DC motors is quite easy with the L298N H-bridge module. First connect each motor to the A and B connections on the L298N module.

If you’re using two motors for a robot (etc) ensure that the polarity of the motors is the same on both inputs. Otherwise you may need to swap them over when you set both motors to forward and one goes backwards!

Next, connect your power supply – the positive to pin 4 on the module and negative/GND to pin 5. If you supply is up to 12V you can leave in the 12V jumper (point 3 in the image above) and 5V will be available from pin 6 on the module.

This can be fed to your Arduino’s 5V pin to power it from the motors’ power supply. Don’t forget to connect Arduino GND to pin 5 on the module as well to complete the circuit. Now you will need six digital output pins on your Arduino, two of which need to be PWM (pulse-width modulation) pins.

PWM pins are denoted by the tilde (“~”) next to the pin number, for example in the image of the Arduino Uno's digital pins.

Finally, connect the Arduino digital output pins to the driver module. In our example we have two DC motors, so digital pins D9, D8, D7 and D6 will be connected to pins IN1, IN2, IN3 and IN4 respectively. Then connect D10 to module pin 7 (remove the jumper first) and D5 to module pin 12 (again, remove the jumper).

The motor direction is controlled by sending a HIGH or LOW signal to the drive for each motor (or channel). For example for motor one, a HIGH to IN1 and a LOW to IN2 will cause it to turn in one direction, and a LOW and HIGH will cause it to turn in the other direction.

However the motors will not turn until a HIGH is set to the enable pin (7 for motor one, 12 for motor two). And they can be turned off with a LOW to the same pin(s). However if you need to control the speed of the motors, the PWM signal from the digital pin connected to the enable pin can take care of it.

This is what we’ve done with the DC motor demonstration sketch. Two DC motors and an Arduino Uno are connected as described above, along with an external power supply. Then enter and upload the following sketch:

// 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);
}

So what’s happening in that sketch? In the function demoOne() we turn the motors on and run them at a PWM value of 200. This is not a speed value, instead power is applied for 200/255 of an amount of time at once.


Then after a moment the motors operate in the reverse direction (see how we changed the HIGHs and LOWs in thedigitalWrite() functions?). To get an idea of the range of speed possible of your hardware, we run through the entire PWM range in the function demoTwo() which turns the motors on and them runs through PWM values zero to 255 and back to zero with the two for loops.

Finally this is demonstrated in the video on this page – using our well-worn tank chassis with two DC motors.

Step 3: Controlling a Stepper Motor With Arduino and L298N

Stepper motors may appear to be complex, but nothing could be further than the truth. In this example we control a typical NEMA-17 stepper motor that has four wires, as shown in the image on this step.

It has 200 steps per revolution, and can operate at at 60 RPM. If you don’t already have the step and speed value for your motor, find out now and you will need it for the sketch.

The key to successful stepper motor control is identifying the wires – that is which one is which. You will need to determine the A+, A-, B+ and B- wires. With our example motor these are red, green, yellow and blue. Now let’s get the wiring done.

Connect the A+, A-, B+ and B- wires from the stepper motor to the module connections 1, 2, 13 and 14 respectively. Place the jumpers included with the L298N module over the pairs at module points 7 and 12. Then connect the power supply as required to points 4 (positive) and 5 (negative/GND).

Once again if your stepper motor’s power supply is less than 12V, fit the jumper to the module at point 3 which gives you a neat 5V power supply for your Arduino. Next, connect L298N module pins IN1, IN2, IN3 and IN4 to Arduino digital pins D8, D9, D10 and D11 respectively.

Finally, connect Arduino GND to point 5 on the module, and Arduino 5V to point 6 if sourcing 5V from the module. Controlling the stepper motor from your sketches is very simple, thanks to the Stepper Arduino library included with the Arduino IDE as standard.

To demonstrate your motor, simply load the stepper_oneRevolution sketch that is included with the Stepper library. To find this, click the File > Examples > Stepper menu in the Arduino IDE.

Finally, check the value for

const int stepsPerRevolution = 200;
in the sketch and change the 200 to the number of steps per revolution for your stepper motor, and also the speed which is preset to 60 RPM in the following line:
myStepper.setSpeed(60);

Now you can save and upload the sketch, which will send your stepper motor around one revolution, then back again. This is achieved with the function

<p>myStepper.step(stepsPerRevolution); // for clockwise </p><p>	myStepper.step(-stepsPerRevolution); // for anti-clockwise

Finally, a quick demonstration of our test hardware is shown in the video on this step.

So there you have it, an easy an inexpensive way to control motors with your Arduino or compatible board. And if you enjoyed this article, or want to introduce someone else to the interesting world of Arduino – check out my book (now in a fourth printing!) “Arduino Workshop”.

<p>Hello</p><p>great job. In step 1, state 3</p><p>&quot;12V jumper &ndash; remove this if using a supply voltage greater than 12V DC. This enables power to the onboard 5V regulator&quot;</p><p>I use 14.4 V baterry. when i remove jumper, &quot;L298 module&quot; shut down, if i put jumper back i get only around 5v to DC motor. I control motor through bluetooth modul. How to get 12V from &quot;L298 module&quot;.</p><p>THANKS A LOT</p><p>This is code i used</p><p><br>int LED=13;<br>volatile int state = LOW;<br>char getstr;<br>int in1=9;<br>int in2=8;<br>int in3=7;<br>int in4=6;<br>int ENA=10;<br>int ENB=5;<br>//int ABS=135;</p><p>void _mForward()<br>{ <br> //analogWrite (ENA, 200); //DOLOČIŠ HTROST MOTORJA<br> digitalWrite(ENA,HIGH);<br> digitalWrite(ENB,HIGH);<br> ///digitalWrite(in1,LOW);<br> ///digitalWrite(in2,HIGH);<br> //digitalWrite(in3,LOW);<br> //digitalWrite(in4,HIGH);<br> Serial.println(&quot;go forward!&quot;);<br> digitalWrite(in1, HIGH);<br> digitalWrite(in2, LOW);<br> // set speed to 200 out of possible range 0~255<br> <br>}<br>void _mBack()<br>{<br> //analogWrite (ENA, 200); //DOLOČIŠ HTROST MOTORJA<br> digitalWrite(ENA,HIGH);<br> digitalWrite(ENB,HIGH);<br> ///digitalWrite(in1,HIGH);<br> ///digitalWrite(in2,LOW);<br> //digitalWrite(in3,HIGH);<br> //digitalWrite(in4,LOW);<br> Serial.println(&quot;go back!&quot;);<br> digitalWrite(in1, LOW);<br> digitalWrite(in2, HIGH); <br>}<br>void _mleft()<br>{<br> //analogWrite (ENB, 200); //DOLOČIŠ HTROST MOTORJA<br> digitalWrite(ENA,HIGH);<br> digitalWrite(ENB,HIGH);<br> //digitalWrite(in1,LOW);<br> //digitalWrite(in2,HIGH);<br> digitalWrite(in3,HIGH);<br> digitalWrite(in4,LOW);<br> Serial.println(&quot;go left!&quot;);<br>}<br>void _mright()<br>{<br> //analogWrite (ENA, 200); //DOLOČIŠ HTROST MOTORJA<br> digitalWrite(ENA,HIGH);<br> digitalWrite(ENB,HIGH);<br> //digitalWrite(in1,HIGH);<br> //digitalWrite(in2,LOW);<br> digitalWrite(in3,LOW);<br> digitalWrite(in4,HIGH);<br>Serial.println(&quot;go right!&quot;);<br>}<br>void _mStop()<br>{<br> digitalWrite(ENA,LOW);<br> digitalWrite(ENB,LOW);<br> digitalWrite(in1,LOW); //jaz dodal - drugače tevrstice ni<br> digitalWrite(in2,LOW); //jaz dodal - drugače tevrstice ni<br> digitalWrite(in3,LOW); //jaz dodal - drugače tevrstice ni<br> digitalWrite(in4,LOW); //jaz dodal - drugače tevrstice ni<br> Serial.println(&quot;Stop!&quot;);<br>}<br>void stateChange()<br>{<br> state = !state;<br> digitalWrite(LED, state); <br>}<br>void setup()<br>{ <br> pinMode(LED, OUTPUT);<br> Serial.begin(9600);<br> pinMode(in1,OUTPUT);<br> pinMode(in2,OUTPUT);<br> pinMode(in3,OUTPUT);<br> pinMode(in4,OUTPUT);<br> pinMode(ENA,OUTPUT);<br> pinMode(ENB,OUTPUT);<br> _mStop();<br>}</p><p><br>void loop()<br>{ <br> getstr=Serial.read();<br> if(getstr=='F')<br> {<br> _mForward();<br> delay(10);<br> }<br> else if(getstr=='B')<br> {<br> _mBack();<br> delay(10);<br> }<br> else if(getstr=='L')<br> {<br> _mleft();<br> delay(10);<br> }<br> else if(getstr=='R')<br> {<br> _mright();<br> delay(10);<br> }<br> else if(getstr=='A')<br> {<br> stateChange();<br> }<br> else //if(getstr=='B')<br> {<br> _mStop(); <br> }<br> }</p>
<p>Have you tried an outside power source? It will not work if the arduino is the only power supply in the system.</p>
<p>i need to control stepper motor without libraries ..</p>
<p>Good tutorial, but could you explain with a simple schema, how to make the correct connection to use a bipolar stepper with this Dual controller?</p><p>Thank you for the tutorial</p>
<p>It's not a dual stepper motor controller. It only controls one stepper motor.</p>
<p>hi , </p><p>I need to know how many motor drive require to run three steeper motor ??</p><p>and how I can know ? </p><p>kindly help guys </p><p>oos8@outlook.com </p>
<p>3 ea L298Ns</p>
<p>can i use this motor drive for mg995 metal server motor</p>
<p>hey i made all the connections but my motors are not working. i m appplying 9V voltage across the motor controller but only red led lights up and not motion is observed on the output. can anyone please help me out. pic are attached below.</p>
<p>Same here! Did you make it work?</p>
<p>Ok i fixed it! i had a mistake: I was trying to power Arduino from USB and the L298N Board from a battery pack...that way it wasn't working and i don't know the reason...But it got fixed when i powered the circuit only from battery...</p>
<p>You can't power things up using different ground reference and you can't connect your usb ground to the battery ground (that would fry your pc usb port (at least I think that's what happened to me a long time ago), but the ln298n has a 5v voltage regulator so you can use the 5v and the GND from the ln298n board to power your arduino up instead of using your usb port</p>
<p>I want to set this up to have a 12V Drill Motor connected to L298N and Arduino, plugged into a 12V wall power supply for power, and the USB port for sending commands to the motor. Is there a difference in wiring it this way? Also, is it a problem to have the 12V wall power supply, but then also have 5V from the USB that's connected for data?</p>
pls <br>help me i have 10dof imu sensor b and neo 7m gps and arduino due.i want to track location and position and diviation angle of coordinate with imu,how to write code?
<p>does it simila to sabertooth? can be use instead of saberooth?? </p><p><a href="https://www.dimensionengineering.com/products/sabertooth2x5" rel="nofollow">https://www.dimensionengineering.com/products/sabe...</a></p><p>https://www.instructables.com/id/Self-Balancing-Segway-Instructabot/?ALLSTEPS</p>
<p>I have a problem. When i power up the L298N with a 9V batteri, and i try to power up a little dc motor (6-9V), then the amount of output voltages falls over the first couple of seconds until there is no output voltage at all. I do not know what i am doing wrong. Can anyone please help?</p>
<p>Hi.</p><p>I have a Nema 17 stepper that works at 3.3v. Can I use this module?</p><p>Thanks.</p>
<p>Hello,</p><p>Worked straight away, using two (2) dc motors from an old robot vacuum. Thanks for the information</p>
<p>Thanks lot for an informative introduction to L298N driver module.</p><p>Have been tinkering with it. Can you please tell me how to stop the stepper say for 3 min. If the delay() function is used will the stepper still be drawing current or will the Driver L298 get hot for drawing excessive current. Please your comments</p>
<p>good morning !!! </p><p>please can one drive a unipolar stepper motor using a 16F877A with this module???</p><p>if yes, how to do the connection??</p>
<p>This board is only designed to run bi-polar motors. For uni-polar stepper motors, you must use a driver with the ULN2003 chip.</p>
<p>Is it possible to drive two NEMA 17 stepper motors from this? </p>
<p>No. You will need one driver board for each stepper motor. You can run two DC brushed motors from this board however...</p>
<p>Thank you for the code I have a different board but was able to modify yours to get mine functional</p>
L298N Dual Motor Controller Module. Can i use 2 of it to control 4 motors?
<p>AWESOME TUTORIAL, IT WORKS!!!:)</p><p>question: why does the module get so hot?</p><p>question: how do i increase the amps to 1.7A to get holding torque of 5.2Ncm thankS:)</p>
<p>Can anyone help me out.I bought a l298n shield identical with the one in this tutorial, but mine isn't working at all.I've tried with arduino uno 5v, arduino mini 3.3v ,nodemcu esp8266 3.3v ,even straight from power supply (gnd and 9v) ,out1 and out2 with 9v motor conected and ENA(HIGH 5V) ,IN1(HIGH 5V),IN2(LOW GND) ,and still no power to motor (0,0v).Any ideas?</p><p>PS: Common ground betwen those 2 power supplies</p>
<p>Check out the diagram i just posted friend ............hope it helps you :)</p>
<p>Try without shields. Connect wire +5V to the pin IN1 or IN2 for motor1. It must turn.</p>
<p>Can you please post wiring diagram????</p>
<p>Check out my comment bro ..................... at the top !!</p>
<p>Here's the wiring diagram for all of u guys who have doubts ...... BTW nice explanation mahn !! </p>
<p>My advice: If you don't use PWM DC motors &gt;12 V, leave 7 an 12 jumpers in place. It will be work perfectly.</p>
<p>I am using arudino Uno R3 and L298n dual full bridge driver... I wired as below:</p><p>In1 to Pin8, </p><p>In2 to Pin9, </p><p>In3 to Pin10 and In4 to Pin11. </p><p>I joined the ground wires of power and arduino ground to L298N ground pin. </p><p>12v power to power source, </p><p>5v power to arduino power pin. </p><p>Enable pins I left out because there are 2 pins for enable 1 and 2.</p><p></p><p>But its not working. Motor only vibrates. Please advice..!!!</p>
<p>Got it working in first try. The setup is a NEMA17 stepper motor controlled by an Arduino Nano that is powered by the 5V output on the controller module. Just tried the Arduino stepper sketch described in this instructable. Thank you.</p>
<p>Thanks for sharing!, I was looking how to connect my Nema 17. </p><p>Regards!</p>
<p>Thanks for the tutorial</p>
<p>can someone tell me what I need to do diffrently if I want to run it of a sepreate power supply for the motor ?</p>
<p>oh more so I get how to do most of the things but the speed contol is confusing me </p>
<p>Hello, thank you very much for your tutorial. I have a question, why do we need to connect two outputs to one motor, for example, IN1 and IN2? I'm use to use two outputs for each engine, one in PWM , the other to enable it.</p>
<p>Useless for me. I am trying to interface with a Basic Stamp, and will write my own code, but do not know what the four input lines are.</p>
<p>Thanks for the tutorial. Got it up and running on the first try. Now to see if I can control it through Bluetooth</p>
<p>Found sumthng new reated to the same.</p>
Can i connect L298n with stepper notor to PLC output? Thank u
<p>I got my 4 wire bipolar stepper to work.</p><p>For those folks that have issues, the biggest one is identifying which two wire pairs go together. Use a multimeter and test for which wires have minimal resistance. Those are the pairs.</p><p>As for which is positive and negative, just guess. If the motor &quot;stutters&quot; with the rpm set at a low (60 rpm) speed, it's likely one of the wire pairs is reversed.</p>
<p>Spent a good day trying to get it to work. Finally figured out my connections were bad. Those blue connectors on the board are not the best, so be sure to strip plenty of wire, and do not tuck the wire all the way into the back of them!</p>
<p>Thank you,thank you,thank you...it took me 15 min to test driver and motors with this tutorial....</p>
<p>Hello. I've purchased Sparkfun ROB 09 238. Haven't got a clue how to connect your driver to this one. Thx for help.</p>
thankyou i got it...
<p>Hi<br> I am trying to Control a Stepper Motor with Arduino and L298N just like in the video above and I am getting erratic motion. The red Led on the L298N module goes on and off.</p><p>I am using a ' Nema 17 42mm Hybrid Stepper Motor 70oz-in 48mm 2.5A' and a '12V 3A Power Supply Adapter Transformer' to power the stepper.</p>

About This Instructable

306,811views

282favorites

Bio: Helping you make it with Australia's best value for supported hobbyist electronics from adafruit, DFRobot, Freetronics, Seeed Studio and more!
More by tronixlabs:Tutorial - Using HC06 Bluetooth to Serial Wireless UART Adaptors with Arduino Control DC and stepper motors with L298N Dual Motor Controller Modules and Arduino Using PCF8574 backpacks with LCDs and Arduino 
Add instructable to: