from machine import Pin
import time
# Define GPIO pins for STEP and DIR
STEP_PIN = 16 # GPIO16 for STEP
DIR_PIN = 17 # GPIO17 for DIR
# Set up the GPIO pins
step = Pin(STEP_PIN, Pin.OUT)
direction = Pin(DIR_PIN, Pin.OUT)
# Function to move stepper motor steps in a given direction
def move_stepper(steps, delay, direction_value):
direction.value(direction_value) # Set the direction (1: CW, 0: CCW)
for _ in range(steps):
step.high()
time.sleep_us(delay) # Short delay for the pulse
step.low()
time.sleep_us(delay) # Short delay between pulses
# Main loop to rotate the motor
try:
while True:
# Rotate clockwise 200 steps (one full rotation if motor is 1.8°/step)
move_stepper(200, 1000, 1)
time.sleep(1) # Wait 1 second
# Rotate counterclockwise 200 steps
move_stepper(200, 1000, 0)
time.sleep(1) # Wait 1 second
except KeyboardInterrupt:
pass