# Using a 5V Stepper Motor with the RasPiRobot Board V2

## Overview

In this tutorial, you will learn how to drive a 5V stepper motor using a Raspberry Pi and the RasPiRobot Board V2 (RRBv2) expansion board for the&nbsp;Raspberry Pi.

A simple Python test program will allow you to control the motor. The code can then esily be incorporated into your projects.

![](https://cdn-learn.adafruit.com/assets/assets/000/022/385/medium800/raspberry_pi_overview_web.jpg?1420805169)

Info: 

# Using a 5V Stepper Motor with the RasPiRobot Board V2

## Wiring

Fit the RRBv2 shield over the GPIO pins furthest from the USB ports (see the picture below).

![](https://cdn-learn.adafruit.com/assets/assets/000/022/386/medium800/raspberry_pi_RPi_RRBV2.jpg?1420805259)

Use male to male jumper wires to connect from the 5 pin socket of the stepper motor to the RRBv2. Note that the red wire from the stepper is not used.

![](https://cdn-learn.adafruit.com/assets/assets/000/022/387/medium800/raspberry_pi_motor_connector_closeup.jpg?1420805328)

Try to use the same color of jumper lead as the wires from the motor. I didn't have a pink jumper lead, so I used white instead of pink.

Next connect the other end of the jumper wires to the L and R screw terminals of the RRBv2 as shown below. The order of connections from top to bottom is: orange, white (pink), blue and yellow.

![](https://cdn-learn.adafruit.com/assets/assets/000/022/388/medium800/raspberry_pi_RRBv2_connector_closeup.jpg?1420805393)

Note that the screw terminals are designed to connect well even to thin leads, so you will need to first unscrew them (counter clockwise) place the lead in the hole and then screw up the connector clockwise.

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.

![](https://cdn-learn.adafruit.com/assets/assets/000/022/389/medium800/raspberry_pi_overview_web.jpg?1420805464)

# Using a 5V Stepper Motor with the 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) 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.

The example program that follows will prompt you for the delay between steps and the number of steps to take, first forwards and then backwards.

To install the code, you can [connect to your Pi using SSH](http://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh)&nbsp;and open an editor by typing:

```auto
$ nano stepper.py
```

Then paste the code below into the editor window and save it using CTRL-X and then Y.

```auto
from rrb2 import *
import time

rr = RRB2()

def forward(delay, steps):  
  for i in range(0, steps):
    rr.set_motors(1, 0, 1, 0)
    time.sleep(delay)
    rr.set_motors(1, 1, 1, 0)
    time.sleep(delay)
    rr.set_motors(1, 1, 1, 1)
    time.sleep(delay)
    rr.set_motors(1, 0, 1, 1)
    time.sleep(delay)

def backwards(delay, steps):  
  for i in range(0, steps):
    rr.set_motors(1, 1, 1, 0)
    time.sleep(delay)
    rr.set_motors(1, 0, 1, 0)
    time.sleep(delay)
    rr.set_motors(1, 0, 1, 1)
    time.sleep(delay)
    rr.set_motors(1, 1, 1, 1)
    time.sleep(delay)

  
def setStep(w1, w2, w3, w4):
  GPIO.output(coil_A_1_pin, w1)
  GPIO.output(coil_A_2_pin, w2)
  GPIO.output(coil_B_1_pin, w3)
  GPIO.output(coil_B_2_pin, w4)

while True:
  delay = raw_input("Delay between steps (milliseconds)?")
  steps = raw_input("How many steps forward? ")
  forward(int(delay) / 1000.0, int(steps))
  steps = raw_input("How many steps backwards? ")
  backwards(int(delay) / 1000.0, int(steps))
  rr.set_motors(0, 0, 0, 0) # All coils off
```

Run the program by typing the command:

```auto
$ sudo python stepper.py
```

You will then see a prompt for the delay between steps. The minimum value here is 3, increase the number to make the stepper turn more slowly. 200 is a good number of steps to enter at the next prompt. When you press enter, you should see the stepper turn.

```auto
$ sudo python stepper.py 
Delay between steps (milliseconds)?3
How many steps forward? 200
How many steps backwards? 200
Delay between steps (milliseconds)?5
How many steps forward? 100
How many steps backwards? 100
Delay between steps (milliseconds)?
```

The program will continue in the loop until you press CTRL-C to quit.

# Using a 5V Stepper Motor with the RasPiRobot Board V2

## Next Steps

See also [this Adafruit tutorial](../../../../adafruits-raspberry-pi-lesson-10-stepper-motors)&nbsp;on using stepper motors with the Raspberry Pi.  
   
For more information of the RaspiRobot Board V2 and its Python library, see [raspirobot.com](http://www.raspirobot.com).


## Featured Products

### Raspberry Pi Model B+ 512MB RAM

[Raspberry Pi Model B+ 512MB RAM](https://www.adafruit.com/product/1914)
OMG OMG OMG, did you hear? There's a Raspberry Pi&nbsp;called the Model B+ and check it out...more USB ports, more GPIO, better power supply, four mounting holes, less sticky-out SD card! Yep, that's right, the fantastic engineers at Raspberry Pi HQ have blessed us with a new design....

In Stock
[Buy Now](https://www.adafruit.com/product/1914)
[Related Guides to the Product](https://learn.adafruit.com/products/1914/guides)
### RasPi Robot Board v3 by MonkMakes

[RasPi Robot Board v3 by MonkMakes](https://www.adafruit.com/product/1940)
The RaspiRobot Board v3 is an expansion board designed to turn your Raspberry Pi into a motor controller! This board comes fully assembled and includes a switched-mode power supply so you can supply your Raspberry Pi from a variety of battery packs.

The v3 fits right on top of your...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1940)
[Related Guides to the Product](https://learn.adafruit.com/products/1940/guides)
### Small Reduction Stepper Motor - 5VDC 32-Step 1/16 Gearing

[Small Reduction Stepper Motor - 5VDC 32-Step 1/16 Gearing](https://www.adafruit.com/product/858)
This is a great first stepper motor, good for small projects and experimenting with steppers. This uni-polar motor has a built-in mounting plate with two mounting holes. There are only 32 steps (11.25 degree) per revolution, and inside is a 1/16&nbsp;reduction gear set. (Actually it's...

Out of Stock
[Buy Now](https://www.adafruit.com/product/858)
[Related Guides to the Product](https://learn.adafruit.com/products/858/guides)
### 6 x AA battery holder with 5.5mm/2.1mm plug

[6 x AA battery holder with 5.5mm/2.1mm plug](https://www.adafruit.com/product/248)
Make a portable power brick with plenty of juice! Use Alkaline AA's for a 9V 3000-4000mAh power supply, or rechargeable NiMH for 2000mAh 7.5V supply. Either one is good for running electronics that have a 5V voltage regulator (thus requiring a 7V+ supply). Will last about 10 times longer...

In Stock
[Buy Now](https://www.adafruit.com/product/248)
[Related Guides to the Product](https://learn.adafruit.com/products/248/guides)
### Female DC Power adapter - 2.1mm jack to screw terminal block

[Female DC Power adapter - 2.1mm jack to screw terminal block](https://www.adafruit.com/product/368)
If you need to connect a DC power wall wart to a board that doesn't have a DC jack - this adapter will come in very handy! There is a 2.1mm DC jack on one end, and a screw terminal block on the other. The terminals are labeled with positive/negative assuming a positive-tip configuration...

In Stock
[Buy Now](https://www.adafruit.com/product/368)
[Related Guides to the Product](https://learn.adafruit.com/products/368/guides)
### Premium Male/Male Jumper Wires - 40 x 3" (75mm)

[Premium Male/Male Jumper Wires - 40 x 3" (75mm)](https://www.adafruit.com/product/759)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 3" (75mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on either end and fit cleanly next to each other...

In Stock
[Buy Now](https://www.adafruit.com/product/759)
[Related Guides to the Product](https://learn.adafruit.com/products/759/guides)

## Related Guides

- [Raspberry Pi WiFi Radio](https://learn.adafruit.com/pi-wifi-radio.md)
- [Using Servos With CircuitPython and Arduino](https://learn.adafruit.com/using-servos-with-circuitpython.md)
- [Playing sounds and using buttons with Raspberry Pi](https://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi.md)
- [AstroPrint 3D Printing](https://learn.adafruit.com/astroprint-3d-printing.md)
- [Stumble-Bot](https://learn.adafruit.com/stumble-bot-with-circuit-playground-and-crickit.md)
- [Adafruit Pi Stemma QT Breakout](https://learn.adafruit.com/adafruit-pi-stemma-qt-breakout.md)
- [FONA Tethering to Raspberry Pi or BeagleBone Black](https://learn.adafruit.com/fona-tethering-to-raspberry-pi-or-beaglebone-black.md)
- [Send Raspberry Pi Data to COSM](https://learn.adafruit.com/send-raspberry-pi-data-to-cosm.md)
- [LED Breath Stats Mask](https://learn.adafruit.com/led-breath-stats-mask.md)
- [How to Build a Testing Jig](https://learn.adafruit.com/how-to-build-a-testing-fixture.md)
- [Slider Crank Mechanism -- from Cardboard and Craft Sticks](https://learn.adafruit.com/cardboard-slider-crank.md)
- [Crawling Animatronic Hand](https://learn.adafruit.com/crawling-hand-with-cpx-and-makecode.md)
- [Improve the Low Speed of Brushed DC Motors](https://learn.adafruit.com/improve-low-speed-performance-of-brushed-dc-motors.md)
- [Adafruit SensorLab - Gyroscope Calibration](https://learn.adafruit.com/adafruit-sensorlab-gyroscope-calibration.md)
- [Robotic AI Bear using ChatGPT](https://learn.adafruit.com/robotic-ai-bear-using-chatgpt.md)
