CircuitPython Steppers

Connecting Crickit with Circuit Playground Express with stepper motors is shown at left. 

The Crickit with Feather uses identical connections with other Crickit boards to stepper motors.

 

Note the Fritzing part for the blue stepper has changed wire order but the color connections shown are identical.

Likewise you can drive two stepper motors with the Crickit HAT for Raspberry Pi. One on the Motor ports, one on the Drive ports. 

 

The coding for each port is a bit different but the functionality is the same.

 

The Drive stepper must be Unipolar - bipolar steppers are not supported on the Drive port, only on the single Motor port.

Even though we don't make it really obvious, you can drive stepper motors from the Crickit.

Stepper motors rotate all the way around but only one 'step' at a time. Usually there's a few hundred steps per turn, making them great for precision motion. The trade off is they're very slow compared to servos or steppers. Also, unlike servos they don't know 'where' they are in the rotation, they can only step forward and backwards.

There's two kinds of stepper motors: bipolar (4-wire) and unipolar (5 or 6-wire). We can control both kinds but with some restrictions!

  • The voltage we use to power the motor is 5V only, so 5V power steppers are best, but sometimes you can drive 12V steppers at a slower/weaker rate
  • You can drive one bi-polar stepper motor via the Motor port
  • You can drive two uni-polar stepper motors, one via the Motor port and one via the Drive port
  • That means you have have two uni-polar steppers or one uni and one bi-polar. But you cannot drive two bi-polar steppers.

Bi-Polar or Uni-Polar Motor Port

The Crickit Motor port can run a unipolar (5-wire and 6-wire) or bipolar (4-wire) stepper. It cannot run steppers with any other # of wires!

The code is the same for unipolar or bipolar motors, the wiring is just slightly different.

Unlike DC motors, the wire order does matter. Connect one coil to the Motor pair #1. Connect the other coil to the Motor pair #2

  • If you have a bipolar motor, connect one motor coil to #1 and the other coil to #2 and do not connect to the center GND block.
  • If you are using a unipolar motor with 5 wires, connect the common wire to the center GND port.
  • If you are using a unipolar motor with 6 wires, you can connect the two 'center coil wires' together to the center GND port

If you are using our "12V" bi-polar stepper, wire in this order: red, yellow, (skip GND center), green, gray

If you are using our 5V uni-polar stepper, wire in this order: orange, pink, red (ground), yellow, blue.

Here is the CircuitPython code for stepping various ways. You can try tweaking the INTERSTEP_DELAY to slow down the motor.

CircuitPython supports 4 different waveform stepping techniques. More on each is detailed at Wikipedia.

  • SINGLE stepping (one coil on at a time) - fast, lowest power usage, weak strength
  • DOUBLE stepping (two coils on at a time) - fast, highest power, high strength
  • INTERLEAVE stepping (alternates between one and two coils on) - slow (half the speed of single or double!), medium power, medium strength
  • MICROSTEPPING - while this is supported its so slow with Crickit we're going to just 'skip' this one!

Unless you have power limiting requirements, DOUBLE is great for most projects. INTERLEAVE gives you smoother motion but is slower. SINGLE is simplest but weakest turning strength.

Download: file
import time
from adafruit_crickit import crickit
from adafruit_motor import stepper

print("Bi-Polar or Uni-Polar Stepper motor demo!")

# make stepper motor a variable to make code shorter to type!
stepper_motor = crickit.stepper_motor
# increase to slow down, decrease to speed up!
INTERSTEP_DELAY = 0.01

while True:
    print("Single step")
    for i in range(200):
        stepper_motor.onestep(direction=stepper.FORWARD)
        time.sleep(INTERSTEP_DELAY)
    for i in range(200):
        stepper_motor.onestep(direction=stepper.BACKWARD)
        time.sleep(INTERSTEP_DELAY)

    print("Double step")
    for i in range(200):
        stepper_motor.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)
        time.sleep(INTERSTEP_DELAY)
    for i in range(200):
        stepper_motor.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
        time.sleep(INTERSTEP_DELAY)
    print("Interleave step")
    for i in range(200):
        stepper_motor.onestep(direction=stepper.FORWARD, style=stepper.INTERLEAVE)
        time.sleep(INTERSTEP_DELAY)
    for i in range(200):
        stepper_motor.onestep(direction=stepper.BACKWARD, style=stepper.INTERLEAVE)
        time.sleep(INTERSTEP_DELAY)

CircuitPython stepper motor control is pretty simple - you can access the motor port for stepper control via the crickit.stepper_motor object (it's an adafruit_motor.stepper type object).

With that object, you can call onestep() to step once, with the direction and stepping style included. The default direction is FORWARD and the default style is SINGLE.

Note that 'forward' and 'backward' are, like DC motors, dependent on your wiring and coil order so you can flip around the coil wiring if you want to change what direction 'forward' and 'backward' means.

Putting time.sleep()'s between steps will let you slow down the stepper motor, however most steppers are geared so you may not want any delays.

Uni-Polar Only Drive Port

The Drive port can also control steppers although it can only do uni-polar! Don't try connecting a 4-wire bi-polar stepper, it won't work at all.

If you are using our 5V uni-polar stepper, wire in this order: red (5V), orange, yellow, pink, blue. That should line up with the wires on the plug

And here's the CircuitPython code. Note that the only difference is we're using the crickit.drive_stepper_motor object now!

Download: file
import time
from adafruit_crickit import crickit
from adafruit_motor import stepper

print("Uni-Polar Stepper motor demo!")

# make stepper motor a variable to make code shorter to type!
stepper_motor = crickit.drive_stepper_motor  # Use the drive port

# increase to slow down, decrease to speed up!
INTERSTEP_DELAY = 0.02
while True:
    print("Single step")
    for i in range(200):
        stepper_motor.onestep(direction=stepper.FORWARD)
        time.sleep(INTERSTEP_DELAY)
    for i in range(200):
        stepper_motor.onestep(direction=stepper.BACKWARD)
        time.sleep(INTERSTEP_DELAY)

    print("Double step")
    for i in range(200):
        stepper_motor.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)
        time.sleep(INTERSTEP_DELAY)
    for i in range(200):
        stepper_motor.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
        time.sleep(INTERSTEP_DELAY)
    print("Interleave step")
    for i in range(200):
        stepper_motor.onestep(direction=stepper.FORWARD, style=stepper.INTERLEAVE)
        time.sleep(INTERSTEP_DELAY)
    for i in range(200):
        stepper_motor.onestep(direction=stepper.BACKWARD, style=stepper.INTERLEAVE)
        time.sleep(INTERSTEP_DELAY)
This guide was first published on Dec 14, 2018. It was last updated on Dec 14, 2018. This page (CircuitPython Steppers) was last updated on Jun 24, 2019.