Interface L298N DC Motor Driver Module With Arduino
Interface L298N DC Motor Driver Module With Arduino
Controlling a DC Motor
We can only have full control over a DC motor if we can control its speed
and spinning direction. This is possible by combining these two techniques.
The higher the duty cycle, the higher the average voltage applied to the DC
motor, resulting in an increase in motor speed. The shorter the duty cycle,
the lower the average voltage applied to the DC motor, resulting in a
decrease in motor speed.
The image below shows PWM technique with various duty cycles and
average voltages.
Pulse Width Modulation (PWM) Technique
Closing two specific switches at the same time reverses the polarity of the
voltage applied to the motor. This causes a change in the spinning direction
of the motor.
The L298N motor driver has a supply range of 5V to 35V and is capable of
2A continuous current per channel, so it works very well with most of our
DC motors.
Technical Specifications
Power Pins
The L298N motor driver module receives power from a 3-pin, 3.5mm-pitch
screw terminal.
The L298N motor driver has two input power pins: VS and VSS.
VS pin powers the IC’s internal H-Bridge, which drives the motors. This pin
accepts input voltages ranging from 5 to 12V.
VSS is used to power the logic circuitry within the L298N IC, and can range
between 5V and 7V.
Output Pins
The output channels of the L298N motor driver, OUT1 and OUT2 for motor A
and OUT3 and OUT4 for motor B, are broken out to the edge of the module
with two 3.5mm-pitch screw terminals. You can connect two 5-12V DC
motors to these terminals.
The direction control pins allow you to control whether the motor rotates
forward or backward. These pins actually control the switches of the H-
Bridge circuit within the L298N chip.
The module has two direction control pins. The IN1 and IN2 pins control the
spinning direction of motor A; While IN3 and IN4 control the spinning
direction of motor B.
The speed control pins ENA and ENB are used to turn on/off the motors and
control their speed.
Pulling these pins HIGH will cause the motors to spin, while pulling them
LOW will stop them. However, with Pulse Width Modulation (PWM), the
speed of the motors can be controlled.
The module usually comes with a jumper on these pins. When this jumper
is in place, the motor spins at full speed. If you want to control the speed of
the motors programmatically, remove the jumpers and connect them to the
Arduino’s PWM-enabled pins.
Warning:
If the motor power supply is less than 12V, you can keep the jumper in
place. If it is greater than 12V, the jumper must be removed to prevent
damage to the onboard 5V regulator.
Also, do not supply power to both the VSS and VS pins while the jumper is
in place.
So, if you connect 12V to the motor power supply terminal, the motors will
receive approximately 10V. This means that a 12V DC motor will never spin
at full speed.
In order to get the motor to run at its maximum speed, the motor power
supply should have a voltage that is slightly higher (+2V) than the actual
voltage requirement of the motor.
Taking into account a voltage drop of 2V, if you are using 5V motors, you
will need to provide 7V at the motor power supply terminal. If you have 12V
motors then your motor supply voltage should be 14V.
This excess voltage drop results in significant power dissipation in the form
of heat. This is why the L298N based motor drivers require a big heatsink.
Next, we need to supply 5V to the logic circuitry of the L298N. We’ll use the
on-board 5V regulator to draw 5V from the motor power supply, so keep
the 5V-EN jumper in place.
Now connect the L298N module’s Input and Enable pins (ENA, IN1, IN2,
IN3, IN4 and ENB) to the six Arduino digital output pins (9, 8, 7, 5, 4 and 3).
Note that both Arduino output pins 9 and 3 are PWM-enabled.
Finally, wire one motor to terminal A (OUT1 and OUT2) and the other to
terminal B (OUT3 and OUT4). You can swap out your motor’s connections.
There is technically no right or wrong way.
Arduino Example Code
The sketch below will show you how to control the speed and spinning
direction of a DC motor using the L298N Motor Driver and can serve as the
basis for more practical experiments and projects.
The sketch moves the motor in one direction for one revolution, then in the
opposite direction. There is also some acceleration and deceleration
involved.
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 loop() {
directionControl();
delay(1000);
speedControl();
delay(1000);
}
Code Explanation:
The Arduino code is fairly simple. It does not require any libraries to work.
The sketch starts by declaring the Arduino pins that are connected to the
L298N’s control pins.
// Motor A connections
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B connections
int enB = 3;
int in3 = 5;
int in4 = 4;
In the setup section of the code, all of the motor control pins, including the
direction and speed control pins, are configured as digital OUTPUT. And the
direction control pins are pulled LOW to initially disable both motors.
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);
In the loop section of the code, we call two user-defined functions with a
one-second delay.
void loop() {
directionControl();
delay(1000);
speedControl();
delay(1000);
}