It's easy to use the STSPIN220 Stepper Motor Driver with CircuitPython and the Adafruit_CircuitPython_STSPIN helper module. This module allows you to easily write Python code to control the breakout with a few GPIO pins.
You can use the example code with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
CircuitPython Microcontroller Wiring
Here is how you'll wire the breakout to a Feather RP2040 and stepper motor:
Check your stepper motor wiring - your motor may have different wire colors or wire order.
- Stepper motor power supply positive to breakout terminal block + (red wire)
- Stepper motor power supply negative to breakout terminal block - (black wire)
- Breakout VDD to Feather 3.3V (red wire)
- Breakout GND to Feather GND (black wire)
- Breakout DIR to Feather pin 5 (blue wire)
- Breakout STEP to Feather pin 6 (orange wire)
- Breakout MS1 to Feather pin 9 (green wire)
- Breakout MS2 to Feather pin 10 (grey wire)
- Breakout EN to Feather pin 11 (pink wire)
- Breakout RST to Feather pin 12 (cyan wire)
- Breakout 1A to stepper motor coil 1 positive (green wire)
- Breakout 2A to stepper motor coil 1 negative (yellow wire)
- Breakout 1B to stepper motor coil 2 positive (red wire)
- Breakout 2B to stepper motor coil 2 negative (black wire)
Python Computer Wiring
Since there are dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired to the breakout and a stepper motor:
Check your stepper motor wiring - your motor may have different wire colors or wire order.
- Stepper motor power supply positive to breakout terminal block + (red wire)
- Stepper motor power supply negative to breakout terminal block - (black wire)
- Breakout VDD to Pi 3.3V (red wire)
- Breakout GND to Pi GND (black wire)
- Breakout DIR to Pi pin 5 (blue wire)
- Breakout STEP to Pi pin 6 (orange wire)
- Breakout MS1 to Pi pin 9 (green wire)
- Breakout MS2 to Pi pin 10 (grey wire)
- Breakout EN to Pi pin 11 (pink wire)
- Breakout RST to Pi pin 12 (cyan wire)
- Breakout 1A to stepper motor coil 1 positive (green wire)
- Breakout 2A to stepper motor coil 1 negative (yellow wire)
- Breakout 1B to stepper motor coil 2 positive (red wire)
- Breakout 2B to stepper motor coil 2 negative (black wire)
Python Installation of STSPIN Library
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
Once that's done, from your command line run the following command:
pip3 install adafruit-circuitpython-stspin
If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_STSPIN library into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following file:
- adafruit_stspin.mpy
Python Usage
Once you have the library pip3 installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:
python3 code.py
# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Basic example for the Adafruit STSPIN220 stepper motor driver library.
"""
import time
import board
import adafruit_stspin
STEPS_PER_REVOLUTION = 200
DIR_PIN = board.D5
STEP_PIN = board.D6
# Create stepper object with full pin configuration
# Defaults to 1/16 microsteps
motor = adafruit_stspin.STSPIN(STEP_PIN, DIR_PIN, STEPS_PER_REVOLUTION)
# Set the speed to 60 RPM
motor.speed = 60
print(f"Microstepping mode set to 1/{motor.microsteps_per_step} at {motor.speed} RPM")
while True:
# Calculate total microsteps for one full revolution
total_microsteps = STEPS_PER_REVOLUTION * motor.microsteps_per_step
print(f"Stepping forward one revolution ({total_microsteps} microsteps)...")
motor.step(total_microsteps)
time.sleep(1.0)
print(f"Stepping backward one revolution ({total_microsteps} microsteps)...")
motor.step(-total_microsteps)
time.sleep(1.0)
In the simple test, only the direction and step pins are used to rotate the stepper motor clockwise and counter clockwise at the default 1/16 microsteps in the loop. You'll see your stepper motor rotate back and forth with a 1 second pause between each turn.
# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Microstepping mode test for the Adafruit STSPIN220 stepper motor driver.
"""
import time
import board
import adafruit_stspin
# Define the number of steps per revolution for your stepper motor
# Most steppers are 200 steps per revolution (1.8 degrees per step)
STEPS_PER_REVOLUTION = 200
DIR_PIN = board.D5 # DIRection pin
STEP_PIN = board.D6 # STEP pin
MODE1_PIN = board.D9 # Mode 1 pin (REQUIRED for mode switching)
MODE2_PIN = board.D10 # Mode 2 pin (REQUIRED for mode switching)
EN_FAULT_PIN = board.D11 # Enable/Fault pin (optional)
STBY_RESET_PIN = board.D12 # Standby/Reset pin (REQUIRED for mode switching)
print("Initializing STSPIN220...")
motor = adafruit_stspin.STSPIN(
STEP_PIN,
DIR_PIN,
STEPS_PER_REVOLUTION,
mode1_pin=MODE1_PIN,
mode2_pin=MODE2_PIN,
en_fault_pin=EN_FAULT_PIN,
stby_reset_pin=STBY_RESET_PIN,
)
print("Adafruit STSPIN220 Microstepping Mode Test")
print("=" * 50)
# Define all available modes with their names for display
MODES = [
(adafruit_stspin.Modes.STEP_FULL, "Full Step"),
(adafruit_stspin.Modes.STEP_1_2, "1/2 Step"),
(adafruit_stspin.Modes.STEP_1_4, "1/4 Step"),
(adafruit_stspin.Modes.STEP_1_8, "1/8 Step"),
(adafruit_stspin.Modes.STEP_1_16, "1/16 Step"),
(adafruit_stspin.Modes.STEP_1_32, "1/32 Step"),
(adafruit_stspin.Modes.STEP_1_64, "1/64 Step"),
(adafruit_stspin.Modes.STEP_1_128, "1/128 Step"),
(adafruit_stspin.Modes.STEP_1_256, "1/256 Step"),
]
BASE_RPM = 60 # Base speed for full step mode
while True:
for mode, mode_name in MODES:
print(f"\nTesting {mode_name} mode...")
try:
# Set the microstepping mode
motor.step_mode = mode
# Get the number of microsteps for this mode
microsteps = motor.microsteps_per_step
motor.speed = BASE_RPM
# Calculate total steps needed for one full revolution
total_steps = STEPS_PER_REVOLUTION * microsteps
print(f" Speed: {motor.speed} RPM")
print(f" Microsteps per full step: {microsteps}")
print(f" Steps for full revolution: {total_steps}")
# Check for any faults before moving
if motor.fault:
print(" WARNING: Fault detected! Clearing...")
motor.clear_fault()
time.sleep(0.1)
# Perform one full revolution forward
print(f" Rotating forward 360°...")
start_time = time.monotonic()
motor.step(total_steps)
rotation_time = time.monotonic() - start_time
print(f" Rotation completed in {rotation_time:.2f} seconds")
# Brief pause to see the position
time.sleep(0.5)
# Return to starting position
print(f" Returning to start position...")
motor.step(-total_steps)
print(f" {mode_name} test complete!")
except ValueError as e:
print(f" ERROR: Could not set {mode_name} mode - {e}")
print(" Make sure MODE1, MODE2, and STBY/RESET pins are connected!")
# Pause between modes
time.sleep(1.0)
print("\n" + "=" * 50)
print("All modes tested! Starting next cycle in 3 seconds...")
print("=" * 50)
time.sleep(3.0)
In the microstep mode test, all of the pins are used to set the different microstep modes on the STSPIN220. In the loop, the different microstep modes are cycled. When a new mode is initiated, the stepper motor makes a full rotation clockwise and then counter clockwise. In the serial console, you'll see output from this process printed out for each microstep mode.
Page last edited June 24, 2025
Text editor powered by tinymce.