Last time I wrote about the Motors Shield and I tried to describe how the L293D modul and the 74HC595 module work. And now is time for programming. But we have to wire it up first. You can connect PINs according this image. (image update - 05.09.2016 - fixed wiring of blue and brow wire on motor shield, thanks to Maxime)
And when you have it connected we can start talking about using python to run the motors.
Requirements
- Raspbery Pi with installed OS (I have Raspbian)
- Installed Git, Python3 and RPi.GPI - sudo aptitude install python3 python3-rpi.gpio
- Connectected Motor Shield to Raspberry Pi
Controlling motors from Python
I've wrote Python class for controlling motors using this Motor Shield. You can download it from my github.
git clone https://github.com/lipoja/AMSpi.git
Using AMSpi class
When you are creating object you can specify what pin numbering you will use.
from AMSpi import AMSpi
# For BOARD pin numbering use AMSpi(True) otherwise BCM is used
with AMSpi() as amspi:
# Set PINs for controlling shift register (GPIO numbering)
amspi.set_74HC595_pins(21, 20, 16)
# Set PINs for controlling all 4 motors (GPIO numbering)
amspi.set_L293D_pins(5, 6, 13, 19)
# Run motors
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4])
# For BOARD pin numbering use AMSpi(True) otherwise BCM is used
with AMSpi() as amspi:
# Set PINs for controlling shift register (GPIO numbering)
amspi.set_74HC595_pins(21, 20, 16)
# Set PINs for controlling all 4 motors (GPIO numbering)
amspi.set_L293D_pins(5, 6, 13, 19)
# Run motors
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4])
When you are creating object you can specify what pin numbering you will use.
- AMSpi() - for GPIO.BCM
- AMSpi(True) - for GPIO.BOARD
In this example I am using BCM. BCM is the GPIO number. BOARD is the number in the circle (on the image above).
Then you have to set the pins for shift register. It is done by set_74HC595_pins method.
amspi.set_74HC595_pins(DIR_LATCH=21, DIR_CLK=20, DIR_SER=16)
After that you must specify used pins for enabling the motors. This is done by set_L293D_pins method.
amspi.set_L293D_pins(5, 6, 13, 19)
You can choose which motor do you want to use. If you have only two motors you can set it like this:
amspi.set_L293D_pins(PWM2A=13, PWM2B=19)
Now the exciting part. Spinning the wheel :-]. You can start one or multiple motors at time. Using run_dc_motors method and specifying which motor you want to run.
For this you can use class variable DC_Motor_X where X is number of the motor.
(DC_Motor_1, DC_Motor_2, DC_Motor_3, DC_Motor_4)
Running all 4 motors:
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4])
Running only two motors:
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2])
If you want to run only one motor you can use same method:
amspi.run_dc_motors([amspi.DC_Motor_1])
Or you can use this method for running only one specific motor:
amspi.run_dc_motor(amspi.DC_Motor_1)
Now the direction of the motor. How do you switch it. Easily.
Just add False as second parameter of method:
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4], clockwise=False)
OK, now you car is driving super fast forward but how do you stop it? Piece of cake. Just call:
amspi.stop_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4])
For this you can use class variable DC_Motor_X where X is number of the motor.
(DC_Motor_1, DC_Motor_2, DC_Motor_3, DC_Motor_4)
Running all 4 motors:
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4])
Running only two motors:
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2])
If you want to run only one motor you can use same method:
amspi.run_dc_motors([amspi.DC_Motor_1])
Or you can use this method for running only one specific motor:
amspi.run_dc_motor(amspi.DC_Motor_1)
Now the direction of the motor. How do you switch it. Easily.
Just add False as second parameter of method:
amspi.run_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4], clockwise=False)
OK, now you car is driving super fast forward but how do you stop it? Piece of cake. Just call:
amspi.stop_dc_motors([amspi.DC_Motor_1, amspi.DC_Motor_2, amspi.DC_Motor_3, amspi.DC_Motor_4])
The metod stop_dc_motors works same as run_dc_motors.
Just specify the motors which you want to stop.
On the git repository is commented example.py file.
You can look at it and see what it can do.
OK, that was easy, right? If you have questions just ask me. I will try to answer them.