from machine import Pin
import time
# Pin definitions
btn_pin = 4 # GPIO pin for the button
led_pin = 12 # GPIO pin for the LED
# Set up pins
button = Pin(btn_pin, Pin.IN, Pin.PULL_UP) # Set button pin as input with pull-up resistor
led = Pin(led_pin, Pin.OUT) # Set LED pin as output
# If button is pushed, light up LED
try:
while True:
if button.value() == 0: # Button pressed (active low)
led.value(1) # Turn LED on
else: # Button released
led.value(0) # Turn LED off
time.sleep(0.1) # Small delay to debounce the button
except KeyboardInterrupt:
print("Program stopped.")