We assembled a robot car and connect the GPIO ports to the motor driver. In this chapter, we will use pythons scripts to toggle the GPIO ports so that we can control the car movement.
GPIO PIN Mapping
The GPIO PIN mapping table
Driver Module
Raspberry PIN
left_I1
GPIO2
left_I2
GPIO3
left_EA
GPIO4
right_I1
GPIO27
right_I2
GPIO27
right_EA
GPIO18
can be described using the following python initialization code :
#Raspberry GPIO PIN for car control
#use GPIO 2, 3 for left motors direction control , 4 for power control
LEFT_FORWARD = 2
LEFT_BACKWARD= 3
LEFT_PWM = 4
#use GPIO 27, 17 for right motors direction control , 18 for power control
RIGHT_FORWARD = 27
RIGHT_BACKWARD= 17
RIGHT_PWM = 18
Initialization
We also set some global variables to set the initial speed of the left/right motors.
#left motor speed initial value
speedleft = 0
#right motor speed initial value
speedright = 0
#power for motor ranges from 0 to 100, 0 is stop, 100 is maximum
PWM_MAX = 100
All the GPIO ports are set to low and the initial power of motors are set to 0.
To move the car forward, both left and right motors have to drive forward. So we need to set the forward GPIO ports to high. To maximize the motor power, we set the duty cycle of both PWM GPIO to 100.
Once the forward() function is called, the car will keep on moving forward. We can add a time delay after the forward function, then we stop the car by setting all GPIO ports to low and change the PWM duty cycle to 0.
lesson1.py is the example python script for the car forward move. This script moves car forward for one second and then stops the car. If longer movement are needed, you can modify the time delay to add more seconds (time.delay(seconds)).
#lession1.py
import RPi.GPIO as GPIO
import sys, tty, termios, os
import time
#Raspberry GPIO PIN for car control
#use GPIO 2, 3 for left motors direction control , 4 for power control
LEFT_FORWARD = 2
LEFT_BACKWARD= 3
LEFT_PWM = 4
#use GPIO 27, 17 for right motors direction control , 18 for power control
RIGHT_FORWARD = 27
RIGHT_BACKWARD= 17
RIGHT_PWM = 18
#left motor speed initial value
speedleft = 0
#right motor speed initial value
speedright = 0
#power for motor ranges from 0 to 100, 0 is stop, 100 is maximum
PWM_MAX = 100
def forward():
GPIO.output(LEFT_BACKWARD, GPIO.LOW)
GPIO.output(LEFT_FORWARD, GPIO.HIGH)
leftmotorpwm.ChangeDutyCycle(100)
GPIO.output(RIGHT_BACKWARD, GPIO.LOW)
GPIO.output(RIGHT_FORWARD, GPIO.HIGH)
rightmotorpwm.ChangeDutyCycle(100)
def stop():
GPIO.output(LEFT_BACKWARD, GPIO.LOW)
GPIO.output(LEFT_FORWARD, GPIO.LOW)
GPIO.output(RIGHT_BACKWARD, GPIO.LOW)
GPIO.output(RIGHT_FORWARD, GPIO.LOW)
leftmotorpwm.ChangeDutyCycle(0)
rightmotorpwm.ChangeDutyCycle(0)
#initialization
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEFT_FORWARD, GPIO.OUT)
GPIO.setup(LEFT_BACKWARD, GPIO.OUT)
GPIO.setup(LEFT_PWM, GPIO.OUT)
GPIO.setup(RIGHT_FORWARD, GPIO.OUT)
GPIO.setup(RIGHT_BACKWARD, GPIO.OUT)
GPIO.setup(RIGHT_PWM, GPIO.OUT)
leftmotorpwm = GPIO.PWM(LEFT_PWM,100)
rightmotorpwm = GPIO.PWM(RIGHT_PWM,100)
leftmotorpwm.start(0)
leftmotorpwm.ChangeDutyCycle(0)
rightmotorpwm.start(0)
rightmotorpwm.ChangeDutyCycle(0)
#my test starts here
forward()
time.sleep(1)
stop()
# End
Backward Movement
To move the car backward, both left and right motors have to drive backward. So we need to set the backward GPIO ports to high. To maximize the motor power, we set the duty cycle of both PWM GPIO to 100.
lesson2.py is the example python script for the car backward move. This script moves car backward for one second and then stops the car.
#lesson2.py
import RPi.GPIO as GPIO
import sys, tty, termios, os
import time
#Raspberry GPIO PIN for car control
#use GPIO 2, 3 for left motors direction control , 4 for power control
LEFT_FORWARD = 2
LEFT_BACKWARD= 3
LEFT_PWM = 4
#use GPIO 27, 17 for right motors direction control , 18 for power control
RIGHT_FORWARD = 27
RIGHT_BACKWARD= 17
RIGHT_PWM = 18
#left motor speed initial value
speedleft = 0
#right motor speed initial value
speedright = 0
#power for motor ranges from 0 to 100, 0 is stop, 100 is maximum
PWM_MAX = 100
def forward():
GPIO.output(LEFT_BACKWARD, GPIO.LOW)
GPIO.output(LEFT_FORWARD, GPIO.HIGH)
leftmotorpwm.ChangeDutyCycle(100)
GPIO.output(RIGHT_BACKWARD, GPIO.LOW)
GPIO.output(RIGHT_FORWARD, GPIO.HIGH)
rightmotorpwm.ChangeDutyCycle(100)
def backward():
GPIO.output(LEFT_BACKWARD, GPIO.HIGH)
GPIO.output(LEFT_FORWARD, GPIO.LOW)
leftmotorpwm.ChangeDutyCycle(100)
GPIO.output(RIGHT_BACKWARD, GPIO.HIGH)
GPIO.output(RIGHT_FORWARD, GPIO.LOW)
rightmotorpwm.ChangeDutyCycle(100)
def stop():
GPIO.output(LEFT_BACKWARD, GPIO.LOW)
GPIO.output(LEFT_FORWARD, GPIO.LOW)
GPIO.output(RIGHT_BACKWARD, GPIO.LOW)
GPIO.output(RIGHT_FORWARD, GPIO.LOW)
leftmotorpwm.ChangeDutyCycle(0)
rightmotorpwm.ChangeDutyCycle(0)
#initialization
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEFT_FORWARD, GPIO.OUT)
GPIO.setup(LEFT_BACKWARD, GPIO.OUT)
GPIO.setup(LEFT_PWM, GPIO.OUT)
GPIO.setup(RIGHT_FORWARD, GPIO.OUT)
GPIO.setup(RIGHT_BACKWARD, GPIO.OUT)
GPIO.setup(RIGHT_PWM, GPIO.OUT)
leftmotorpwm = GPIO.PWM(LEFT_PWM,100)
rightmotorpwm = GPIO.PWM(RIGHT_PWM,100)
leftmotorpwm.start(0)
leftmotorpwm.ChangeDutyCycle(0)
rightmotorpwm.start(0)
rightmotorpwm.ChangeDutyCycle(0)
#my test starts here
backward()
time.sleep(1)
stop()
# End
Moving Direction Control
We can move the car left or right by moving wheels in the opposite directions. To move car left, we can run left side wheels backward and the right side wheels forward. We can do this by setting left backward GPIO to high and right GPIO to low. The PWM GPIO is set to 100 to maximize the power.
To move the car right, we run right side wheels backward and left side wheels forward. This requires left GPIO to set to high and right GPIO to set to low.
lesson3.py is the complete python script to move the car left for one second and then move the car to right for another second. After that, the car stops.
import RPi.GPIO as GPIO
import sys, tty, termios, os
import time
#Raspberry GPIO PIN for car control
#use GPIO 2, 3 for left motors direction control , 4 for power control
LEFT_FORWARD = 2
LEFT_BACKWARD= 3
LEFT_PWM = 4
#use GPIO 27, 17 for right motors direction control , 18 for power control
RIGHT_FORWARD = 27
RIGHT_BACKWARD= 17
RIGHT_PWM = 18
#left motor speed initial value
speedleft = 0
#right motor speed initial value
speedright = 0
#power for motor ranges from 0 to 100, 0 is stop, 100 is maximum
PWM_MAX = 100
def forward():
GPIO.output(LEFT_BACKWARD, GPIO.LOW)
GPIO.output(LEFT_FORWARD, GPIO.HIGH)
leftmotorpwm.ChangeDutyCycle(100)
GPIO.output(RIGHT_BACKWARD, GPIO.LOW)
GPIO.output(RIGHT_FORWARD, GPIO.HIGH)
rightmotorpwm.ChangeDutyCycle(100)
def backward():
GPIO.output(LEFT_BACKWARD, GPIO.HIGH)
GPIO.output(LEFT_FORWARD, GPIO.LOW)
leftmotorpwm.ChangeDutyCycle(100)
GPIO.output(RIGHT_BACKWARD, GPIO.HIGH)
GPIO.output(RIGHT_FORWARD, GPIO.LOW)
rightmotorpwm.ChangeDutyCycle(100)
def left():
GPIO.output(LEFT_BACKWARD, GPIO.HIGH)
GPIO.output(LEFT_FORWARD, GPIO.LOW)
leftmotorpwm.ChangeDutyCycle(100)
GPIO.output(RIGHT_BACKWARD, GPIO.LOW)
GPIO.output(RIGHT_FORWARD, GPIO.HIGH)
rightmotorpwm.ChangeDutyCycle(100)
def right():
GPIO.output(LEFT_BACKWARD, GPIO.LOW)
GPIO.output(LEFT_FORWARD, GPIO.HIGH)
leftmotorpwm.ChangeDutyCycle(100)
GPIO.output(RIGHT_BACKWARD, GPIO.HIGH)
GPIO.output(RIGHT_FORWARD, GPIO.LOW)
rightmotorpwm.ChangeDutyCycle(100)
def stop():
GPIO.output(LEFT_BACKWARD, GPIO.LOW)
GPIO.output(LEFT_FORWARD, GPIO.LOW)
GPIO.output(RIGHT_BACKWARD, GPIO.LOW)
GPIO.output(RIGHT_FORWARD, GPIO.LOW)
leftmotorpwm.ChangeDutyCycle(0)
rightmotorpwm.ChangeDutyCycle(0)
#initialization
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEFT_FORWARD, GPIO.OUT)
GPIO.setup(LEFT_BACKWARD, GPIO.OUT)
GPIO.setup(LEFT_PWM, GPIO.OUT)
GPIO.setup(RIGHT_FORWARD, GPIO.OUT)
GPIO.setup(RIGHT_BACKWARD, GPIO.OUT)
GPIO.setup(RIGHT_PWM, GPIO.OUT)
leftmotorpwm = GPIO.PWM(LEFT_PWM,100)
rightmotorpwm = GPIO.PWM(RIGHT_PWM,100)
leftmotorpwm.start(0)
leftmotorpwm.ChangeDutyCycle(0)
rightmotorpwm.start(0)
rightmotorpwm.ChangeDutyCycle(0)
#my test starts here
left()
time.sleep(1)
right()
time.sleep(1)
stop()
# End