CircuitPython for Stepper Motors

You can learn the basics of CircuitPython with Circuit Playground Express here.

With the special Crickit support in CircuitPython it saves you tons of code and makes it really fast to get started. 

For in-depth information on using stepper motors with CircuitPython, see this page

Only the Feather Crickit, Crickit HAT and the Crickit for Circuit Playground Express support CircuitPython, the micro:bit does not support CircuitPython at present. There is no Crickit support in programming the micro:bit in MicroPython which is different than CircuitPython.

Running the Stepper Motor Forward and Backwards

Here is a simple CircuitPython program that will run one stepper connected to the Motor block (as wired at the top of this page). The stepper is moved in one direction 200 steps, wait a second, and then the opposite direction 200 steps.

10 milliseconds is the least amount of time you should use between steps. If you go much lower, the driver may miss steps. Of course the time can be longer for a slower speed.

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

# Step motor forward for a second and then backward a second & repeat
while True:
    for i in range(400):
    	crickit.stepper_motor.onestep(direction=stepper.FORWARD)
        time.sleep(0.010)  # minimum sleep between steps
    time.sleep(1.0)        # wait a second
    for i in range(400):
        crickit.stepper_motor.onestep(direction=stepper.BACKWARD)
    	time.sleep(0.010)
    time.sleep(1.0)

Running a Stepper Motor Continuously

A stepper can run like a very slow continuous DC motor or continuous servo. The following code runs a stepper at maximum speed in one direction. Change stepper.FORWARD to stepper.BACKWARD to change the direction of the movement.

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

# Step motor in one direction forever
while True:
    crickit.stepper_motor.onestep(direction=stepper.FORWARD)
    time.sleep(0.010)  # minimum sleep between steps

You will note the maximum speed, especially for the blue stepper (which is geared), is very slow!

Using the Drive Terminals for Unipolar Steppers

The Drive terminal block on Crickit can be used to control only unipolar steppers. 

Here are the same two code samples as listed above. For Circuit Playground Express with Crickit, instead of using crickit.stepper_motor, the code uses crickit.drive_stepper_motor.

For a Feather with Crickit, instead of the original crickit.stepper_motor, use crickit.feather_drive_stepper_motor.

The following uses the changes for Circuit Playground Express - make the name change for Feather noted above if you have a Feather + Crickit.

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

# Step motor forward for a second and then backward a second & repeat
while True:
    for i in range(400):
    	crickit.drive_stepper_motor.onestep(direction=stepper.FORWARD)
        time.sleep(0.010)  # minimum sleep between steps
    time.sleep(1.0)        # wait a second
    for i in range(400):
        crickit.drive_stepper_motor.onestep(direction=stepper.BACKWARD)
    	time.sleep(0.010)
    time.sleep(1.0)

And the continuous stepper example for the Drive port:

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

# Step motor in one direction forever
while True:
    crickit.drive_stepper_motor.onestep(direction=stepper.FORWARD)
    time.sleep(0.010)  # minimum sleep between steps

Have fun integrating a stepper motor into your projects!

This guide was first published on Jul 04, 2018. It was last updated on Jul 04, 2018.
This page (CircuitPython) was last updated on Sep 17, 2020.