How to Drive a Stepper Motor with RaspiRobot v3

Stepper motors are motors that step through their rotation. How far the motor shaft rotates is determined by the motors step angle (for example, 1.8 degrees).  This offers finite control that make them popular in robotics, 2D and 3D printers, plotters, etc. I’m going to walk you through using a RaspiRobot v3 board attached to a Raspberry Pi 3 to control a stepper motor.

Building a 3D Printer

If all you want to do is learn about stepper motors to build a 3D printer I would recommend buying a kit instead. It’s actually a good way to learn about how printers use lead screws, limit switches (end stops) and belt drives to control movement along an axis.

Open source 3D printers based on RepRap use a dedicated board called a Rambo or a Mini Rambo.

But there are many other things that you can build using stepper motors, such as robotic arms, etc.

Stepper Motor vs PWM

You can control the rotation and speed of a hobby DC motor using pulse width modulation (PWM). But that’s not as precise as using a stepper motor.

Disclaimer

If you damage any of your equipment the author takes no responsibility.

Terminology

This article will use the following terms:

  • RRB3 – RaspiRobot V3 board

What You Will Need

Attach the Board

Here is a picture of how the RRB3 attaches to the Raspberry Pi. The pins on the bottom should go as far to the right as possible and line up with the end of the Pi connector.

rrb3_install_hw

Note that the board is only being held up on one side. When working with the connectors make sure you don’t lean on the board.

Install the Software

The instructions for setting up and testing the board are here: raspirobotboard3 (github.com)

With some caveats I will now walk you through them.

Remote into your Raspberry Pi 3 via ssh and execute the following:

You will need git. If it isn’t installed on your Pi, do the following:

$ sudo apt-get install git

Once git is installed you can clone the RRB3 setup code to your Pi and install it:

$ git clone https://github.com/simonmonk/raspirobotboard3.git
$ cd raspirobotboard3/python/
$ sudo python setup.py install

Test the Board

For this step you don’t need to attach a stepper motor yet.

Using the python console you can now run some tests (as per the GitHub instructions). What you are looking for is to see if you can turn on and off the two LEDs on the RaspiRobot board.

NOTE: The documentation has you pass in parameters to the RRB3 constructor call. I’m using no parameters which I will explain below.

from rrb3 import *
rr = RRB3()
rr.set_led1(1)
rr.set_led1(0)
rr.set_led2(1)
rr.set_led2(0)
rr.sw1_closed()

Press Ctrl-D to exit the Python console.

The RBB3 Constructor

The instructions assume you have 9 volts applied to the board and you are using it to control regular DC motors. So it tells you to do this:

rr = RRB3(9,6)

I’m actually testing by powering the board via the Pi 3 and the Pi wall adapter with no motors attached so that doesn’t apply to my initial setup.

From the documentation:

“The first parameter ‘9’ is the battery voltage (6 x 1.5V AA batteries). The second parameter (‘6’) is the motor voltage (6V for most low cost robot chassis motors). It is important to set these values correctly, as the library will manage the voltage supplied to the motors, to prevent them burning out or running too fast.”

Later when I show you how to drive a stepper motor we will need to set this based on the stepper motor that you are using.

Additional RRB3 Commands

There are additional commands (methods) that may or may not apply to what you are doing: { forward, reverse, left, right, stop, set_oc1, set_oc2 }.

Shutdown

Now we are going to wire-up the stepper and voltage supply. So power down your Pi.

$ sudo poweroff

Give it a few seconds then disconnect the power. You can leave the RaspiRobot board attached.

Store the Power Adapter

Put away your Pi power adapter because we are now going to power the Pi directly from the RaspiRobot board.  It’s not a good idea to have the Pi powered by both at the same time.

Current Limiting Power Supply

It’s always good to read the reviews on Amazon for equipment that you buy. Someone on there mentioned that if I pass 12v to the stepper it may pull as much as 11 AMPs. The preference is around 2AMPs. Fortunately, I have a current limiting power supply.  I strongly recommend that you use one as well when experimenting so that you don’t fry either board.

Wiring the RaspiRobot

Using the screw driver from an eyeglass repair kit, open up the power terminal on the right end of the RRB3 (see picture).  Turn the screws to the left (‘left to loose’) until there is enough room to insert the 16 gauge wire (preferably solid and not frayed – or may sure a fray doesn’t cross over).  The terminal on the far right is the voltage which can be as high as 12VDC. Never take my word for it.  Look at the labels on the board and look at the specs. With revisions and new models this could change. The terminal to it’s left is the ground. Once everything else is wired, both leads should go to a current limited power supply set to limit 2A at 12VDC. But don’t hook up the power until you have the motor wired up.

rrb3_wired

The stepper motor that I’m using has a 4-pin female connector attached to it. So I tried to color match the wires and inserted four male-to-male jumper wires into the connector. Then I screwed them into the four slots on the RRB3 next to the power connector. Look at your motors specs and keep the coils together. For example on my motor spec one coil is wired to the black and red wires and the other is wired to the green and black wires. So I kept them together as pairs when I wired them to the RRB3.

Not all NEMA 17 Stepper Motors are Created Equal

One very important thing to realize is that NEMA 17 is just a specification for the physical dimensions of a stepper motor. So one NEMA 17 motor may require 2VDC. Another may require 24VDC.

My NEMA 17 stepper motor is rated at 2.2VDC/phase. There is a debate as to whether or not this means that I should use a parameter of 2.2 of 4.4 for the motor voltage.  I opened a ticket with the maker of the board and will provide an update once I hear back.

I was using a DC Power Supply with current limiting capabilities. I set the limit to 2A. When I experimented with sending 6VDC to the motor, the current limit light started flashing.

Since I am powering the board with 12VDC and I only want to send 4.4VDC to the stepper motor my RRB3 call will need to look like this:

rr = RRB3(12.0,4.4)

Test the Motor

from rrb3 import *
import time
rr = RRB3(12.0,4.4)
rr.step_forward(0.01,5)
rr.step_forward(0.01,200)
rr.step_forward(0.001,200)
rr.step_forward(0.009,200)
rr.step_forward(0.005,200)
rr.step_forward(0.004,200)
rr.step_forward(0.003,200)
rr.step_forward(0.002,200)
rr.step_forward(0.001,200)  # starts to stutter
rr.step_forward(0.001,200)

When done, send the following command:

GPIO.cleanup()

Then press Ctrl-D once to exit the python interpreter.

Drive from a File

Create a python (*.py) file with the name of your stepper and voltage. For example I created a file called nema17_4vdc.py by doing the following on my Raspberry Pi:

nano nema17_4vdc.py

Add this code, adjusting the RRB3 voltage settings for your power supply and motor:

from rrb3 import *

rr = RRB3(12.0,4.4)

rr.step_forward(0.01,200)
rr.step_forward(0.005,200)
rr.step_reverse(0.005,200)
rr.step_reverse(0.01,200)

GPIO.cleanup()

Save the file.

At the command line, run the file:

python nema17_4vdc.py

The motor should turn at one speed, then speed up, then reverse, then slow down.

Going Further

Once you are comfortable with driving a stepper motor you may want to look at sites like OpenBuilds. I have no association with them. They seem to have a lot of parts and kits for building any type of stepper motor driven device that you can imagine.


References


Mitch Allen is a tech writer specializing in both hardware and software. To download his latest apps and books please visit his Web site at mitchallen.com