In this post we are going to learn about L298N dual H-bridge DC motor driver module which can be used to drive brushed DC motors and stepper motors with microcontrollers and ICs.
Overview
Modular circuit boards are best time savior for electronics designers which also reduce the prototyping errors. This is mostly preferred by programmers who write code for microcontrollers spend their majority of the time by typing codes in front of the computer and have less time for solder the discrete electronic components.
Find many great new & used options and get the best deals for Dual H Bridge L298n DC Stepper Motor Driver Module Controller Board for Arduino at the best online prices at. See the wiring diagram below. Connect the A+,A-, B+ and B- wires from the stepper motor to the module connection OUT 1, OUT 2, OUT3, and OUT4 or A1, A2, B3, and B4. Place the jumper included with the L298n Module over the pin. As you can see the diagram below, Red, Green, Yellow, and Blue are connected into the l298n board pin. Hardware Details.
That’s why we can find tons and tons of different modular circuits are made just for Arduino boards, it is easy to interface and have advantage of least hardware errors while designing our prototype.
Illustration of L298N module:
The module is built around IC L298N; it is commonly available at E-commerce websites.
We use DC motor drivers because the ICs and microcontrollers are not capable of delivering current not more than 100 milliamps in general. The microcontrollers are smart but not strong; this module will add some muscles to Arduino, ICs and other microcontrollers to drive high power DC motors.
It can control 2 DC motors simultaneously up to 2 amps each or one stepper motor. We can control the speed using PWM and also its rotational direction of the motors.
This module is ideal for building robots and land moving projects such as toy cars.
Let’s see the technical details of L298N module.
Pin description:
· On the left hand side there are OUT1 and OUT2 port, which is for connecting DC motor. Similarly, OUT3 and OUT4 for another DC motor.
· ENA and ENB are enable pins, by connecting ENA to high or +5V; it enables the port OUT1 and OUT2. If you connect the ENA pin to low or ground, it disables the OUT1 and OUT2. Similarly, for ENB and OUT3 and OUT4.
· IN1 to IN4 are the input pins which will be connected to Arduino. If you input IN1 +Ve and IN2 –Ve from microcontroller or manually, the OUT1 turns high and OUT2 turns low, thus we can drive motor.
· If you input IN3 high, OUT4 turns high and if you input IN4 low OUT3 turns low, now we can drive another motor.
· If you want to reverse the rotational direction of the motor just reverse IN1 and IN2 polarity, similarly for IN3 and IN4.
· By applying PWM signal to ENA and ENB you can control the speed of the motors on two different output ports.
· The board can accept from 7 to 12V nominally. You can input power at +12V terminal and ground to 0V.
· The +5V terminal is OUTPUT which can be used to power Arduino or any other module if needed.
Jumpers:
There are three jumper pins; you can scroll up see the illustrated image.
All the jumpers will be connected initially; remove or keep the jumper depending on your need.
Jumper 1 (see illustrated image):
· If you’re motor need more than 12V supply you have to disconnect the jumper 1 and apply desired voltage (maximum 35V) at 12v terminal. Bring another 5V supply and input at +5V terminal. Yes, you have to input 5V if you need to apply more than 12V (when jumper 1 is removed).
· The 5V input is for proper functioning of the IC, since removing the jumper will disable the in-built 5v regulator and protect from higher input voltage from 12v terminal.
· The +5V terminal acts as output if your supply is between 7 to 12V and acts as input if you apply more than 12V and jumper is removed.
· Most of the projects just need motor voltage below 12V so, keep the jumper as it is and use +5V terminal as output.
Jumper 2 and Jumper 3 (see illustrated image):
· If you remove these two jumpers you have to input the enable and disable signal from the microcontroller, most of the users prefer removing the two jumpers and applying the signal from microcontroller.
· If you keep the two jumpers the OUT1 to OUT4 will be always enabled. Remember ENA jumper for OUT1 and OUT2. ENB jumper for OUT3 and OUT4.
Now let’s see a practical circuit, how can we interface motors, Arduino and supply to the driver module.
Schematic:
The above circuit can be used for toy cars, if you change the code appropriately and add a joystick.
You just need to power the L289N module and the module will power the Arduino via Vin terminal.
The above circuit will rotate the both motors clock-wise for 3 second and stop for 3 second. After that the motor will rotate anti-clockwise for 3 seconds and stop for 3 seconds. This demonstrates the H-bridge in action.
After that both the motor will start rotating slowly in anti-clockwise gaining speed gradually to maximum and gradually reduce the speed to zero. This demonstrates speed control of motors by PWM.
Program:
//----------------Program developed by R.GIRISH--------------//
const int Enable_A = 9;
const int Enable_B = 10;
const int inputA1 = 2;
const int inputA2 = 3;
const int inputB1 = 4;
const int inputB2 = 5;
void setup()
{
pinMode(Enable_A, OUTPUT);
pinMode(Enable_B, OUTPUT);
pinMode(inputA1, OUTPUT);
pinMode(inputA2, OUTPUT);
pinMode(inputB1, OUTPUT);
pinMode(inputB2, OUTPUT);
}
void loop()
{
//----Enable output A and B------//
digitalWrite(Enable_A, HIGH);
digitalWrite(Enable_B, HIGH);
//----------Run motors-----------//
digitalWrite(inputA1, HIGH);
digitalWrite(inputA2, LOW);
digitalWrite(inputB1 , HIGH);
digitalWrite(inputB2, LOW);
delay(3000);
//-------Disable Motors----------//
digitalWrite(Enable_A, LOW);
digitalWrite(Enable_B, LOW);
delay(3000);
//-------Reverse Motors----------//
digitalWrite(Enable_A, HIGH);
digitalWrite(Enable_B, HIGH);
digitalWrite(inputA1, LOW);
digitalWrite(inputA2, HIGH);
digitalWrite(inputB1 , LOW);
digitalWrite(inputB2, HIGH);
delay(3000);
//-------Disable Motors----------//
digitalWrite(Enable_A, LOW);
digitalWrite(Enable_B, LOW);
delay(3000);
//----------Speed rise----------//
for(int i = 0; i < 256; i++)
{
analogWrite(Enable_A, i);
analogWrite(Enable_B, i);
delay(40);
}
//----------Speed fall----------//
for(int j = 256; j > 0; j--)
{
analogWrite(Enable_A, j);
analogWrite(Enable_B, j);
delay(40);
}
//-------Disable Motors----------//
digitalWrite(Enable_A, LOW);
digitalWrite(Enable_B, LOW);
delay(3000);
}
//----------------Program developed by R.GIRISH--------------//
Author’s prototype:
If you have any questions regarding this L298N DC motor driver project, feel free to express in the comment section, you may receive a quick reply.
Let us continue exploring Raspberry Pi and its features by Interfacing L298N Motor Driver with Raspberry Pi and control a DC Motor with the help of a Python Script. In this project, we will learn about L298N Motor Driver and how the Raspberry Pi L298N Motor Driver Module Interface works.
Overview
Interfacing L298N Motor Driver Module with Raspberry Pi will allow us to control a DC Motor (in fact, you can control two DC Motors).
When I say control a DC Motor, I mean you can start a motor, stop it, make it rotate in forward direction, backward directions, increase the speed of rotation and also decrease the speed.
For this, I’ll be using the L298N Motor Driver Module. If you remember, I have already made a project on CONTROLLING A DC MOTOR WITH RASPBERRY PI using L293D Motor Driver.
How different will it be for controlling a DC Motor using Raspberry Pi with L293D and L298N? Well, there won’t be much of a difference as essentially both these modules serve the same purpose.
But it is always nice to learn about something new and implement it into a project. So, first, let me take you through a simple introduction to L298N Motor Driver Module.
L298N Motor Driver Module
In recent times, L298N Motor Driver Module has become the favorite choice of hobbyists and makers when it comes to DC Motor Control. It became so popular that the module is now available at a very low cost.
The L298N Motor Driver Module is based on the powerful L298N Motor Driver IC. The pin diagram of the L298N Motor Driver IC is shown in the image below. This IC, along with few other extra components make up the L298N Motor Driver Module.
The above image shows the L298N Motor Driver Module with names of all the components on it. To name a few, it contains screw terminals for connecting motors and power supply, male headers for connecting different pins like enable, IN1, IN2 etc.
NOTE:
- There is jumper near the power supply terminals of the L298N Motor Driver Module. If it is connected, it will enable the on-board 5V regulator, which supplies +5V logic supply for L298N IC.
- This must be used only if the supply voltage is less than or equal to +12V. If it is more than +12V, then disconnect the jumper to disable the regulator.
Circuit Diagram of Raspberry Pi L298N Interface
The following image shows the circuit diagram of the Raspberry Pi L298N Motor Driver Module Interface. The circuit diagram is made with the help of Fritzing App.
Components Required
- Raspberry Pi
- L298N Motor Driver Module
- 12V DC Motor
- 12V Power Supply for Motor and Motor Driver
- Power Supply for Raspberry Pi
- Connecting Wires
Circuit Design
The design of the Raspberry Pi L298N Motor Driver Interface Circuit is very simple. First connect 12V Power Supply to L298N Motor Driver Module. Then, make the GND terminals of Raspberry Pi and L298N Motor Driver Module common (connect them together).
Now, since we are controlling a single DC Motor, we need to use a single channel of the L298N. In order to do that, connect the ENA pin of L298N to Physical Pin 22 (GPIO25) of Raspberry Pi.
Coming to the Inputs of the Motor, connect the IN1 and IN2 of L298N Module to Physical Pins 16 and 18 (GPIO23 and GPIO24). That’s it. The rest of the job is done by the Python Script.
Code
The Python Script for Interfacing L298N Motor Driver Module with Raspberry Pi is given below. We will see the explanation of the code in the Working Section.
Working of the Project
If you take a look at the code carefully, you can easily understand the working of the project. After creating the Python Script with the above code, run the script.
You will get a message regarding the default speed and direction of the Motor. This is followed by a list of commands you must use to control the motor. These commands are given below.
- r – run (to run or start the motor)
- s – stop (to stop the motor)
- f – forward (to run the motor in forward direction) – default direction
- b – backward (to reverse the direction of rotation)
- l – low (to decrease the speed to 25%) – default speed
- m – medium (to run the motor at medium speed 50%)
- h – high (to increase the speed to 75% level)
- e – exit (to stop the motor and exit Python)
I think the commands are self-explanatory.
Applications
- The aim of the Raspberry Pi L298N Motor Driver Module Interface is just to understand the concept behind controlling a simple DC Motor with Raspberry Pi.
- By understanding this concept, you can implement several Motor related Projects with Raspberry Pi like:
- Raspberry Pi Robot
- Web Controlled Robot using Raspberry Pi
- IoT based Door Lock Monitoring