DIY : Controlling 5V DC Motor using Raspberry Pi 3 Model B+
In this demonstration, i will show how to turn on a 5V DC Motor using a Raspberry Pi 3 Model B+. Being able to control a motor from Raspberry Pi can help in building robots, remote controlled garage doors, remote controlled fans or other appliances.
What all do we need?
- Raspberry Pi 3 Model B+ installed with Raspian.
Version information below.
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
- Python: Installed on Raspberry Pi. Its possible its pre-installed. Version information below.
- Breadboard: This is where all the connections go on.
- Cables/Wires: To make the connections.
- L293D Motor driver chip
- 1 DC Motor 5V
- 2 AA Batteries
- 1 Battery Holder
Reading PIN Numbers on Raspberry Pi
PINs on Raspberry Pi: Figure#1is an image showing all the pins of Raspberry Pi 3 Model B+ (It has 40 Pins).
- The numbers inside the RED box is the PIN Numbers which can be used in BOARD mode.
- The numbers outside the RED box, for example GPIO2 or GPIO14 or GPIOx are the notations to be used in BCM Mode. I generally prefer BOARD mode.
Reading Pin numbers on L293D Chip
I have gone through multiple articles and where the PIN#1 started from seemed different in different articles. After few trials and errors, i came up with the Figure#2 reference image. And based on some other articles (one of them linked here), this looks accurate. However, this is the reference i will be using for this demonstration.
It is recommended to have the Pi turned off when making multiple connections to avoid short-circuits on accident.
I have color coded all my wires that i used for connections as below:
Note: I am using BOARD mode for PIN numbers on Pi
- Red: Power from PI
- Black: Ground
- Green: Pi PIN#36
- Yellow: Pi PIN#38
- Orange: Pi PIN#40
- White: Connection from L293D to Motor. PIN#3 and PIN#6 of L293D to Motor.
- Blue: Battery to L293D PIN#9
Sequence of connections
Connect 5V(Pi PIN#2) to L293D PIN#1(Enable PIN)
- We are using Raspberry Pi PIN#3 for the ground
- We are connecting ground on breadboard to the vertical connections, so we don’t have to use multiple ground pins on the Pi. Instead, we could use the vertical connections. As shown in the screenshot, if the Ground is connected as shown in label#1 on the image, the whole vertical line is ground now.
Figure#3 shows the above connections:
For easy access to the ground on the both sides of breadboard, we connect the vertical line with vertical line on other side as shown in below image.
Continue on Grounds for L293D, below are the PINS on L293D which need to be grounded. PINS #4, #5, #12 and #13 are ground pins.
Figure#4 shows more ground connections to L293D:
Connect Pi’s GPIO to L293D
- Pi PIN#36 to L293D PIN#2
- Pi PIN#38 to L293D PIN#7
- Pi PIN#40 to L293D PIN#8
Figure#5 shows the GPIO to Chip connections:
Connect Power using AA Battery to L293D Chip
- Connect the PIN#9 of L293D Chip with one of the vertical connections which is not used by ground.
2. Connect the Batteries’s Positive to the vertical connection same as PIN#9’s.
3. Connect the Batteries’s Negative to ground.
Connect Motor with L293D Chip
- Connect Motor with PIN#3 and #6 of L293D
Figure#6 shows Battery and motor connections:
Connections are all done.
Power up the Raspberry Pi
Write the Code
Create a file with a name of your preference with “.py” extension. For this demo, let’s call it motor.py and paste the below code into this file.
import RPi.GPIO as GPIO
from time import sleep
Motor1A = 36
Motor1B = 38
Motor1E = 40
print "Turning motor on"
print "Stopping motor"
In above code, we see the below happening: (The line numbers represented are serial numbers of the below.)
- We are using BOARD mode
2. We are using PIN#36, #38 and #40 of GPIO as OUT mode.
3. To run the motor, we provide value of 1 or HIGH to PIN#36 and PIN#40 and 0 or LOW to PIN#38.
4. We sleep for 10 seconds, that means the motor will run for 10 seconds (This is how long the motor will run because during this time the PINS are with value 1 or HIGH)
5. To shutdown the motor, we pass value of 0 or LOW to PIN#40.
6. Finally, we clean up to reset all GPIO pins of Raspberry to its default values.
All Set, Ready to run the motor?
Go to the directory where the above file is stored from command line and run the below command. (If you used a different file name, use that file name.)
The motor should have started running. And the terminal would show below:
Turning motor on
After running for 10 seconds, the motor should have stopped. And the terminal would show below:
Turning motor on
So, now we have the motor running by passing signal from Raspberry Pi. There are more things that can be done like controlling the direction and speed of the motor and controlling more than one motor. We will look at those things in upcoming stories.