from machine import Pin, ADC
import time
# Stepper Motor Pins for A4988
DIR = Pin(26, Pin.OUT) # Direction pin
STEP = Pin(25, Pin.OUT) # Step pin
EN = Pin(27, Pin.OUT) # Enable pin (LOW = ON)
# Input Devices
button = Pin(14, Pin.IN, Pin.PULL_UP) # Button with pull-up
pot = ADC(Pin(34)) # Potentiometer
pot.atten(ADC.ATTN_11DB) # Full range 0–3.3V
pot.width(ADC.WIDTH_10BIT) # 10-bit resolution (0–1023)
# Enable motor driver
EN.value(0)
motor_running = False # Motor ON/OFF toggle
def step_motor(delay, direction=1):
DIR.value(direction)
STEP.value(1)
time.sleep_us(int(delay * 1_000_000)) # high pulse
STEP.value(0)
time.sleep_us(int(delay * 1_000_000)) # low pulse
print("Stepper with Button + Potentiometer Control")
while True:
# Button press → toggle motor state
if button.value() == 0:
motor_running = not motor_running
print("▶ Motor Running" if motor_running else "⏹ Motor Stopped")
time.sleep(0.3) # debounce delay
if motor_running:
# Speed from potentiometer
pot_value = pot.read()
delay = 0.0005 + (pot_value / 1023) * 0.01 # 0.5ms – 10ms
step_motor(delay, direction=1) # CW direction