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

(Last Updated On: July 10, 2019)

L298N

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

For the complete step by step explanation you can watch the video given at the end of this Article.


The components and tools used in this project can be purchased from Amazon, the components Purchase links are given below:

Arduino Uno: https://amzn.to/2tVz1vu
Mega 2560: https://amzn.to/2ze1kdu
Robot Car chassis kit: https://amzn.to/2tTpkgo
L298N dual H-Bridge motor driver: https://amzn.to/2XF4sam
LM7805 Voltage Regulator: https://amzn.to/2IZEl5x
Potentiometer: https://amzn.to/2JSLbfg
330 ohm resistors pack: https://amzn.to/2NtlAeh
5v 2A adaptor: https://amzn.to/2GsNfcw
2n2222 npn transistors: https://amzn.to/2EmXsIb
10k Resistor: https://amzn.to/2KTd6OW
lm7805 Voltage Regulator: https://amzn.to/2IZEl5x
330-ohm resistors pack: https://amzn.to/2NtlAeh
female DC power jack socket: https://amzn.to/2KEo1gt
470 uf capacitors: https://amzn.to/2xDCOzf
5×7 cm vero board: https://amzn.to/2OHGbvn
female headers: https://amzn.to/2zqmtiJ
connection wires: https://amzn.to/2DpBuW7
Super Starter kit for Beginners: https://amzn.to/2KJvmKG
Jumper Wires: https://amzn.to/2KMoVXs
Bread Board: https://amzn.to/2MS4q8X
12v Adaptor: https://amzn.to/2Ntr6h1
PCB plate: https://amzn.to/2IUwpCt
Variable Supply: https://amzn.to/2MT4Qfj
Digital Multimeter: https://amzn.to/2Nvft9i
Vero Board / strip board: https://amzn.to/2MTf9jD
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2zfoNuJ
Solder wire: https://amzn.to/2ufUMWf
Wire Stripper: https://amzn.to/2KOqxfU
wire cutter: https://amzn.to/2ucIq14
PCB small portable drill machine: https://amzn.to/2Nu62XF


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.


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.

The In1, In2, In3 and In4 Pins of the L298N Motor driver are connected with Pins 8, 9, 10, and 11 of the Arduino.

255 means that the motors are set at their maximum speed.


L298N Speed control Programming:

All the connections remain the same.

Watch Video Tutorial:

Recommended For You

About the Author: 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