Simple test

Ensure your device works with this simple test.

examples/stspin_simpletest.py
 1# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4
 5"""
 6Basic example for the Adafruit STSPIN220 stepper motor driver library.
 7"""
 8
 9import time
10
11import board
12
13import adafruit_stspin
14
15STEPS_PER_REVOLUTION = 200
16
17DIR_PIN = board.D5
18STEP_PIN = board.D6
19
20# Create stepper object with full pin configuration
21# Defaults to 1/16 microsteps
22motor = adafruit_stspin.STSPIN(STEP_PIN, DIR_PIN, STEPS_PER_REVOLUTION)
23
24# Set the speed to 60 RPM
25motor.speed = 60
26
27print(f"Microstepping mode set to 1/{motor.microsteps_per_step} at {motor.speed} RPM")
28
29while True:
30    # Calculate total microsteps for one full revolution
31    total_microsteps = STEPS_PER_REVOLUTION * motor.microsteps_per_step
32
33    print(f"Stepping forward one revolution ({total_microsteps} microsteps)...")
34    motor.step(total_microsteps)
35    time.sleep(1.0)
36
37    print(f"Stepping backward one revolution ({total_microsteps} microsteps)...")
38    motor.step(-total_microsteps)
39    time.sleep(1.0)

Microstep mode test

Cycle through setting the different microstep modes

examples/stspin_microsteps.py
  1# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
  2#
  3# SPDX-License-Identifier: MIT
  4
  5"""
  6Microstepping mode test for the Adafruit STSPIN220 stepper motor driver.
  7"""
  8
  9import time
 10
 11import board
 12
 13import adafruit_stspin
 14
 15# Define the number of steps per revolution for your stepper motor
 16# Most steppers are 200 steps per revolution (1.8 degrees per step)
 17STEPS_PER_REVOLUTION = 200
 18
 19DIR_PIN = board.D5  # DIRection pin
 20STEP_PIN = board.D6  # STEP pin
 21MODE1_PIN = board.D9  # Mode 1 pin (REQUIRED for mode switching)
 22MODE2_PIN = board.D10  # Mode 2 pin (REQUIRED for mode switching)
 23EN_FAULT_PIN = board.D11  # Enable/Fault pin (optional)
 24STBY_RESET_PIN = board.D12  # Standby/Reset pin (REQUIRED for mode switching)
 25
 26print("Initializing STSPIN220...")
 27motor = adafruit_stspin.STSPIN(
 28    STEP_PIN,
 29    DIR_PIN,
 30    STEPS_PER_REVOLUTION,
 31    mode1_pin=MODE1_PIN,
 32    mode2_pin=MODE2_PIN,
 33    en_fault_pin=EN_FAULT_PIN,
 34    stby_reset_pin=STBY_RESET_PIN,
 35)
 36
 37print("Adafruit STSPIN220 Microstepping Mode Test")
 38print("=" * 50)
 39
 40# Define all available modes with their names for display
 41MODES = [
 42    (adafruit_stspin.Modes.STEP_FULL, "Full Step"),
 43    (adafruit_stspin.Modes.STEP_1_2, "1/2 Step"),
 44    (adafruit_stspin.Modes.STEP_1_4, "1/4 Step"),
 45    (adafruit_stspin.Modes.STEP_1_8, "1/8 Step"),
 46    (adafruit_stspin.Modes.STEP_1_16, "1/16 Step"),
 47    (adafruit_stspin.Modes.STEP_1_32, "1/32 Step"),
 48    (adafruit_stspin.Modes.STEP_1_64, "1/64 Step"),
 49    (adafruit_stspin.Modes.STEP_1_128, "1/128 Step"),
 50    (adafruit_stspin.Modes.STEP_1_256, "1/256 Step"),
 51]
 52
 53BASE_RPM = 60  # Base speed for full step mode
 54
 55while True:
 56    for mode, mode_name in MODES:
 57        print(f"\nTesting {mode_name} mode...")
 58
 59        try:
 60            # Set the microstepping mode
 61            motor.step_mode = mode
 62
 63            # Get the number of microsteps for this mode
 64            microsteps = motor.microsteps_per_step
 65            motor.speed = BASE_RPM
 66
 67            # Calculate total steps needed for one full revolution
 68            total_steps = STEPS_PER_REVOLUTION * microsteps
 69            print(f"  Speed: {motor.speed} RPM")
 70            print(f"  Microsteps per full step: {microsteps}")
 71            print(f"  Steps for full revolution: {total_steps}")
 72
 73            # Check for any faults before moving
 74            if motor.fault:
 75                print("  WARNING: Fault detected! Clearing...")
 76                motor.clear_fault()
 77                time.sleep(0.1)
 78
 79            # Perform one full revolution forward
 80            print(f"  Rotating forward 360°...")
 81            start_time = time.monotonic()
 82            motor.step(total_steps)
 83            rotation_time = time.monotonic() - start_time
 84            print(f"  Rotation completed in {rotation_time:.2f} seconds")
 85
 86            # Brief pause to see the position
 87            time.sleep(0.5)
 88
 89            # Return to starting position
 90            print(f"  Returning to start position...")
 91            motor.step(-total_steps)
 92
 93            print(f"  {mode_name} test complete!")
 94
 95        except ValueError as e:
 96            print(f"  ERROR: Could not set {mode_name} mode - {e}")
 97            print("  Make sure MODE1, MODE2, and STBY/RESET pins are connected!")
 98
 99        # Pause between modes
100        time.sleep(1.0)
101
102    print("\n" + "=" * 50)
103    print("All modes tested! Starting next cycle in 3 seconds...")
104    print("=" * 50)
105    time.sleep(3.0)