from machine import Pin
import time
relay = Pin(19, Pin.OUT)
button = Pin(8, Pin.IN, Pin.PULL_UP)
state = False
last_button = 1
# tắt relay ban đầu (active LOW)
relay.value(1)
while True:
current_button = button.value()
# phát hiện nhấn (1 → 0)
if last_button == 1 and current_button == 0:
state = not state
# relay active LOW
relay.value(0 if state else 1)
last_button = current_button
time.sleep(0.05)