In an earlier tutorial I looked at how you can use a RasPiRobot board to supply power to your Raspberry Pi using an AA Battery Pack. In this tutorial I take that a stage further to show how you can make your Raspberry Pi control small DC motors.

Disconnect your Raspberry Pi from its power supply and plug the RRBV2 onto the GPIO header. If you have a model B+ Raspberry Pi, then the board goes over the headers nearest the edge of the board as shown below.

Now attach the motors to the screw terminal pairs marked L and R. It does not really matter which way around you connect the motor leads, as long as the leads from one motor go to the pair of screw terminals marked L and the other motor's leads go to the screw terminal pair marked R. If you swap over the leads for a particular motor, it will spin in reverse.

Connect the battery pack to the screw terminals of the RRBV2. If you are using a battery pack with flying leads then these can be attached into the screw terminals directly. If you have a battery pack with a 2.1mm plug on the end, then either use a screw terminal to 2.1mm socket adaptor like this and some short lengths of wire.

When attaching the battery make sure that the positive (red) lead goes to the right-most screw terminal marked Vin and the black lead goes to the screw terminal marked GND.

With the battery connected, the Raspberry Pi will start to boot, it will be supplied with power from the battery pack.

Your Pi will need to be connected to the Internet so that you can download the RRBV2 Python library. To download the library open LXTerminal (or use SSH) and type in the following commands:

$ wget https://github.com/simonmonk/raspirobotboard2/raw/master/python/dist/rrb2-1.1.tar.gz
$ tar -xzf rrb2-1.1.tar.gz
$ cd rrb2-1.1
$ sudo python setup.py install

Rather than type in the long URL above its is probably a good idea to browse to this tutorial from your Raspberry Pi so that you can copy and paste the commands.

Once the library has installed, we can start up a Python command line and try a few commands to get the motors running. Type the following command to start an Interactive Python session.

from rrb2 import *
rr = RRB2()
rr.forward()
If you have a very old revision 1 model B Raspberry Pi then change the second line to: rr = RRB2(revision=1)

To stop the motors again, enter the following line:

rr.stop()

To set the motors running forward for 5 seconds, use the command:

rr.forward(5)

To set the motors running forward at half speed enter the following command:

rr.forward(0, 0.5)

As well as forward try experimenting with the commands reverse, left and right, which work in just the same way as forward.

As well as being able to control a pair of DC motors, the RRBV2 has some other useful features:

  • you can plug a selection of Adafruit I2C displays straight into its I2C socket
  • it directly accepts an HC-SR-04 range finder
  • it has 2 built-in LEDs
  • it has 2 inputs that can be connected to switches
  • it has 2 buffered open-collector outputs 

For more information of the RaspiRobot Board V2 and its Python library, see raspirobot.com.

If you want to take your whole Raspberry Pi mobile, get yourself a low cost robot chassis on eBay. Look for one that includes 6V gear motors and wire them up just like the motors we used here.

This guide was first published on Sep 19, 2014. It was last updated on Mar 08, 2024.