After finishing up the onboard LED blink program, the next I did was to connect an LED and a switch as shown below

The LED is connected to GPIO 15 and switch to GPIO 14. Please run the code below
# import machine, imports the Pico's functions and definitions
import machine
# imports time based functions
import utime
# Define the external LED, This LED is connected along with a limiting resistor on pin 15
led_external = machine.Pin(15, machine.Pin.OUT)
# Define the Button with Internal pulldown, defaults value to 0
button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
# If the value turns to 1, then its considered pressed
# The 0.5 Seconds valie worked for me
while True:
if button.value() == 1:
led_external.toggle()
utime.sleep(0.5)
For me the switch debounce worked ok at about half a second. You will have to tweak that value.
Every time the switch is pressed, the LED changes state. Fairly straightforward 🙂