L298 Motor Driver Board

Posted on by
L298 Motor Driver Board Rating: 5,6/10 2895 votes

In this article you will learn how to control a stepper motor with the L298N Motor Driver. This driver board is usually used to control DC motors, but it is also an inexpensive alternative to control stepper motors! It can control both the speed and the spinning direction of most stepper motors like a NEMA 17.

L298 Motor Driver PCB. EAGLE files for L298 Dual H-Bridge Motor Driver Board. Also available at OSH Park. Using the L298 Motor Bridge IC. A very popular and reasonably priced all-in-one H-bridge motor driver is the L298. It can control two motors, not just one. It can handle 2 amps per motor, though to get the maximum current be sure to add a heat sink.

I have included a wiring diagram and many example codes. In the first example we will look at the Stepper.h Arduino library. I highly recommend to also take a look at the example codes for the AccelStepper library at the end of this tutorial. This library is fairly easy to use and can greatly improve the performance of your hardware.

After each example, I break down and explain how the code works, so you should have no problems modifying it to suit your needs.


If you would like to learn more about other stepper motor drivers, then the articles below might be useful:

The Arduino Motor Shield Rev3 also uses a L298 driver.

Information about the L298N Motor Driver

The L298N Motor Driver Board is built around the L298 dual full-bridge driver, made by STMicroelectronics. With this motor driver you can control DC motors, stepper motors, relays and solenoids. It comes with two separate channels, called A and B, that you can use to drive 2 DC motors, or 1 stepper motor when combined.

The L298N is usually mounted on a (red) breakout board, which makes wiring a lot easier. The breakout board also includes a 78M05 5 V power regulator.

Why is my stepper motor getting HOT?

One thing that is very important to remember is that the L298 does not have an easy way to set a current limit unlike other stepper motor drivers. This means that the current draw depends on the relationship between the inductance and resistance (L/R) of the stepper motor that you connect to it. When the motor draws too much current, you can damage the driver and the motor will get hot!

What this means for you, is that you you need to be careful when selecting the stepper motor and power supply to use with this motor driver. Not all stepper motors will work! The L298N operating voltage is between 4.8 and 46 volts (max 35 V when mounted on the breakout board). Since the driver can supply a maximum of 2 amperes per channel, you need to find a stepper motor that can be used in this voltage range and doesn’t exceed the maximum current rating.

Check the datasheet of your stepper motor and look for the voltage/current draw of the motor. If you can’t find the datasheet, you can measure the resistance of one of the windings and use the following formula to get an estimation of the current draw:

I = U ÷ R or Current draw (A) = Supply voltage (V) ÷ Winding resistance (Ω)

I would try to find a motor that draws less than 2 A at the voltage that you want to use.

The motor I used for this tutorial draws around 1 A at 5 V. I also found this stepper motor from Adafruit that works great at 12V and only draws 350 mA.

If the motor you want to drive doesn’t work with the L298N motor driver, it is best to use a chopper drive instead. I wrote tutorials for the A4988 and DRV8825 driver that work great with many stepper motors.

L298N Motor Driver Specifications

Operating voltage5 – 35 V
Logic voltage4.5 – 7 V
Max current2 A per channel or 4 A max
Motor controllerL298N, drives 2 DC motors or 1 stepper motor
Voltage regulator78M05
Module dimensions43 x 43 x 28 mm
Hole dimensions3.2 mm, 37 mm spacing
CostCheck price

For more information, you can check out the datasheet below:

L298N Pinout

The L298 comes in several different packages, the pinout for the L298N (Multiwatt15) is given below:

L298N (Multiwatt15) pinout
Pin no.NameFunction
1, 15Sense A, Sense BThe sense resistor needs to be connected between this pin and GND (not used on breakout board).
2, 3Out 1, Out2Outputs of the Bridge A; the current that flows through the load connected between these two pins is monitored at pin 1.
4VsSupply Voltage for the Power Output Stages
5, 7Input 1, Input 2TTL Compatible Inputs of Bridge A
6, 11Enable A, Enable BTTL Compatible Enable Input: the LOW state disables the bridge A (enable A) and/or the bridge B (enable B).
8GNDGround
9VSSSupply Voltage for the Logic Blocks.
10, 12Input 3, Input 4TTL Compatible Inputs of the Bridge B.
13, 14Out 3, Out4Outputs of the Bridge B; the current that flows through the load connected between these two pins is monitored at pin 15.


Tools and Materials

The follow this tutorial, you will need the following components.

Hardware components

L298N Motor Driver Boardx 1
NEMA 17 stepper motorIn this tutorial I used a bipolar stepper motor with 4 wires. Try to find a stepper motor that can run at 12 V and doesn’t exceed the 2 A current limit. This one from Adafruit works great at 12V and doesn’t draw too much current (350 mA).x 1
Arduino UNO R3If you want to control multiple stepper motors, you can use an Arduino Mega.x 1
Power supply (12 V)x 1
Jumper wiresMale-female and male-male~ 10
USB Type-B cablex 1

Software

Arduino IDE

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com. Amazon and the Amazon logo are trademarks of Amazon.com, Inc, or its affiliates.

Wiring – Connecting L298N to stepper motor and Arduino

The wiring diagram/schematic below shows you how to connect a stepper motor, power supply and Arduino to the L298N breakout board.

The connections are also given in the table below:

L298N Connections

L298NConnection
+12V5 – 35 V power supply
GNDPower supply and Arduino ground
12 V jumperRemove if motor power > 12 V!
5V+ (optional)5 V Arduino if 12 V jumper is removed
IN1Pin 8 Arduino
IN2Pin 9 Arduino
IN3Pin 10 Arduino
IN4Pin 11 Arduino
ENA and ENB jumperLeave installed
OUT1 + OUT2Stepper motor coil A
OUT3 + OUT4Stepper motor coil B

Important note: remove the +12V jumper if you are using a power supply higher than 12 V.

When the +12V jumper is attached, the on-board voltage regulator is enabled and it will create the 5 V logic voltage.

You also need to keep both the ENA and ENB jumpers in place so the the motor is always enabled.

How to determine the stepper motor wiring?

If you can’t find the datasheet of your stepper motor, it can be difficult to figure out how to wire your motor correctly. I use the following trick to determine how to connect 4 wire bipolar stepper motors:

The only thing you need to identify is the two pairs of wires which are connected to each of the two coils. One coil gets connected to OUT1 and OUT2 and the other to OUT3 and OUT4, the polarity doesn’t matter.

To find the two wires from one coil, do the following with the motor disconnected:

  1. Try to spin the shaft of the stepper motor by hand and notice how hard it is to turn.
  2. Now pick a random pair of wires from the motor and touch the bare ends together.
  3. Next, try to spin the shaft of the stepper motor again.
  4. If you feel a lot of resistance, you have found a pair of wires from the same coil. If you can spin the shaft freely, try another pair of wires.
Driver

Now connect the two coils to the pins shown in the wiring diagram above.

(If it is still unclear, please leave a comment below, more info can also be found on the RepRap.org wiki)


Stepper.h library example code for L298N driver with stepper motor and Arduino

You can upload the following example code to your Arduino using the Arduino IDE.

This example uses the Stepper.h library, which should come pre-installed with the Arduino IDE. You can find it by going to Sketch > Include Library > Stepper.

This sketch turns the stepper motor 1 revolution in one direction, pauses, and then turns 1 revolution in the other direction.

How the code works:

The sketch starts by including the Stepper.h Arduino library. More information about this library can be found on the Arduino website.

Next we need to define how many steps it takes for the motor to rotate 1 revolution. n this example we will be using the motor in full step mode. This means it takes 200 steps to rotate 360 degrees. You can change this value if you want if you are using a different type of stepper motor or setup.

After this, you need to create a new instance of the Stepper class, which represents a particular stepper motor connected to the Arduino. For this we use the function Stepper(steps, pin1, pin2, pin3, pin4) where steps is the number of steps per revolution and pin1 through pin4 are the pins used to drive the stepper motor. In our case these are pins 8, 9, 10 and 11.

In this case I called the stepper motor ‘myStepper’ but you can use other names as well, like ‘z_motor’ or ‘liftmotor’ etc. Stepper liftmotor = Stepper(stepsPerRevolution, 8, 9, 10, 11);. The name ‘myStepper’ will be used to set the speed and number of steps for this particular motor. Note that you can create multiple stepper objects with different names if you want to control more than one motor.

In the setup() we define the speed of the motor. You can set the speed of the motor in RPM with the function setSpeed(rpm). I set it to 100, so we should see around 1.6 revolutions per second.

In the loop section of code, we simply call the step(steps) function which turns the motor a specific number of steps at a speed determined by the setSpeed(rpm) function. Passing a negative number to this function reverses the spinning direction of the motor.

Note that the step(steps) function is blocking, this means it will wait until the motor has finished moving to pass control to the next line in your sketch.


Installing the AccelStepper library

In the following three examples I will show you how you can control both the speed, the direction and the number of steps the stepper motor should take. In this example I will be using the AccelStepper library.

The AccelStepper library written by Mike McCauley is an awesome library to use for your project. One of the advantages is that it supports acceleration and deceleration, but it has a lot of other nice functions too.

You can download the latest version of this library here or click the button below.

You can install the library by going to Sketch > Include Library > Add .ZIP Library… in the Arduino IDE.

Another option is to navigate to Tools > Manage Libraries… or type Ctrl + Shift + I on Windows. The Library Manager will open and update the list of installed libraries.

You can search for ‘accelstepper‘ and look for the library by Mike McCauley. Select the latest version and then click Install.


1. Continuous rotation AccelStepper example code

The following sketch can be used to run one or more stepper motors continuously at a constant speed. (No acceleration or deceleration is used).

How the code works:

The first step is to include the library with #include <AccelStepper.h>.

The next step is to define the motor interface type. The motorinterface type must be set to 4 when using a 4 wire stepper motor in full step mode (200 steps/revolution). You can find the other interface types here.

The statement #define is used to give a name to a constant value. The compiler will replace any references to this constant with the defined value when the the program is compiled. So everywhere you mention motorInterfaceType, the compiler will replace it with the value 4 when the program is compiled.

Next, you need to create a new instance of the AccelStepper class with the appropriate motor interface type and connections.

In this case I called the stepper motor ‘stepper’ but you can use other names as well, like ‘z_motor’ or ‘liftmotor’ etc. AccelStepper liftmotor = AccelStepper(motorInterfaceType, 8, 9, 10, 11);. As you saw in the previous example, the name that you give to the stepper motor will be used later to set the speed, position and acceleration for that particular motor. You can create multiple instances of the AccelStepper class with different names and pins. This allows you to easily control 2 or more stepper motors at the same time.

In the setup() section of the code we define the maximum speed in steps/second. Speeds of more than 1000 steps per second can be unreliable, so I set this as the maximum. Note that I specify the name of the stepper motor (‘stepper’), for which I want to define the maximum speed. If you have multiple stepper motors connected, you can specify a different speed for each motor:

In the loop() we first set the speed that we want the motor to run at. For this we use the function setSpeed(). (you can also place this in the setup section of the code).

stepper.runSpeed() polls the motor and when a step is due, executes 1 step. This depends on the set speed and the time since the last step. If you want to change the direction of the motor, you can set a negative speed: stepper.setSpeed(-400); turns the motor the other way.


2. Example code to control number of steps or revolutions

With the following sketch you can control both the speed, direction and the number of steps/revolutions.

In this case, the stepper motor turns 2 revolutions clockwise with 200 steps/sec, then turns 1 revolution counterclockwise at 600 steps/sec, and lastly turns 3 revolutions clockwise at 400 steps/sec.

Code explanation:

The first part of the code up to the loop() section is exactly the same as in the previous example.

In the loop I make use of a while loop in combination with the currentPosition() function. First, I set the current position of the stepper motor to zero with stepper.setCurrentPosition(0).

Next we make use of the while loop. A while loop will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. So in this case I check if the current position of the stepper motor is not equal to 200 steps (!= means: is not equal to). While this is not the case, we run the stepper motor at a constant speed as set by setSpeed().

In the rest of the loop, we do exactly the same, just with a different speed and target position.


3. Acceleration and deceleration example code

In this example we will look at one of the main reasons to use the AccelStepper library.

With the following sketch you we can add acceleration and deceleration to the movements of the stepper motor, without any complicated coding. The first section of this sketch is the same as in example 1, but the setup and the loop are different.

The motor will run five revolutions back and forth with a speed of 200 steps per second and an acceleration of 50 steps/second2.

How the code works:

In the setup(), besides the maximum speed, we need to define the acceleration/deceleration. For this we use the function setAcceleration().

In the loop section of the code, I used a different way to let the motor rotate a predefined number of steps. First I set the target position with the function moveTo(). Next, we simply use the function runToPosition() to let the motor run to the target position with the set speed and acceleration. The motor will decelerate before reaching the target position.

Finally, we set the new target position back to the 0, so that we return to the origin.


Conclusion

In this article I have shown you how you can control a stepper motor with a L298N motor driver. We have looked at 4 examples, using both the Stepper and AccelStepper libraries. I hope you found it useful and informative. If you did, please share it with a friend that also likes electronics!

I would love to know what projects you plan on building (or have already built) with the L298N motor driver. If you have any questions, suggestions or if you think that things are missing in this tutorial, please leave a comment down below.

Note that comments are held for moderation in order to prevent spam.

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Beginner

If you are planning on assembling your new robot friend, you will eventually want to learn about controlling DC motors. One of the easiest and inexpensive way to control DC motors is to interface L298N Motor Driver with Arduino. It can control both speed and spinning direction of two DC motors.

And as a bonus, it can even control a bipolar stepper motor like NEMA 17.

Control Stepper Motor with L298N Motor Driver & Arduino
One of the easiest and inexpensive way to control stepper motors is to interface L298N Motor Driver with Arduino. It can control both speed and...

Controlling a DC Motor

In order to have a complete control over DC motor, we have to control its speed and rotation direction. This can be achieved by combining these two techniques.

  • PWM – For controlling speed
  • H-Bridge – For controlling rotation direction

PWM – For controlling speed

L298 Motor Driver Board Pdf Software

The speed of a DC motor can be controlled by varying its input voltage. A common technique for doing this is to use PWM (Pulse Width Modulation)

PWM is a technique where average value of the input voltage is adjusted by sending a series of ON-OFF pulses.

The average voltage is proportional to the width of the pulses known as Duty Cycle.

The higher the duty cycle, the greater the average voltage being applied to the dc motor(High Speed) and the lower the duty cycle, the less the average voltage being applied to the dc motor(Low Speed).

Below image illustrates PWM technique with various duty cycles andaverage voltages.

H-Bridge – For controlling rotation direction

The DC motor’s spinning direction can be controlled by changing polarity of its input voltage. A common technique for doing this is to use an H-Bridge.

An H-Bridge circuit contains four switches with the motor at the center forming an H-like arrangement.

Closing two particular switches at the same time reverses the polarity of the voltage applied to the motor. This causes change in spinning direction of the motor.

Below animation illustrates H-Bridge circuit working.

L298N Motor Driver IC

At the heart of the module is the big, black chip with chunky heat sink is an L298N.

The L298N is a dual-channel H-Bridge motor driver capable of driving a pair of DC motors. That means it can individually drive up to two motors making it ideal for building two-wheel robot platforms.

For more details please refer below datasheet.

Power Supply

The L298N motor driver module is powered through 3-pin 3.5mm-pitch screw terminals. It consists of pins for motor power supply(Vs), ground and 5V logic power supply(Vss).

NOTE

The L298N motor driver IC actually has two input power pins viz. ‘Vss’ and ‘Vs’.

From Vs pin the H-Bridge gets its power for driving the motors which can be 5 to 35V. Vss is used for driving the logic circuitry which can be 5 to 7V. And they both sink to a common ground named ‘GND’.

The module has an on-board 78M05 5V regulator from STMicroelectronics. It can be enabled or disabled through a jumper.

When this jumper is in place, the 5V regulator is enabled, supplying logic power supply(Vss) from the motor power supply(Vs). In this case, 5V input terminal acts as an output pin and delivers 5V 0.5A. You can use it to power up the Arduino or other circuitry that requires 5V power supply.

When the jumper is removed, the 5V regulator gets disabled and we have to supply 5 Volts separately through 5 Volt input terminal.

WARNING

You can put the jumper in place, if the motor power supply is below 12V. If it is greater than 12V, you should remove the jumper to avoid the onboard 5V regulator from getting damaged.

Also DO NOT supply power to both the motor power supply input and 5V power supply input when jumper is in place.

Voltage Drop of L298N

The voltage drop of the L298N motor driver is about 2V. This is due to the internal voltage drop in the switching transistors in the H-Bridge circuit.

So, if we connect 12V to the motor power supply terminal, the motors will receive voltage around 10V. This means that a 12V DC motor will never spin at its maximum speed.

To get maximum speed out of motor, the motor power supply should be bit higher voltage(2V) than motor’s actual voltage requirement.

Considering the voltage drop of 2V, if you are using 5V motors you’ll need to provide 7V at motor power supply terminal. If you have 12V motors then your motor supply voltage should be 14V.

Output Pins

The L298N motor driver’s output channels for the motor A and B are broken out to the edge of the module with two 3.5mm-pitch screw terminals.

You can connect two DC motors having voltages between 5 to 35V to these terminals.

Each channel on the module can deliver up to 2A to the DC motor. However, the amount of current supplied to the motor depends on system’s power supply.

Control Pins

For each of the L298N’s channels, there are two types of control pins which allow us to control speed and spinning direction of the DC motors at the same time viz. Direction control pins & Speed control pins.

Direction Control Pins

Using the direction control pins, we can control whether the motor spins forward or backward. These pins actually control the switches of the H-Bridge circuit inside L298N IC.

The module has two direction control pins for each channel. The IN1 and IN2 pins control the spinning direction of the motor A while IN3 and IN4 control motor B.

The spinning direction of a motor can be controlled by applying either a logic HIGH(5 Volts) or logic LOW(Ground) to these inputs. The below chart illustrates how this is done.

Controlling motor’s spinning direction with control inputs
Input1Input2Spinning Direction
Low(0)Low(0)Motor OFF
High(1)Low(0)Forward
Low(0)High(1)Backward
High(1)High(1)Motor OFF

Speed Control Pins

The speed control pins viz. ENA and ENB are used to turn the motors ON, OFF and control its speed.

Pulling these pins HIGH will make the motors spin, pulling it LOW will make them stop. But, with Pulse Width Modulation (PWM), we can actually control the speed of the motors.

The module usually comes with a jumper on these pins. When this jumper is in place, the motor is enabled and spins at maximum speed. If you want to control the speed of motors programmatically, you need to remove the jumpers and connect them to PWM-enabled pins on Arduino.

L298N Motor Driver Module Pinout

Before diving into hookup and example code, let’s first take a look at its Pinout.

VCC pin supplies power for the motor. It can be anywhere between 5 to 35V. Remember, if the 5V-EN jumper is in place, you need to supply 2 extra volts than motor’s actual voltage requirement, in order to get maximum speed out of your motor.

GND is a common ground pin.

5V pin supplies power for the switching logic circuitry inside L298N IC. If the 5V-EN jumper is in place, this pin acts as an output and can be used to power up your Arduino. If the 5V-EN jumper is removed, you need to connect it to the 5V pin on Arduino.

ENA pins are used to control speed of Motor A. Pulling this pin HIGH(Keeping the jumper in place) will make the Motor A spin, pulling it LOW will make the motor stop. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor A.

IN1 & IN2 pins are used to control spinning direction of Motor A. When one of them is HIGH and other is LOW, the Motor A will spin. If both the inputs are either HIGH or LOW the Motor A will stop.

IN3 & IN4 pins are used to control spinning direction of Motor B. When one of them is HIGH and other is LOW, the Motor B will spin. If both the inputs are either HIGH or LOW the Motor B will stop.

ENB pins are used to control speed of Motor B. Pulling this pin HIGH(Keeping the jumper in place) will make the Motor B spin, pulling it LOW will make the motor stop. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor B.

OUT1 & OUT2 pins are connected to Motor A.

OUT3 & OUT4 pins are connected to Motor B.

Wiring L298N motor driver module with Arduino UNO

Now that we know everything about the module, we can begin hooking it up to our Arduino!

Start by connecting power supply to the motors. In our experiment we are using DC Gearbox Motors(also known as ‘TT’ motors) that are usually found in two-wheel-drive robots. They are rated for 3 to 12V. So, we will connect external 12V power supply to the VCC terminal. Considering internal voltage drop of L298N IC, the motors will receive 10V and will spin at slightly lower RPM. But, that’s OK.

Next, we need to supply 5 Volts for the L298N’s logic circuitry. We will make use of the on-board 5V regulator and derive the 5 volts from the motor power supply so, keep the 5V-EN jumper in place.

Now, the input and enable pins(ENA, IN1, IN2, IN3, IN4 and ENB) of the L298N module are connected to six Arduino digital output pins(9, 8, 7, 5, 4 and 3). Note that the Arduino output pins 9 and 3 are both PWM-enabled.

Finally, connect one motor to terminal A(OUT1 & OUT2) and the other motor to terminal B(OUT3 & OUT4). You can interchange your motor’s connections, technically, there is no right or wrong way.

L298 Motor Controller Datasheet

When you’re done you should have something that looks similar to the illustration shown below.

Arduino Code – Controlling a DC Motor

The following sketch will give you complete understanding on how to control speed and spinning direction of a DC motor with L298N motor driver and can serve as the basis for more practical experiments and projects.

Code Explanation:

L298 Motor Driver Wiki

The arduino code is pretty straightforward. It doesn’t require any libraries to get it working. The sketch starts with declaring Arduino pins to which L298N’s control pins are connected.

In setup section of code, all the motor control pins are declared as digital OUTPUT and pulled LOW to turn both the motors OFF.

In loop section of the code we call two user defined functions at an interval of a second. These functions are:

L298 Motor Driver Datasheet

  • directionControl() – This function spins both motors forward at maximum speed for two seconds. It then reverses the motor’s spinning direction and spins for another two seconds. Finally it turns the motors off.
  • speedControl() – This function accelerates both the motors from zero to maximum speed by producing PWM signals using analogWrite() function, then it decelerates them back to zero. Finally it turns the motors off.