Controlling Direction and Speed of DC Motor Using Raspberry Pi

Introduction: Controlling Direction and Speed of DC Motor Using Raspberry Pi

About: Student of Electronics & Communication Engineering @ Techno India NJR Institute of Technology,Udaipur, Rajasthan, India

This is a simple guide when teaches you how to control DC motors using Raspberry pi. About raspberrypi - it is a cradit card size small computer which has general purpose input output GPIO pins which are used to control different electronic devices, sensing device and motors

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Control Direction of DC Motor Via RPi

Controlling DC motor using RPi

Our next task is to control a DC motor using python script and GPIO headers on raspberry pi and this will create a picture of ‘how robot works’ in our mind. You can control fan of your room according to the weather outside (as you know the use of sensors with rpi). Doing all these will make you understand the basic rules of robotics.

We are going to control a DC motor which will need at least 400mA of current to work properly but our RPi can supply nearly 20mA only. It means we have to connect a DC power source externally. It is not a problem! We have small batteries of 6V and 9V available in the market.

Warning: Do not connect a motor directly to the Raspberry Pi, it will damage your Raspberry Pi.

Obviously we will required a motor controller IC to control the functionality of the motor(to rotate it clockwise or anti-clockwise). It is better to start with L293D motor controller IC.


· A Raspberry Pi with SD card preinstalled with Raspbian

· A Breadboard

· An L293D motor driver IC

· Jumper wires(Male to male and female to male)

· One DC motor rated for 6v

· One 6V or 9V battery.


It is a motor driver IC we can control 2 motors at a time from it. Its pin configuration is shown below. It is a 16 pin IC which includes (at one side) an Vcc (to power up), a GND pin, an Enable pin( to on and off a motor), two inputs corresponding to two outputs.


· First connect the Vcc pin of L293D to 5V (3.3V can also be use)

· Now connect the grounds

· We are using GPIO-2,3,4 to control the IC.(GPIO-02 & GPIO-03 as input 1 &2 respectively)

· Connect GPIO-04 to respective Enable pin of the IC.

· Now finally connect output 1 &2 to both the connection of the motor as shown in figure.

Python Code:-

import RPi.GPIO as GPIO

from time import sleep


Motor1A = 02

Motor1B = 03

Motor1E = 04




print "Motor going to Start"

GPIO.output(Motor1A,GPIO.HIGH) # to run motor in clockwise direction

GPIO.output(Motor1B,GPIO.LOW) # put it high to rotate motor in anti-clockwise direction

GPIO.output(Motor1E,GPIO.HIGH) # Should be always high to start motor


print "Stopping motor"

GPIO.output(Motor1E,GPIO.LOW) # to stop the motor


After running the above code your motor will start rotating in clockwise direction for 5 seconds. By modifying the above code you can do reverse it. Now we can make a car using two or four motors.

Controlling speed of DC motor using RPi

As you know at some of the places we need to also control the speed of motors. From the above topic you understood the ON-OFF control of the motors but that’s not enough right?? Now you should have a question in your mind that how we can produce analog voltage from GPIO pins of RPi. No problem because we can produce voltage pulses from it via PWM(pulse width modulation) and control output voltage of GPIO pin.

Pulse Width Modulation

It is a very simple process from which we can control the voltage frequency of the source. PWM module is available for GPIO in RPi & as we know

Time period= 1/frequency

So if I select a frequency of 100Hz than it means the time period is 10m sec. (voltage is triged to ‘HIGH’ for 10m sec). Our battery supplying 9V to motor and to reduce the speed we have to reduce this voltage. So if we want to reduce the speed to half than we have to change the dutycycle (it is percentage of the time period for which voltage is HIGH) to 50 .

As voltage supplied to the motor is controlling by Enable pin of the motor driver IC- L293N so we only need to apply the PWM to Enable pin to vary the speed. Bellow is the python code to understand PWM and which needs the connection above in previous topic (control DC motor via RPi).

>>>import RPi.GPIO as GPIO # import GPIO librery

>>>from time import sleep

>>> GPIO.setmode(GPIO.BCM)

>>>Motor1A = 02 # set GPIO-02 as Input 1 of the controller IC

>>>Motor1B = 03 # set GPIO-03 as Input 2 of the controller IC

>>>Motor1E = 04 # set GPIO-04 as Enable pin 1 of the controller IC




>>>pwm=GPIO.PWM(04,100) # configuring Enable pin means GPIO-04 for PWM

>>>pwm.start(50) # starting it with 50% dutycycle

>>>print "GO forward"





# this will run your motor in forward direction for 2 seconds with 50% speed.

>>>pwm.ChangeDutyCycle(80) # increasing dutycycle to 80

>>>print "GO backward"





# this will run your motor in reverse direction for 2 seconds with 80% speed by supplying 80% of the battery voltage

>>>print "Now stop"


>>>pwm.stop() # stop PWM from GPIO output it is necessary


Hope it will work for you!!

Be the First to Share


    • LED Strip Speed Challenge

      LED Strip Speed Challenge
    • Sculpting Challenge

      Sculpting Challenge
    • Clocks Contest

      Clocks Contest

    5 Discussions


    3 years ago

    seriously, what's all that code?

    dude, use gpiozero library, but make sure you got v1.2.0

    there's a library for motors, and even for robots (with 2 motors)

    the code will be something like

    from gpiozero import Motor

    motor = Motor(17, 18)

    motor.forward(0.36) #duty value as a float from 0 to 1

    have fun :D


    Reply 3 years ago

    for PWM on a Raspberry Pi 3 using the GPIOZero Library, can I use any GPIO Pin? I'm new to the RPi, and I don't want anything to short circuit.


    Reply 3 years ago

    Yaah !! U r right. But this is also the way to do so;


    Reply 3 years ago

    yea i know, i used lines of code like that before to control a motor, and i even made them into functions to control a robot with 2 motors (forward, backward, right, ... bla bla bla ). that was till i realized that the same EXACT thing can be done by like 2 or 3 lines of code. they made it even easier than arduino :D


    Reply 3 years ago

    Yaah code should be compact as possible u r right!! Thank u very much for the suggetion®®