Raspberry Pi Pico - Button - LED

This guide shows you how to control an LED using a Raspberry Pi Pico and a button. We will explore two methods to achieve this.

Application 1 - The LED matches the button's status:

Application 2 - Each time you press the button, the LED switches its state.

For Application 2, it is necessary to debounce the button to ensure it functions properly. We will understand its importance by observing the behavior of the LED with and without debouncing in the Raspberry Pi Pico code.

Raspberry Pi Pico control LED

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Push Button
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LED and Button

If you need help understanding LED and button (including how they are arranged, how they work, and how to program them), the tutorials below may be useful:

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Button LED

This image is created using Fritzing. Click to enlarge image

Application 1 - The LED follows the button's state

Raspberry Pi Pico Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-button-led """ from machine import Pin import time # Define the pins BUTTON_PIN = 2 # The Raspberry Pi Pico pin connected to the button (GP2) LED_PIN = 13 # The Raspberry Pi Pico pin connected to the LED (GP13) # Initialize the pins button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP) # set Pico pin to input pull-up mode led = Pin(LED_PIN, Pin.OUT) # set Pico pin to output mode while True: button_state = button.value() # read new state if button_state == 0: print("The button is being pressed") led.value(1) # turn on the LED else: print("The button is unpressed") led.value(0) # turn off the LED time.sleep(0.1) # small delay to debounce the button

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Wire the components according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Press the button and hold it for a few seconds.
  • Check out the message in the Shell at the bottom of Thonny.
  • Observe the change in the LED's status.

The LED status matches the button status.

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Code Explanation

Read the detailed explanations in the source code's comments for each line!

Application 2 - The LED changes its state each time the button is pressed

Raspberry Pi Pico Code - Button Toggles LED Without Debouncing

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-button-led """ from machine import Pin import time # Define the pins BUTTON_PIN = 2 # The Raspberry Pi Pico pin connected to the button (GP2) LED_PIN = 13 # The Raspberry Pi Pico pin connected to the LED (GP13) # Initialize the pins button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP) # set Pico pin to input pull-up mode led = Pin(LED_PIN, Pin.OUT) # set Pico pin to output mode # Initialize the states led_state = 0 button_state = button.value() prev_button_state = button_state while True: prev_button_state = button_state # save the last state button_state = button.value() # read new state if prev_button_state == 1 and button_state == 0: print("The button is pressed") # toggle state of LED led_state = not led_state # control LED according to the toggled state led.value(led_state) time.sleep(0.1) # small delay to debounce the button

Detailed Instructions

  • Copy the code and open it in Thonny IDE.
  • Upload the code to the Raspberry Pi Pico.
  • Press and release the button multiple times.
  • Observe how the LED's state changes.

Each time you press the button, the LED state changes once.

Code Explanation

You can find the explanation in the comments of the above Raspberry Pi Pico code.

In the code, led_state = not led_state does the same thing as this code:

if led_state == 1: # if the LED is currently on led_state = 0 # turn it off else: # if the LED is currently off led_state = 1 # turn it on

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!