Thursday, 25 October 2012

Controlling BigTrack Motors with my Raspberry Pi

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

I've very shamelessly found and used an instructables article which shows how to use the Arduino. I would recommend you read it, most of the instructions can be followed exactly, the only differences come towards the end when it comes to connecting it up to the Raspberry Pi instead of the Arduino. So have a look and read this here :

Also if this is your first ever thing using the GPIO's then I would recommend that you start with the articles in the MagPi Magazines first as they will do a lot better job at explaining what's going on. Have a look here :

The idea of the using a Big Track as a platform wasn't mine either, there have been some good articles in the MagPi Magazine, and some activity on the Raspberry Pi forums (

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 (

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 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.


import time
import RPi.GPIO as GPIO

#Declare the GPIO settings
#print "Set GPIO Board numbers"
# to use Raspberry pi board pin numbers

# 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)


#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)


#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

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. 


  1. Cool. Read your post at and I think you have a great point. The setup is made such that a person must know too much about everything. i.e. coding, operating systems, and electronics - in detail. There is not enough good information like this out there for those who want to skip the electronics and get straight into coding fun stuff. Just tell me what parts to buy and exactly how to hook it up so that I can continue being productive writing fun software. Nice that people like you are writing down the 'recipes' to get people through it.

  2. Thanks very much for your comments, very glad you have found it useful. I've not done much with my raspberry pi over Christmas & new year but hopefully will get some more bits up and running in the not too distant future.