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

564,723

376

69

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

About: Fun, interesting and educational electronics kits and devices in Australia

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

5 People Made This Project!

Recommendations

  • Big and Small Contest

    Big and Small Contest
  • For the Home Contest

    For the Home Contest
  • Make It Bridge

    Make It Bridge

69 Comments

0
macg.mc92
macg.mc92

1 year ago

Hola buenos días compañeros.

Neceaito que alguien me ayude donde puedo conseguir la librería stepper h para controlar un nema 17 para controlarlo con el L298N dual

16226981573201300704561809797635.jpg
0
GAMKCMO
GAMKCMO

Question 2 years ago on Step 2

I have used the L298n and was able to get it going. I'm trying to move on to the DRV8825 with the expansion bd. Not able to find much specific info about the differences in coding these 2 drivers. Would like to go onto the closed loop S42b.

0
luhocruz
luhocruz

5 years ago

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.

0
kasperuza
kasperuza

Reply 2 years ago

I had the same problem. With all the Arduino projects are there out and we just copy and paste from any site, we forgot that people can make mistakes and everything can go wrongly.
The L298N is a controller that mostly works better with no Stepper motors or with Stepper motors, high voltage. There is a trick to work with this module and a stepper motors like those but I do not recommend because it just not feel right. Basically the trick is not keeping the motor with "HIGH" signal on the 2 coils after spinning. So what I did was a stop function in Arduino to set "LOW" on the four OUTS.
On the other hand, the best Drivers for Nema17 motors or similar are: DRV8825 and A4988. I suggest you to use any of those two and you will be fine.

0
mvniemi
mvniemi

Reply 5 years ago

I had similar issues, turns out the l298n is not well suited to driving low-impedance low voltage stepper motors (your motor is 2.9ohm and 2.0v giving a rated 0.7 amps) because at its working voltage it draws too much current and will quickly overheat. I recommend using a "chopper" style driver like the StepStick, they use current limiting and can work with these types of motors.

0
Farrukh_
Farrukh_

3 years ago

it seems we don't need to remove jumper that enable 5V output if input voltage is above 12V, as per datasheet, regulator can accept up to 35V. there are also two 35V capacitors on board. not sure if for regulator or l298 chip.

0
KinzaW1
KinzaW1

4 years ago

Thank you so much, helped me so much

0
HarisA31
HarisA31

Question 4 years ago on Step 3

Hi, I have arduino leonardo and BTS7960 for control a dc motor. Now I want change dc motor to stepper motor.
is this driver the same as bts7960? because I'm not sure the firmware installed on Arduino can't work with l298n

0
SEB23232323
SEB23232323

4 years ago

I am controlling a 12 V DC motor, the input voltage (between pin 4 and 5 ) is 11,68 V, I am using a pwm value of 255 and when I measure the output voltage to the motor (between pin 14 and 13) I found 6.28 V. Is that normal, because it is suppose to be equal to the input voltage (11.68V). (only one motor is connected)

0
vertex28
vertex28

6 years ago

Hello

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

THANKS A LOT

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
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
///digitalWrite(in1,LOW);
///digitalWrite(in2,HIGH);
//digitalWrite(in3,LOW);
//digitalWrite(in4,HIGH);
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
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
///digitalWrite(in1,HIGH);
///digitalWrite(in2,LOW);
//digitalWrite(in3,HIGH);
//digitalWrite(in4,LOW);
Serial.println("go back!");
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
void _mleft()
{
//analogWrite (ENB, 200); //DOLOČIŠ HTROST MOTORJA
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
//digitalWrite(in1,LOW);
//digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go left!");
}
void _mright()
{
//analogWrite (ENA, 200); //DOLOČIŠ HTROST MOTORJA
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
//digitalWrite(in1,HIGH);
//digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go right!");
}
void _mStop()
{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
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
Serial.println("Stop!");
}
void stateChange()
{
state = !state;
digitalWrite(LED, state);
}
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(9600);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
_mStop();
}


void loop()
{
getstr=Serial.read();
if(getstr=='F')
{
_mForward();
delay(10);
}
else if(getstr=='B')
{
_mBack();
delay(10);
}
else if(getstr=='L')
{
_mleft();
delay(10);
}
else if(getstr=='R')
{
_mright();
delay(10);
}
else if(getstr=='A')
{
stateChange();
}
else //if(getstr=='B')
{
_mStop();
}
}

0
matjaz.zikovsek
matjaz.zikovsek

Reply 4 years ago

Note that the 5V regulated power on pin 5 above is an output when the 5V_EN jumper is in place. Otherwise you must input 5V regulated power at pin 5 so that the circuit can operate properly. Do not enable the onboard 5V regulator if you are supplying more than 12V to motors on pin 3 or the regulator will burn out.

Torej, če napajaš regulator z več kot 12V in odstraniš mostiček 5V_EN, moraš na pin 5 priklopiti +5V, za delovanje TTL logike regulatorja.

0
htapaha
htapaha

Reply 5 years ago

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

0
MohammadH144
MohammadH144

Question 4 years ago on Step 2

I want to call because some problems

0
MaharshiP4
MaharshiP4

Question 5 years ago on Introduction

hey there,
i need to connect four 12v dc motor to motor driver L298N .
how can i connect it , or i have two two motor driver for that .
pl..answer with proper steps how to connect.
thanks in advance.

0
JeffH95
JeffH95

5 years ago

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?

0
mankinggeo
mankinggeo

5 years ago

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

0
MehdiC7
MehdiC7

5 years ago

i need to control stepper motor without libraries ..

0
Matias IvanM
Matias IvanM

6 years ago

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

0
bartowj
bartowj

Reply 6 years ago

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

0
OthmanO1
OthmanO1

6 years ago

hi ,

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

and how I can know ?

kindly help guys

oos8@outlook.com