Overview
In this guide we will learn how to control any LED by pressing a push button or a switch by interfacing them with Raspberry Pi Pico.
The Raspberry Pi Pico detects the level change of the button to determine whether the button was pressed. Press the button to turn on the LED light for the first time, and press the button to turn off the LED light again, so as to realize the function of turning on and off the LED light.
Components Required
In this guide, I used Elecrow Raspberry Pi Pico Starter Kit to test different Modules. You can buy the kit and perform some other operations as well. From this kit, you can use the following components.
1. Raspberry Pi Pico Board -1
2. Push Button Switch – 1
3. LED – 1
4. Breadboard -1
5. Jumper Wires – 4
Push Button Switch
A Push button, also known as a tact switch, is a kind of electronic component. Its internal structure is connected and disconnected by the force change of metal shrapnel. When in use, press the switch, The switch is closed and the circuit is turned on. When released, the switch bounces and the circuit is disconnected.
Circuit Diagram & Hardware Setup
Now let us see how we can Control LED using Push Button Switch by interfacing it with Raspberry Pi Pico Board. Here is the simple connection diagram.
Connect the Push Button Pin to GP0 & its cross pin to GND. Similarly, connect the LED positive Pin to Raspberry Pi Pico GP1 Pin & negative Pin to GND Pin.
MicroPython Code/Program
Copy the following Code to the Thonny Editor.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from machine import Pin from utime import sleep_ms button = Pin(0, Pin.IN, Pin.PULL_UP) #Internal pull-up led = Pin(1, Pin.OUT) State=0 #0 means that the light is currently off if __name__ == '__main__': while True: print(button.value()) if button.value() == 0: #key press if State==0: led.value(1) sleep_ms=100 while button.value() == 0: State=1 else: led.value(0) sleep_ms=100 while button.value()== 0: State=0 |
Run the script and you can start checking the LED State by pressing the button.
When a button is pressed the LED will turn ON.
If the same button is pressed for the second time, the LED state will toggle and LED will turn off.












3 Comments
nice example, this worked like a charm. However im uncertain what difference Pin.PULL_UP makes? initially i didnt use it and than began to test with and without Pin.PULL_UP.
It’s just to stop the pin from floating voltages when there is no contact. If you set the pin as PULL_UP then it defaults to pin.value as 1, and you would have to ground it to change it to a 0. If you set the pin as PULL_DOWN then it’s opposite, value 0 till you feed it a positive voltage. Prevents phantom states 😀 I don’t even know if that’s a term but I think it sounds cool so I’m coining it!
THIS IS TOTALLY WRONG !!
– YOU SHOULD NEVER POWER AN LED DIRECTLY FROM A MCU PIN!!!
– ALL LEDS MUST HAVE A RESISTANCE IN SERIES TO LIMIT THE CURRENT!!!!
– THOSE WHO DON’T KNOW WHAT THEY’RE DOING SHOULD NOT COME TO THE INTERNET TO GIVE BAD EXAMPLES!!!!