Microcontroller Tutorials

Arduino, Raspberry Pi, PIC, Beaglebone Resources

How to Use L298N Motor Driver

You can drive a LED on or off using a microcontroller like Arduino or PIC. There’s no problem there because the 20 mA or so current from the pins is enough. Obviously, you can’t drive loads that require much more current like a motor. This is where the L298N motor controller comes in.

Introduction

The L298N motor controller follows the H-bridge configuration, which is handy when controlling the direction of rotation of a DC motor. An H-bridge schematic looks like this:

H-bridge

Here, the motor rotates in the direction dictated by the switches. When S1 and S4 are on, the left motor terminal is more positive than the right terminal, and the motor rotates in a certain direction. On the other hand, when S2 and S3 are on, the right motor terminal is more positive than the left motor terminal, making the motor rotate in the other direction.

H-bridge left and right

The other benefit of using an H-bridge is that you can provide a separate power supply to the motors. This is very significant especially when using an Arduino board where the 5V power source is simply not enough for two DC motors.

The L298N IC

The L298N is an integrated circuit that follows the H-bridge concept. It comes in two IC packages: MultiWatt15 and PowerSO20. Here are the pinouts for each package:

 

This IC drives two motors through two channels, A and B. For example, if a motor is using channel A, its terminals must be connected to pins Out 1 and Out 2. The Enable A pin must be high to turn on the motor. To drive a motor to a direction, say, clockwise, the pin Input 1 must be high while the pin Input 2 must be low. To drive the motor counter clockwise, the pin Input 1 is low while the pin Input 2 is high.

The diagram above shows an example diagram for using the L298N to drive one DC motor.

Here are the IC's ratings:

The most notable feature here is its high power supply although its input pins follow lower voltage levels. This means you can power high voltage motors while controlling them with microcontrollers.

More information about the L298N IC is found on its datasheet:

For this tutorial, we will be focusing more on the L298N breakout board.

The L298N Motor Controller Board

As already mentioned, the L298N has four inputs corresponding to the four switches in the H-bridge diagram above. All you need to do is apply signals to the inputs to make the motor(s) rotate to a certain direction.

L298N motor controller board

Using a breakout board is easier for prototyping compared to using the IC.

The controller board, shown above, has +12V and +5V terminals. The +12V pin is where the motor power is attached. This pin can accept voltages from +7VDC to +35VDC.

Important note: remove the +12V jumper shown if you are using powers higher than +12V.

When the +12V jumper is attached, the on-board voltage regulator is now enabled, and you can source +5V from the +5V terminal.

You have Motor A inputs and Motor B inputs. These connect to the microcontroller. The motor terminals connect to Motor Terminals 1, 2, 3, 4. Specifically, motor A connects to terminals 1 and 2 while Motor B connects to terminals 3 and 4.

There are also two other jumpers on the board, as shown. Remove these jumpers if you are using DC motors and keep it for stepper motors. Speed control for Motor A and Motor B is achieved via PWM on these pins. More on that later.

Connecting a DC Motor to the L298N Board

Here is a wiring diagram for connecting two DC motors to the L298N driver board.

L298N board with motors attached

Using the L298N if straightforward. If you want the left motor to rotate in one direction, apply a high pulse to IN1 and a low pulse to IN2. To reverse the direction, reverse the pulses to IN1 and IN2. The same applies to the right motor.

Here’s a table that summarizes the pins and corresponding motor direction. These assumes you are following the same Fritzing diagram above.

IN1 IN2 IN3 IN4 Direction
0 0 0 0 Stop
1 0 1 0 Forward
0 1 0 1 Reverse
1 0 0 1 Left
0 1 1 0 Right

Speed Control?

Speed control is also possible with the L298N motor driver. All you need is feed PWM signals to the motor enable pins. The speed of the motor will vary according to the width of the pulses. The wider the pulses, the faster the motor rotates. How fast the motor rotates for a given pulse width will vary from motor to motor even if they look exactly the same. Thus, the actual pulse width must be derived through experiment.

Using the L298N with Arduino

An example diagram for connecting the L298N motor controller board to an Arduino is shown:

L298N board with Arduino

You can attach the control pins to any digital (or even analog) pins. However, for motor speed control, the motor enable pins must be attached to a PWM enabled pin. Here you see that the motor enable pins connect to pin 10 and pin 5, both of which are PWM pins.

Here’s an example Arduino sketch utilizing the diagram above:

You can modify this sketch to include a function for going backwards, turning left and turning right. Just follow the table above.

Using the L298N with PIC 

You can also use a PIC microcontroller with the L298N motor controller board. Here is an example diagram using the PIC16F877A:

L298N Board example with PIC

And here’s a XC8 code that basically does the same as the Arduino sketch above:

You might need to read my PWM tutorial for PICs to understand the code above.

I hope you find this tutorial helpful!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.