from machine import Pin
from time import sleep
# Define pins connected to the stepper driver
step_pin = Pin(22, Pin.OUT) # Pin to send step pulses
dir_pin = Pin(27, Pin.OUT) # Pin to control direction
# Function to move the stepper motor
def move_stepper(steps, direction):
dir_pin.value(direction) # Set direction (1 for one direction, 0 for the other)
for _ in range(steps):
step_pin.value(1) # Send a pulse
sleep(0.001) # Short delay for the step to take effect
step_pin.value(0) # Turn off the pulse
sleep(0.001) # Delay between steps
# Example usage
try:
while True:
print("Moving forward")
move_stepper(2, 1) # Move 200 steps in one direction
sleep(1) # Wait for a second
# Wait for a second
except KeyboardInterrupt:
print("Stopping motor")