Arduino Projects

Arduino L298n Motor Driver control Tutorial, Speed & Direction, PWM

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

  1. Robot parts assembling
  2. L298N motor driver Pinout and explanation.
  3. L298N Interfacing with Arduino
  4. Programming and finally number
  5. 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:

12v Adaptor:

Arduino Uno

Arduino Nano

Mega 2560:

Robot Car chassis kit:

L298N motor driver:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

DISCLAIMER:

Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!



Robot Parts Assembling:

L298N

First of all, I started by fixing the motors.

L298N

L298N

After fixing both the Motors, then I installed both the wheels.

L298N

After I was done with the Motors and wheels, then I Started fixing the front wheel.

L298N

Then I installed the Battery Holder.

L298N

Finally with the help of two long Bolts I made a base for the L298N motor driver.

L298N

L298N

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:

L298N

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.

L298N

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.


L298N

While the terminal block3 is labeled with 12v, ground and +5v.

L298N

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


L298N

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.

L298N

L298N

Connect the red wire of the left motor with out1.


L298N

Now connect the black wire of the left motor with out2.

L298N

Connect the red wire of the right motor with out3 and connect the black wire of the right motor with out4.

L298N

These are the two wires coming from the battery holder.

L298N

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.


L298N

L298N

Now connect the +5v terminal of the motor driver with the Arduino’s 5v.

L298N

L298N

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.

L298N

L298N

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.L298N

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.

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.

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.

 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.

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);


   


}

Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

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

Back to top button