Introduction: Controlling Direction and Speed of DC Motor Using Raspberry Pi
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
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.
Requirements:
· 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.
L293D
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.
Connections:
· 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
GPIO.setmode(GPIO.BCM)
Motor1A = 02
Motor1B = 03
Motor1E = 04
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
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
sleep(5)
print "Stopping motor"
GPIO.output(Motor1E,GPIO.LOW) # to stop the motor
GPIO.cleanup()
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
>>>GPIO.setup(Motor1A,GPIO.OUT)
>>>GPIO.setup(Motor1B,GPIO.OUT)
>>>GPIO.setup(Motor1E,GPIO.OUT)
>>>pwm=GPIO.PWM(04,100) # configuring Enable pin means GPIO-04 for PWM
>>>pwm.start(50) # starting it with 50% dutycycle
>>>print "GO forward"
>>>GPIO.output(Motor1A,GPIO.HIGH)
>>>GPIO.output(Motor1B,GPIO.LOW)
>>>GPIO.output(Motor1E,GPIO.HIGH)
>>>sleep(2)
# this will run your motor in forward direction for 2 seconds with 50% speed.
>>>pwm.ChangeDutyCycle(80) # increasing dutycycle to 80
>>>print "GO backward"
>>>GPIO.output(Motor1A,GPIO.HIGH)
>>>GPIO.output(Motor1B,GPIO.LOW)
>>>GPIO.output(Motor1E,GPIO.HIGH)
>>>sleep(2)
# this will run your motor in reverse direction for 2 seconds with 80% speed by supplying 80% of the battery voltage
>>>print "Now stop"
>>>GPIO.output(Motor1E,GPIO.LOW)
>>>pwm.stop() # stop PWM from GPIO output it is necessary
>>>GPIO.cleanup()
Hope it will work for you!!