After lots and lots of reading forums, reading the MagPi Magazine items on the GPIO, reading yet more forums, searching Google and generally stumbling about I have finally after many many months got my Raspberry Pi to control 2 motors. I have been honestly surprised that there aren't more articles giving practical examples of how to get the Raspberry Pi to do stuff like controlling Motors.
I've put this article to hopefully help anyone else who has been similarly struggling. I can't say that this is the perfect solution, I'm sure that you could argue that the Raspberry Pi is not the best device in practice to use, a lot of people have told me to do it using an Arduino .... but I don't have one of those, what I do have is a Raspberry Pi and to be honest I don't think its too bad at doing it.
Background reading and other references
Things you will need
Soldering the header pins to the TB6612FNG
This should be pretty easy, and its the only soldering needed. Basically the board comes without any pins, I used some breakaway header pins like these
Cut of 2 strips of 8 pins, and solder to each side of the controller and you should have something that looks like this
Adding the controller to the bread board and start wiring
Read the the instructables article for a good diagram showing what you need to wire up and to where (
http://www.instructables.com/id/Using-the-Sparkfun-Motor-Driver-1A-Dual-TB6612FNG-/?ALLSTEPS)
Add the controller to the board
Here I've added the external power source and connected the ground
And the other bottom conner of the breadboard I have attached the Raspberry Pi. Note I have connected the positive up to 5v (Pin 2)
Next start adding the diodes, again following the diagram on the instructables article
Add a few more diodes and wires (the two green wires go off to the big track motors) and the first motor is connected
Add the other 4 diodes and you should be almost done. Below is my breadboard with everything nearly connected - basically everything except the Raspberry Pi
And from the top
Attached the Raspberry Pi
Attaching the Raspberry Pi is pretty simple, I attached it up using the following pin numbers.
PWMA = Pin 7 (GPIO #4)
AIN2 = Pin 11 (GPIO #17)
AIN1 = Pin 12 (GPIO #18)
STBY = Pin 13 (GPIO #21)
BIN1 = Pin 15 (GPIO #22)
BIN2 = Pin 16 (GPIO #23)
PWMB = Pin 18 (GPIO #24)
It should now look something like this
Motor Control Sanity Check
I wrote this programme in Python. To run it you will need to make a file called sanity_motor_control.py. This programme is pretty simple, it will drive the motors in one direction for 5 seconds, then drive the motors in the other direction for another 5 seconds before switching off.
Its a good place to start to make sure that everything is connected up and running correctly.
#!user/bin/python
import time
import RPi.GPIO as GPIO
#Declare the GPIO settings
#print "Set GPIO Board numbers"
# to use Raspberry pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set up GPIO pins
GPIO.setup(7, GPIO.OUT) #Connected to PWMA
GPIO.setup(11, GPIO.OUT) #Connected to AIN2
GPIO.setup(12, GPIO.OUT) #Connected to AIN1
GPIO.setup(13, GPIO.OUT) #Connected to STBY
GPIO.setup(15, GPIO.OUT) #Connected to BIN1
GPIO.setup(16, GPIO.OUT) #Connected to BIN2
GPIO.setup(18, GPIO.OUT) #Connected to PWMB
#Specify the direct to turn the motor
#Clockwise AIN1/BIN1 = HIGH and AIN2/BIN2 = LOW
#Counter-Clockwise: AIN1/BIN1 = LOW and AIN2/BIN2 = HIGH
#First we will drive everything clockwise
#Set the direction of Motor A
GPIO.output(12, GPIO.HIGH) #Set AIN1
GPIO.output(11, GPIO.LOW) #Set AIN2
#Set the Speed / PWM for A
GPIO.output(7, GPIO.HIGH) #Set PWMA
#Set the direction of Motor B
GPIO.output(15, GPIO.HIGH) #Set BIN1
GPIO.output(16, GPIO.LOW) #Set BIN2
#Set the Speed / PWM for B
GPIO.output(18, GPIO.HIGH) #Set PWMA
#Make sure STBY is disabled - Set it to HIGH
GPIO.output(13, GPIO.HIGH)
time.sleep(5)
#Now drive the motor in the other direction (Counter Clockwise)
#Set the direction of Motor A
GPIO.output(12, GPIO.LOW) #Set AIN1
GPIO.output(11, GPIO.HIGH) #Set AIN2
#Set the Speed / PWM for A
GPIO.output(7, GPIO.HIGH) #Set PWMA
#Set the direction of Motor B
GPIO.output(15, GPIO.LOW) #Set BIN1
GPIO.output(16, GPIO.HIGH) #Set BIN2
#Set the Speed / PWM for B
GPIO.output(18, GPIO.HIGH) #Set PWMA
#Make sure STBY is disabled - Set it to HIGH
GPIO.output(13, GPIO.HIGH)
time.sleep(5)
#Now set everything to low (Switch everything Off)
GPIO.output(12, GPIO.LOW) #Set AIN1
GPIO.output(11, GPIO.LOW) #Set AIN2
GPIO.output(7, GPIO.LOW) #Set PWMA
GPIO.output(15, GPIO.LOW) #Set BIN1
GPIO.output(16, GPIO.LOW) #Set BIN2
GPIO.output(18, GPIO.LOW) #Set PWMA
GPIO.output(13, GPIO.LOW) #Set STBY
To run the programme run the command sudo python sanity_motor_control.py
I'm going to leave it at that for now I think. I'm working on a more complex example code which using the pygame library will allow you to control and drive the motors using the keyboard cursor keys. I'll updated as soon as I've got it a bit more sorted out.