Interfacing L298N H-bridge motor driver with raspberry pi
First things first, if you have not checked out my previous blog about how to set up a raspberry pi with out an HDMI cable and monitor, then have a quick read this blog.
Now, let’s continue with our journey. At this point, you are probably wondering what journey, is there a destination? Well, there is. I will be explicitly talking about it another separate blog (yeah, I am a little lazy in writing blogs, sorry ;( ).
However, a little sneak peak into this project, this robot has motors (surprise surprise ;p) to power the wheels. To protect the controller from the current usage incompatibility, a motor driver is used. To know more about what are motor drivers and why are they used, please read this amazing blog.
What is a motor driver and why do we need it with the Raspberry Pi
The motors require an amount of current that is larger than the amount of current that the Raspberry Pi could handle…
In this current blog, we will interface the L298N H-bridge with raspeberry pi and run a script on the pi to move the robot. This will be divided into 2 sections.
- Hardware Integration
- Software Program and testing
- Hardware Integration
First, let’s talk about L298N.
We have 4 6V DC motors, and we have only two motor outputs, therefore, 2 motors will use the same motor output from the H-bridge. Hence, 2 motors connect to Motor A output, the other 2 motors connect to the Motor B output.
Before we connect the motor driver to the pi, let’s take care of the battery pack which would power the motors. The +ve of the batter pack gets connected to the power supply connection from the image and the -ve of the battery pack gets connected to the GND.
Once the battery pack is connected properly to the H-bridge, a red light will start glowing.
Now, let’s connect the H-bridge with raspberry pi.
There can be many combinations for connecting L298N to pi. This is one of them:
2. Software Program and testing
Now that the hardware of H-Bridge has been interfaced with the Pi, let’s write a python program to run this hardware and see some action ;p.
import RPi.GPIO as gpio
import timedef init():
gpio.setup(24, gpio.OUT)def forward(sec):
gpio.cleanup() def reverse(sec):
gpio.cleanup()seconds = 3time.sleep(seconds)
Code is available at
You can't perform that action at this time. You signed in with another tab or window. You signed out in another tab or…
Let’s run this basic program.
Yayee, that feels good. So we have the motors running for this robot now.
For the next post, I will be writing about interfacing of other sensors to the raspberry pi.
Thanks for reading. If you like this blog, please clap.
Next up, read about the two types of perception sensors I am using for my robot. The problems, solutions and procedure is described in the blog below.