from machine import Pin
import time
relay = Pin(17, Pin.OUT)
button = Pin(16, Pin.IN, Pin.PULL_UP)
motor_state = False
while True:
if button.value() == 0: # Button pressed
motor_state = not motor_state
relay.value(motor_state)
print("Motor ON" if motor_state else "Motor OFF")
# Wait until button released (IMPORTANT)
while button.value() == 0:
pass
time.sleep(0.2) # debounce delay