from machine import Pin
from time import sleep #delay for second
from time import sleep_ms #delay for milisecond
# Pins comfiguration setup
LED = Pin(17, Pin.OUT) #LED pin 17 as OUTPUT
SW = Pin(16, Pin.IN) #Switch pin 16 as INPUT and PULL_UP
# Initial state
LED.off() #OFF LED for initial
# Loop
while True:
sleep_ms(10) #short delay
#Switch is pressed
while SW.value()==0: #Checked switch condition PULL_UP type
LED.on() #LED ON if switch is pressed
print("LED ON, SWITCH IS PRESSED") #Print on serial monitor
sleep(1) #Take 1 second sleep
#Switch not pressed
LED.off() #LED OFF if switch is not pressed
print("LED OFF, PRESS SWITCH TO TURN ON LED") #Print on serial monitor
sleep_ms(10) #short delay