Arduino L298n Motor Driver control Tutorial, Speed & Direction, PWM
Table of Contents
L298N Motor Driver with Arduino Description:
This is a getting started tutorial on how to use the L298N motor driver and control the forward, left, right, and reverse movement. in this tutorial, you will also learn how to use the pulse width modulation to control the speed of a dc motor. For the best understanding, I will explain two Arduino programs, the first program will explain only the basics like for example, how to control the direction of a robot using L298N motor driver. While in the 2nd program I will also add the PWM to control the Speed of DC Motors. This Tutorial Covers
- Robot parts assembling
- L298N motor driver Pinout and explanation.
- L298N Interfacing with Arduino
- Programming and finally number
- Testing
I have also used the same L298N motor driver for controlling the stepper motor.
For the complete step-by-step explanation, you can watch the video given at the end of this Article.
Amazon Links:
Arduino Nano USB-C Type (Recommended)
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
Robot Parts Assembling:
First of all, I started by fixing the motors.
After fixing both the Motors, then I installed both the wheels.
After I was done with the Motors and wheels, then I Started fixing the front wheel.
Then I installed the Battery Holder.
Finally with the help of two long Bolts I made a base for the L298N motor driver.
With this my Robot Parts assembling completed. Now before I explain anything else first let’s have a look at the L298N motor driver and it’s Pinout.
L298N Motor Driver:
This is the L298N dual H-bridge Motor driver. This motor driver can be used to control Dc motors that have voltages between 5 and 35volts, with a peak current of up to 2amps. As this is a dual H-Bridge motor driver, it can be used to control the speed and direction of two DC motors at the same time.
Now let’s take a closer look at the Pinout of L298N module.
This module has three terminal blocks. terminal block1 will be used for motor A and is clearly labeled with out1 and out2, this is where we connect the two wires of the dc motor. Terminal block2 will be used for motor B and is clearly labeled with out3 and out4.
While the terminal block3 is labeled with 12v, ground and +5v.
The 12v terminal is used to supply the voltage to the dc motors, this voltage can be from 5 to 35volts. The ground terminal is connected with the ground of the external power supply and is also connected with the ground of the controller board, which in my case is Arduino board which
As you can see this motor driver also have some male headers which are clearly labeled with ENA…IN1…IN2…IN3…IN4 and ENB. The ENA and ENB are used to enable both the motors. Jumper caps mean that both the motors are enabled by default and the motors will rotate at maximum speed. If the jumper caps are removed and the ENA and ENB pins are connected with the PWM pins of the Arduino, the motors speed can be controlled using the pulse width modulation which I will explain in the programming. Then IN1 and IN2 pins are used for controlling the direction of motor A while the IN3 and IN4 are used to control the direction of motor B. now let’s start the interfacing.
L298N Motor Driver Interfacing With Arduino:
First of all fix the motor driver and Arduino.
Connect the red wire of the left motor with out1.
Now connect the black wire of the left motor with out2.
Connect the red wire of the right motor with out3 and connect the black wire of the right motor with out4.
These are the two wires coming from the battery holder.
Connect the red wire with the +12v terminal and connect the black wire with the ground terminal and also connect the ground terminal with the Arduino’s ground.
Now connect the +5v terminal of the motor driver with the Arduino’s 5v.
Now remove the jumper caps and connect two jumper wires with the ENA and ENB pins. Connect ENA with pin number 5 of the Arduino which is the PWM pin and connect the ENB pin with pin number 6 which is also a PWM pin.
now connect jumper wires with IN1, IN2, IN3 and IN4 AND connect IN1 with pin number8, connect IN2 with pin number 9, connect IN3 with pin number 10, and connect IN4 with pin number 11.
So we are done with the interfacing and now let’s control the motors forward, left, right and reverse movement and also control the speed of the dc motors.
Note: if you have missed any connection and face any Problem, you can watch video given at the end of this Article. In the video each and every connection is clearly explained.
L298N Arduino Program Number1:
The purpose of this program is to explain how to control the forward, left, right and reverse movement of the motors using L298N motor driver. While in the next program I have explained the speed controlling.
First of all, I started off by defining all the Pins of the L298N motor driver. As explained during the interfacing, the ena and enb Pins of the L298N motor driver are connected with the Arduino’s Pin Number 5 and Pin number 6. Pin number 5 and Pin number 6 both are the PWM Pins of the Arduino Uno.
1 2 |
int ena = 5; int enb = 6; |
The In1, In2, In3 and In4 Pins of the L298N Motor driver are connected with Pins 8, 9, 10, and 11 of the Arduino.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int in1 = 8; int in2 = 9; int in3 = 10; int in4 = 11; void setup() { Serial.begin(9600); pinMode(ena, OUTPUT); pinMode(enb, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); |
255 means that the motors are set at their maximum speed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
analogWrite(ena, 255); analogWrite(enb, 255); delay(1000); } void loop() { digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); delay(1000); // forward digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); delay(2000); digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); delay(1000); // LEFT digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); delay(1000); digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); delay(1000); //RIGHT // forward digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); delay(1000); digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); delay(1000); //REVERSE digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); delay(1000); } |
L298N Speed Control Programming:
All the connections remain the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
int ena = 5; int enb = 6; int in1 = 8; int in2 = 9; int in3 = 10; int in4 = 11; int sc = A1; //speed controlling variable resistor int mspeed = 0; // motor speed, the variable resistor value will be stored in this variable void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(ena, OUTPUT); pinMode(enb, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); pinMode(sc, INPUT); analogWrite(ena, 0); analogWrite(enb, 0); } void loop() { mspeed = analogRead(sc); mspeed = map(mspeed, 0, 1023, 0 , 255); analogWrite(ena, mspeed); analogWrite(enb, mspeed); // forward digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); } |