How to control DC motors using Raspberry pi
Hey, hello everyone so I hope you are doing well. Today in this lesson we will learn how we can control any of your DC motors using your Raspberry Pi and python scripts. We will make a modular program which not only helps you to change the motor pins easily but it also helps you to implement different logics with the motors. Suppose later on you want to control your motors to make an RC car then this code can easily use the same code there. By the end of this lesson, you can able to use 2 very famous motor drivers namely L293D and L298N. So without further a due let’s start learning.
Note: As our Raspberry pi board works on 3.3 to 5v max logic so we can’t directly control any DC motors with our Raspberry pi for that purpose we have to use the motor drivers which can drive from 6 to 12v dc motors. Here we learn how to use those boards with our Raspberry pi.
Now to follow me along with this lesson you need some hardware components they are listed below.
- 2x 12V DC motors
- 1x L293D or L298N motor drivers
- 5x Jumper wires
- 1x 12V power supply
I assumed that you guys setup your Raspberry Pi with the raspbian OS properly if not then go through my previous post about How to setup your Raspberry pi . Now after the setup process is done let’s quickly make the connections properly according to the given circuit diagram. Note: while connecting the components do switch off your Raspberry Pi which will save your pi from any accidental shock circuit.
Now after properly hooked up everything let’s tarn on the power of your Raspberry pi and fire up your favourite python editor. Now after that simply copy this block of code in your editor or visit our GitHub repository for more info. And after that simply save the program with a suitable name with the extension .py .
import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) m1a,m1b=8,7 # defining motor pins GPIO.setup(m1a,GPIO.OUT) # set the motor pins as output GPIO.setup(m1b,GPIO.OUT) # set the motor pins as output def forward(): # rotate the motor clockwise GPIO.output(m1a,True) GPIO.output(m1b,False) def backword(): GPIO.output(m1a,False) # rotate the motor anti clockwise GPIO.output(m1b,True) def stop(): GPIO.output(m1a,False) # Stops the motor GPIO.output(m1b,False) while True: forward() time.sleep(1) stop() time.sleep(.5) backword() time.sleep(1) stop() time.sleep(.5)
Now to run the script we simply need to run a small command on our terminal.
And as soon as you execute the command you can see the output something like this. First the motor1 and motor2 moves clockwise after 1 sec they start rotating anti-clockwise.
Now let’s see how everything works?
Fist of all here the concept of H-bridge is applied to control the motor’s direction. Inside those motor drivers, there 2 H-bridges are used one for each motor. And the working of these H-bridges is something like this.
Here suppose the s1, s2, s3,s4 are four different switches and there is a rule applied that s1 is reverse of s3 and s2 is reverse of s4. That means when s1 is open then s3 is in the closed position, like the same when s2 is open then s4 is in the closed position and vise versa. And by those two GPIOs of the Raspberry pi, we are controlling the s1 and s2. This indirectly changes the polarity of the motor’s inputs and as a result, the motor stats turning either in clockwise or in the anti-clockwise direction.
So I hope everything works properly on your side but if you have any problem or question, simply put that in the comments section below. Also hope you learn something new today, if so don’t forget to share this in your community and leave a message below. Stay Home Stay Safe!