Interfacing L298N H-bridge motor driver with raspberry pi

Sharad Rawat
4 min readAug 30, 2020

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.

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.

  1. Hardware Integration
  2. Software Program and testing

Let’s go.

  1. Hardware Integration

First, let’s talk about L298N.

L298N — H Bridge Motor Driver.

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.

Red light glowing once power is ON.

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:

Connections of L298N with Pi

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 time
def init():
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.OUT)
gpio.setup(22, gpio.OUT)
gpio.setup(23, gpio.OUT)
gpio.setup(24, gpio.OUT)
def forward(sec):
init()
gpio.output(17, False)
gpio.output(22, True)
gpio.output(23, True)
gpio.output(24, False)
time.sleep(sec)
gpio.cleanup()
def reverse(sec):
init()
gpio.output(17, True)
gpio.output(22, False)
gpio.output(23, False)7
gpio.output(24, True)
time.sleep(sec)
gpio.cleanup()
def left_turn(sec):
init()
gpio.output(17, True)
gpio.output(22, False)
gpio.output(23, True)
gpio.output(24, False)
time.sleep(sec)
gpio.cleanup()
def right_turn(sec):
init()
gpio.output(17, False)
gpio.output(22, True)
gpio.output(23, False)
gpio.output(24, True)
time.sleep(sec)
gpio.cleanup()
seconds = 3time.sleep(seconds)
print("forward")
forward(seconds)
time.sleep(seconds-2)
print("right")
right_turn(seconds)
time.sleep(seconds-2)
time.sleep(seconds)
print("forward")
forward(seconds)
time.sleep(seconds-2)
print("right")
right_turn(seconds)
time.sleep(seconds-2)

Code is available at

Let’s run this basic program.

Testing
Photo by Lavi Perchik on Unsplash

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.

Photo by Anna Louise on Unsplash

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.

--

--