# Controlling Motors using the Raspberry Pi and RasPiRobot Board V2

## Overview

In [an earlier tutorial](../../../../raspirobot-battery-powered-raspberry-pi-displays)&nbsp;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.

![](https://cdn-learn.adafruit.com/assets/assets/000/019/779/medium800/raspberry_pi_overview.jpg?1411118639)

# Controlling Motors using the Raspberry Pi and RasPiRobot Board V2

## Wiring

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.

![](https://cdn-learn.adafruit.com/assets/assets/000/019/780/medium800/raspberry_pi_RPi_RRBV2.jpg?1411118687)

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](https://www.adafruit.com/products/368)&nbsp;and some short lengths of wire.

![](https://cdn-learn.adafruit.com/assets/assets/000/019/781/medium800/raspberry_pi_closeup.jpg?1411118832)

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.

# Controlling Motors using the Raspberry Pi and RasPiRobot Board V2

## Software

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](../../../../adafruits-raspberry-pi-lesson-6-using-ssh)) and type in the following commands:

```auto
$ 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.

# Controlling Motors using the Raspberry Pi and RasPiRobot Board V2

## Python Experiments

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.

```auto
from rrb2 import *
rr = RRB2()
rr.forward()
```

Info: 

To stop the motors again, enter the following line:

```auto
rr.stop()
```

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

```auto
rr.forward(5)
```

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

```auto
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_.

# Controlling Motors using the Raspberry Pi and RasPiRobot Board V2

## Next Steps

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

- you can plug&nbsp;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&nbsp;

For more information of the RaspiRobot Board V2 and its Python library, see [raspirobot.com](http://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.


## Related Guides

- [Introducing Adafruit Crickit #MakeRobotFriend](https://learn.adafruit.com/adafruit-crickit-creative-robotic-interactive-construction-kit.md)
- [Adafruit INA3221 Breakout](https://learn.adafruit.com/adafruit-ina3221-breakout.md)
- [No-Solder Faux Neon Bike Lights](https://learn.adafruit.com/no-solder-faux-neon-bike-lights.md)
- [Robotic AI Bear using ChatGPT](https://learn.adafruit.com/robotic-ai-bear-using-chatgpt.md)
- [Stick Person Costume with Neon LED Strips](https://learn.adafruit.com/led-neon-stick-person-costume.md)
- [Crickit Paddle Wheel Boat](https://learn.adafruit.com/crickit-boat.md)
- [Desktop Dumpster Fire](https://learn.adafruit.com/desktop-dumpster-fire.md)
- [LED Matrix FIFA World Cup Scoreboard](https://learn.adafruit.com/led-matrix-fifa-world-cup-scoreboard.md)
- [Optical Sensor Drum Track Sequencer](https://learn.adafruit.com/drum-track-sequencer.md)
- [Automatic Dice Roller](https://learn.adafruit.com/automatic-dice-roller.md)
- [Chilled Drinkibot](https://learn.adafruit.com/chilled-drinkibot.md)
- [RGB Matrix Panels With Raspberry Pi 5](https://learn.adafruit.com/rgb-matrix-panels-with-raspberry-pi-5.md)
- [Adafruit I2C to 8 Channel Solenoid Driver](https://learn.adafruit.com/adafruit-i2c-to-8-channel-solenoid-driver.md)
- [Glowing Bottle Castle and Capacitive Touch Piano](https://learn.adafruit.com/glowing-bottle-castle-and-capacitive-touch-piano.md)
- [Dotstar LED and Glass Pebble Floor](https://learn.adafruit.com/neopixel-and-glass-pebble-floor.md)
