The L298N motor driver is what you’ll likely need to start with even though you’ll eventually need to learn how to operate DC motors in order to create your own robot. It has the ability to regulate the two DC motors’ rotational speed and direction.
Additionally, it has the ability to manage bipolar stepper motors like the NEMA 17. Check out this guide to learn more if you want to.
Controlling a DC Motor
A DC motor can only be completely under our control if we have complete control over its speed and direction of rotation. Combining these two methods makes it possible to do this.
- PWM- Control Speed
- H-Bridge- Control the Spinning direction
PWM – to control the speed
A DC motor’s speed can be altered by altering the input voltage. Pulse width modulation is an approach that is frequently used to achieve this (PWM).
PWM is a method that involves transmitting a series of ON-OFF pulses to change the average value of the input voltage. The Duty Cycle, often known as the average voltage, is a function of the pulse width.
The average voltage applied to the DC motor increases with increasing the duty cycle, increasing motor speed. The average voltage provided to the DC motor decreases as the duty cycle gets shorter, which slows down the motor speed.
The PWM approach is displayed in the graphic below with varied duty cycles and average voltages.
H-Bridge – to control the spinning direction
By altering the polarity of a DC motor’s input voltage, the direction in which it spins can be changed. Using an H-bridge is a common method for achieving this.
Four switches are stacked in an H shape, with the motor in the middle, to form an H-bridge circuit.
Reversing the polarity of the electricity provided to the motor requires simultaneously closing two specified switches. As a result, the motor’s direction of rotation changes.
The H-bridge circuit’s operation is demonstrated in the following animation.
L298N Motor Driver Chip
The L298N, a large, black chip with a substantial heat sink, sits in the heart of the module.
The L298N chip is perfect for creating a two-wheeled robotic platform since it has two common H-bridges that can drive a pair of DC motors.
The L298N motor driver works well with the majority of our DC motors because it can handle 2A of continuous current per channel and a supply range of 5V to 35V.
Technical Specifications
Motor output voltage | 5V – 35V |
Motor output voltage (Recommended) | 7V – 12V |
Logic input voltage | 5V – 7V |
Continuous current per channel | 2A |
Max Power Dissipation | 25W |
L298N Motor Driver Module Pinout
There are 11 pins on the L298N module, which enable external communication. Following is the pinout:
Power Pins
A 3-pin, 3.5mm-pitch screw terminal provides power to the L298N motor driver module.
VCC | VS and VSS are the L298N motor driver’s two input power pins. |
GND | is the common ground pin. |
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
Two 3.5mm-pitch screw terminals are used to break out the L298N motor driver’s output channels, designated OUT1 and OUT2 for motor A and OUT3 and OUT4 for motor B, to the module’s edge. Two 5-12V DC motors can be connected to these connections.
The module’s channels each have a 2A maximum output for the DC motor. However, the capacity of the motor power supply determines how much current is delivered to the motor.
Direction Control Pins
The motor’s direction of rotation can be changed using the direction control pins. These pins of the L298N chip really regulate the switches of the H-Bridge circuit.
There are two direction control pins on the module. Motor A’s spinning direction is controlled by the IN1 and IN2 pins, whereas motor B’s spinning direction is controlled by the IN3 and IN4 pins.
Applying logic HIGH (5V) or logic LOW (Ground) to these inputs will change the direction in which the motor spins. The results of various combinations are displayed in the chart below.
Input 1 | Input 2 | Spinning 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 motors are turned on and off, and their speed is controlled, using the speed control pins ENA and ENB
The motors can be started or stopped by pulling these pins HIGH or LOW, respectively. However, the motors’ speed can be managed by pulse width modulation (PWM).
On these pins, the module normally has a jumper. The motor runs at full speed when this jumper is installed. Remove the jumpers and connect the motors to the Arduino’s PWM-enabled pins if you wish to regulate the motors’ speed through code.
On-board 5V Regulator and Jumper
A jumper controls the module’s 78M05 5V regulator, which can be enabled or disabled.
The logic power supply (VSS) is obtained from the motor power supply while this jumper is in place, enabling the 5V regulator (VS). In this instance, the output pin is the 5V input terminal, which provides 5V 0.5A. It can be used to power an Arduino or other 5V-powered circuits.
The 5V regulator is deactivated when the jumper is removed, thus we must supply 5V independently via the VSS pin.
Warning!
Keep the jumper in place if the motor power source is less than 12V. The jumper needs to be taken out if the voltage exceeds 12 volts in order to protect the onboard 5 volt regulator.
Additionally, avoid powering the VSS and VS pins simultaneously while the jumper is in place.
Voltage Drop of L298N
The voltage drop of the L298N is about 2V. This is because an H-Bridge requires the current to flow through two transistors, which results in a total voltage drop of 2V. Internal switching transistors have a voltage drop of about 1V when forward-biased.
So, the motors will receive about 10V if you connect 12V to the motor power supply line. Therefore, a 12V DC motor will never rotate at its maximum speed.
The motor power supply should have a voltage that is a little bit greater (+2V) than the actual voltage requirement of the motor in order to get the motor to run at its maximum speed.
If you are using 5V motors, you must supply 7V at the motor power supply terminal to account for a voltage loss of 2V. The recommended motor supply voltage for 12V motors is 14V.
!! This excessive voltage drop causes a significant amount of power to be lost as heat. The L298N-based motor drivers need a large heatsink because of this.
Wiring an L298N Motor Driver Module to an Arduino
We can begin connecting the module to our Arduino now that we are fully informed about it.
Connecting the motor power supply will be the first step. We’re employing “TT” motors, sometimes known as DC gearbox motors, in our experiment because they’re frequently used in two-wheel-drive robots. They have a 3 to 12V rating. So, we’ll join a 12V external power supply to the VS terminal. The motors will receive 10V and spin at a somewhat lower RPM because L298N has a voltage drop of roughly 2V. But it’s all right.
Next, we must supply 5V to the L298N’s logic circuits. Keep the 5V-EN jumper in place because we’ll be using the on-board 5V regulator to take 5V from the motor power source.
Connect the six Arduino digital output pins to the ENA, IN1, IN2, IN3, IN4 and ENB input and enable pins of the L298N module (9, 8, 7, 5, 4 and 3). It should be noted that the Arduino output pins 9 and 3 support PWM.
Last but not least, connect one motor to terminal A (OUT1 and OUT2) and the other to terminal B. (OUT3 and OUT4). Your motor’s connections can be changed. Technically, there is no right or wrong way.
Arduino Example Code
The following design can be used as the starting point for more realistic experiments and projects because it demonstrates how to regulate the speed and spinning direction of a DC motor using the L298N Motor Driver.
The motor is depicted rotating once in one direction, then once in the other. Additionally, there is some acceleration and deceleration.
! The motor may hum when accelerating or decelerating, especially at lower PWM settings. There is nothing to worry about because this is usual. This occurs because the DC motor needs a specific voltage to function.
// Motor A connections int enA = 9; int in1 = 8; int in2 = 7; // Motor B connections int enB = 3; int in3 = 5; int in4 = 4; 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); // Turn off motors - Initial state digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); } void loop() { directionControl(); delay(1000); speedControl(); delay(1000); } // This function lets you control spinning direction of motors void directionControl() { // Set motors to maximum speed // For PWM maximum possible values are 0 to 255 analogWrite(enA, 255); analogWrite(enB, 255); // Turn on motor A & B digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); delay(2000); // Now change motor directions digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); delay(2000); // Turn off motors digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); } // This function lets you control speed of the motors void speedControl() { // 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); }
Code Explanation:
The Arduino code is quite straightforward. It can operate without any libraries. The L298N’s control pins are connected to the Arduino pins that are first declared in the sketch.
// Motor A connections int enA = 9; int in1 = 8; int in2 = 7; // Motor B connections int enB = 3; int in3 = 5; int in4 = 4;
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); // Turn off motors - Initial state digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); }
We delay the calls to two user-defined functions by one second in the code’s loop section.
void loop() { directionControl(); delay(1000); speedControl(); delay(1000); }
These Functions include-
- directionControl() – Both motors spin at full speed for two seconds as a result of this function. The motors then rotate in the opposite direction for the next two seconds. It eventually shuts off the motors.
void directionControl() { // Set motors to maximum speed // For PWM maximum possible values are 0 to 255 analogWrite(enA, 255); analogWrite(enB, 255); // Turn on motor A & B digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); delay(2000); // Now change motor directions digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); delay(2000); // Turn off motors digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); }
- speedControl() – This function creates a PWM signal using the analogWrite() function that accelerates both motors from zero to their maximum speed before slowing them down to zero. It eventually shuts off the motors.
void speedControl() { // 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); }