from machine import Pin
from time import sleep_us, sleep
# Define the pins connected to STEP and DIR on the A4988
STEP_PIN = 5 # Replace with your GPIO pin for STEP
DIR_PIN = 4 # Replace with your GPIO pin for DIR
# Initialize the STEP and DIR pins
step_pin = Pin(STEP_PIN, Pin.OUT)
dir_pin = Pin(DIR_PIN, Pin.OUT)
def step_cw(steps, delay=1000):
dir_pin.value(1) # Set direction to clockwise
for i in range(steps):
step_pin.toggle()
sleep_us(delay)
step_pin.toggle()
sleep_us(delay)
def step_acw(steps, delay=1000):
dir_pin.value(0) # Set direction to counterclockwise
for i in range(steps):
step_pin.value(1)
sleep_us(delay)
step_pin.value(0)
sleep_us(delay)
# Example usage
print("Rotating 200 steps clockwise")
step_cw(200) # Move 200 steps clockwise
sleep(1) # Delay between directions
print("Rotating 200 steps counterclockwise")
step_acw(200) # Move 200 steps counterclockwise
sleep(1)