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.

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 and open an editor by typing:

$ nano stepper.py

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

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:

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

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

This guide was first published on Jan 09, 2015. It was last updated on Jan 09, 2015.

This page (Software) was last updated on Oct 01, 2021.

Text editor powered by tinymce.