from machine import Pin
import time
# Define the GPIO pins for the stepper motor control
STEP_PIN_1 = 15 # A-
STEP_PIN_2 = 2 # A+
STEP_PIN_3 = 0 # B+
STEP_PIN_4 = 4 # B-
# Define step sequence for the stepper motor
step_sequence = [
[1, 0, 0, 1], # A-, B-
[1, 1, 0, 0], # A-, B+
[0, 1, 0, 0], # A+, B+
[0, 1, 1, 1] # A+, B-
]
# Create Pin objects for stepper control
pins = [Pin(pin, Pin.OUT) for pin in (STEP_PIN_1, STEP_PIN_2, STEP_PIN_3, STEP_PIN_4)]
def step_motor(steps, delay_ms=10):
num_steps = abs(steps)
direction = 1 if steps > 0 else -1
for _ in range(num_steps):
for step in step_sequence[::direction]: # Reverse sequence if moving backwards
for pin, state in zip(pins, step):
pin.value(state)
time.sleep_ms(delay_ms)
def main():
while True:
try:
steps = int(input("Enter number of steps (positive or negative): "))
step_motor(steps)
except ValueError:
print("Invalid input. Please enter an integer.")
if __name__ == "__main__":
main()