Introduction: Control DC and Stepper Motors With L298N Dual Motor Controller Modules and Arduino

Picture of Control DC and Stepper Motors With L298N Dual Motor Controller Modules and Arduino

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

Picture of 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

Picture of 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);
  // now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH); 
  // 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);
  // decelerate from maximum speed to zero
  for (int i = 255; i >= 0; --i)
    analogWrite(enA, i);
    analogWrite(enB, i);
  // now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);  
void loop()

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

Picture of 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:

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”.


hein0170 made it! (author)2016-05-20

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.

keksmail2 (author)hein01702017-10-12

Hi! Could you please share your connection as schema! I have the same driver and the same nema-17 (red + green + blue + black), but my motor just vibrates without moving. I think I missed some wires between nema-17 and L298N.

xjtsfjgnk (author)keksmail22017-10-18

I gues you forgott to connect the gnd of the board and the gnd of your arduino.

i hope this is helpful

keksmail2 (author)xjtsfjgnk2017-10-21

Thank`s a lot! it was really helpful, now it works!

luzho27 (author)hein01702016-05-26

Thanks for sharing!, I was looking how to connect my Nema 17.


luhocruz (author)2017-10-20

Hi, my motor are working right, but he is heating so much! Up to 70°C+-. Im using a Nema 16 ZEPHYR - 9BYG218 / 2S39Q-02034S 1,2kgf 1A. My source is a 12V 10A, im using the l298n and mystepper library arduino. Can you help me? Ps: there are no charge.

JeffH95 (author)2017-09-07

I've got something profoundly weird going on; I've double-checked my wiring and everything seems fine, but the No.1 engine does nothing. I hooked up my cheap oscilloscope to the outputs expecting to see nothing, but the signals appeared to be identical in voltage and behavior to the No.2 side. I hooked up the No.1 side to a resistor and an LED and indeed the light goes on, dims, etc. just as I would expect. I swap the motors and the motor on the No.1 side does nothing and the No. 2 side works. I'm stumped. Anyone got any suggestions?

mankinggeo (author)2017-07-15

Can I use L298N to control a VCR Capstan Motor (without driver)? How can I do this?

vertex28 (author)2016-12-31


great job. In step 1, state 3

"12V jumper – remove this if using a supply voltage greater than 12V DC. This enables power to the onboard 5V regulator"

I use 14.4 V baterry. when i remove jumper, "L298 module" 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 "L298 module".


This is code i used

int LED=13;
volatile int state = LOW;
char getstr;
int in1=9;
int in2=8;
int in3=7;
int in4=6;
int ENA=10;
int ENB=5;
//int ABS=135;

void _mForward()
//analogWrite (ENA, 200); //DOLOČIŠ HTROST MOTORJA
Serial.println("go forward!");
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed to 200 out of possible range 0~255

void _mBack()
//analogWrite (ENA, 200); //DOLOČIŠ HTROST MOTORJA
Serial.println("go back!");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
void _mleft()
//analogWrite (ENB, 200); //DOLOČIŠ HTROST MOTORJA
Serial.println("go left!");
void _mright()
//analogWrite (ENA, 200); //DOLOČIŠ HTROST MOTORJA
Serial.println("go right!");
void _mStop()
digitalWrite(in1,LOW); //jaz dodal - drugače tevrstice ni
digitalWrite(in2,LOW); //jaz dodal - drugače tevrstice ni
digitalWrite(in3,LOW); //jaz dodal - drugače tevrstice ni
digitalWrite(in4,LOW); //jaz dodal - drugače tevrstice ni
void stateChange()
state = !state;
digitalWrite(LED, state);
void setup()
pinMode(LED, OUTPUT);

void loop()
else if(getstr=='B')
else if(getstr=='L')
else if(getstr=='R')
else if(getstr=='A')
else //if(getstr=='B')

htapaha (author)vertex282017-04-21

Have you tried an outside power source? It will not work if the arduino is the only power supply in the system.

MehdiC7 (author)2017-04-10

i need to control stepper motor without libraries ..

Matias IvanM (author)2017-03-16

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?

Thank you for the tutorial

bartowj (author)Matias IvanM2017-03-24

It's not a dual stepper motor controller. It only controls one stepper motor.

OthmanO1 (author)2017-03-17

hi ,

I need to know how many motor drive require to run three steeper motor ??

and how I can know ?

kindly help guys

bartowj (author)OthmanO12017-03-24

3 ea L298Ns

samitha rajakaruna (author)2017-02-26

can i use this motor drive for mg995 metal server motor

T_dawgr made it! (author)2017-01-28

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.

Antoine L (author)T_dawgr2017-02-02

Same here! Did you make it work?

Antoine L (author)Antoine L2017-02-02

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...

IgorV31 (author)Antoine L2017-02-25

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

Tanoshimi2000 (author)2017-02-15

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?

uyegyi (author)2017-02-07

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?

swapnilchaure (author)2017-01-25

does it simila to sabertooth? can be use instead of saberooth??

LouisDT (author)2017-01-25

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?

templas (author)2016-12-30


I have a Nema 17 stepper that works at 3.3v. Can I use this module?


desertwalker (author)2016-12-12


Worked straight away, using two (2) dc motors from an old robot vacuum. Thanks for the information

tquad (author)2016-12-09

Thanks lot for an informative introduction to L298N driver module.

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

hsoleil (author)2016-08-31

good morning !!!

please can one drive a unipolar stepper motor using a 16F877A with this module???

if yes, how to do the connection??

breandanmcewen9 (author)hsoleil2016-11-24

This board is only designed to run bi-polar motors. For uni-polar stepper motors, you must use a driver with the ULN2003 chip.

brandonm225 (author)2016-11-13

Is it possible to drive two NEMA 17 stepper motors from this?

No. You will need one driver board for each stepper motor. You can run two DC brushed motors from this board however...

KeithM78 (author)2016-11-19

Thank you for the code I have a different board but was able to modify yours to get mine functional

Michael_Juan (author)2016-09-24

L298N Dual Motor Controller Module. Can i use 2 of it to control 4 motors?

GabrielC115 (author)2016-09-20


question: why does the module get so hot?

question: how do i increase the amps to 1.7A to get holding torque of 5.2Ncm thankS:)

AdrianP70 (author)2016-04-21

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?

PS: Common ground betwen those 2 power supplies

siddhantchoyal (author)AdrianP702016-08-01

Check out the diagram i just posted friend ............hope it helps you :)

Try without shields. Connect wire +5V to the pin IN1 or IN2 for motor1. It must turn.

nageshsp (author)2016-07-10

Can you please post wiring diagram????

siddhantchoyal (author)nageshsp2016-08-01

Check out my comment bro ..................... at the top !!

siddhantchoyal (author)2016-08-01

Here's the wiring diagram for all of u guys who have doubts ...... BTW nice explanation mahn !!

DeivM (author)2016-07-25

My advice: If you don't use PWM DC motors >12 V, leave 7 an 12 jumpers in place. It will be work perfectly.

nageshsp (author)2016-07-13

I am using arudino Uno R3 and L298n dual full bridge driver... I wired as below:

In1 to Pin8,

In2 to Pin9,

In3 to Pin10 and In4 to Pin11.

I joined the ground wires of power and arduino ground to L298N ground pin.

12v power to power source,

5v power to arduino power pin.

Enable pins I left out because there are 2 pins for enable 1 and 2.

But its not working. Motor only vibrates. Please advice..!!!

يوسفا6 (author)2016-04-20

Thanks for the tutorial

DavidB552 (author)2016-04-03

can someone tell me what I need to do diffrently if I want to run it of a sepreate power supply for the motor ?

DavidB552 (author)DavidB5522016-04-03

oh more so I get how to do most of the things but the speed contol is confusing me

fqantonio (author)2016-03-31

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.

JamesN87 (author)2016-03-30

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.

AnsonN (author)2016-03-25

Thanks for the tutorial. Got it up and running on the first try. Now to see if I can control it through Bluetooth

RaviJ10 (author)2016-03-25

Found sumthng new reated to the same.

GalvinL1 (author)2016-03-17

Can i connect L298n with stepper notor to PLC output? Thank u

About This Instructable



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 ArduinoControl DC and stepper motors with L298N Dual Motor Controller Modules and ArduinoUsing PCF8574 backpacks with LCDs and Arduino
Add instructable to: