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.

$cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION="9 (stretch)"
  • Python: Installed on Raspberry Pi. Its possible its pre-installed. Version information below.
$python --version
Python 2.7.13
  • 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).

Figure#1: GPIO Pins on Raspberry Pi
  • 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.

Figure#2: Pins of L293D

Begin Connections

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)

Connect Ground

  1. We are using Raspberry Pi PIN#3 for the ground
  2. 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:

Figure#3: Inital Enabling of L293D

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:

Figure#4: L293D Ground Connections

Connect Pi’s GPIO to L293D

  1. Pi PIN#36 to L293D PIN#2
  2. Pi PIN#38 to L293D PIN#7
  3. Pi PIN#40 to L293D PIN#8

Figure#5 shows the GPIO to Chip connections:

Figure#5: GPIO to L293D

Connect Power using AA Battery to L293D Chip

  1. 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

  1. Connect Motor with PIN#3 and #6 of L293D

Figure#6 shows Battery and motor connections:

Figure#6: Batttery and Motor connected to L293D

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 and paste the below code into this file.

import RPi.GPIO as GPIO 
from time import sleep
GPIO.setmode(GPIO.BOARD)             #1
Motor1A = 36 
Motor1B = 38
Motor1E = 40
GPIO.setup(Motor1A,GPIO.OUT)         #2
print "Turning motor on"
GPIO.output(Motor1A,GPIO.HIGH) #3
    print "Stopping motor"
GPIO.output(Motor1E,GPIO.LOW) #4
GPIO.output(Motor1E,GPIO.LOW) #5
GPIO.cleanup() #6

In above code, we see the below happening: (The line numbers represented are serial numbers of the below.)

  1. 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
Stopping motor

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.


Please enter your comment!
Please enter your name here