Home / Tutorials / Arduino Tutorial / How to Use L298N Motor Driver
L298N Motor Controller Tutorial

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. A simplified H-bridge is shown below:

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 Motor Controller Board

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

The controller board also 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.

As shown in the diagram, you have Motor A inputs and Motor B inputs. These are connected to the microcontroller. The motor terminals are connected to Motor Terminals 1, 2, 3, 4 where Motor A is connected to terminals 1 and 2 while Motor B is connected 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 are connected 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!

Check Also

arduino rgb led tutorial

Arduino RGB LED Tutorial

A RGB LED is a special light emitting diode which can produce 16 million possible …

Leave a Reply

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