Simple Stepper Motor Control with Raspberry Pi
Stepper motors are a type of brushless DC motor that can be controlled so that they advance a fixed step size. Additionally, they provide a strong holding torque. This can make them useful in applications where you want to rotate the motor precisely without employing encoders. When prototyping, I like to use development boards that can be transferred to a custom PCB easily. This gives me a clear path if I want to move the design from the Protoboard to something more polished.
Here’s the parts list:
- Pololu DRV8825 carrier
- 57oz-in 1Nm NEMA 17 Stepper Motor 1.3A 40mm for CNC router or mill
- 100 Pcs 25V 100uF 105C Radial Lead Electrolytic Capacitors
- Raspberry Pi Model A (256MB) – I had a Model A laying around, but you should be able to use the Model B with a little modification
- Adafruit Pi Cobbler Breakout Kit for Raspberry Pi – Assembled! – If you are using the Model B, you may want the breakout for the full model B GPIO header
- HiMart New DC 12V 3A Switching Power Supply Adapter For 110V- 240V AC 50/60Hz 2.1mm With HiMart Robbin
Wait. Didn’t you say that you wanted a clear path to an integrated PCB design? Why use the Pi then? You’ll be hard-pressed to design that SoC into your own design, or at the least, make a full Linux compatible single board PC. Because I’m a hypocrite. Maybe we’ll connect it to a joystick down the road, and this will make life easier.
The Circuit
The minimum number of connections are used for this proof-of-concept. I’m using the pins labeled GPIO 7 and GPIO 8 in the Raspberry Pi documentation to control the DRV8825’s STEP and DIR inputs, respectively.
Make sure to turn the DRV8825 carrier board’s potentiometer fully counter-clockwise. This will severely limit the coil current, but prevent burn-out before we are ready to set this properly.
The Software
There are a few options for controlling the Pi’s GPIO pins. I’m going to avoid the high-level libraries to get high-speed control of the IO pins. Joonas Pihlajamaa provides a through benchmark of the various options, showing that C is needed to get high-speed control of the GPIO. We could probably write a Python wrapper around some C code to get achieve high speed control, but for simplicity, lets use the minimum needed to verify that our circuit is working. The 3 files main.c
, gpio.c
, and gpio.h
are all that is needed to get the motor turning. It’s nearly identical to the RPi low-level peripheral example, though I wrapped some of the operations for convenience.
The project’s code is available on Github.
The Test
Turn the potentiometer clockwise 20 degrees or so. With the Raspberry Pi header or 12 V supply disconnected, you should be able to rotate the stepper motor shaft by hand. With both connected, the DRV8825 will be enabled and the stepper will hold. The torque required to rotate the motor should feel much higher and you can hear a high pitched whine from motor, though the later varies with hold torque, age, and hearing damage. Follow Pololu’s procedure to adjust the power limiting via the potentiometer.
The test program must be executed with “sudo” or as root because it accesses /dev/mem
. If everything works out, the motor should spin smoothly when you run the program.