Raspberry Pi Pico - Button

The button is a basic but essential part used in many Raspberry Pi Pico projects. It might look complex due to its mechanical and physical features, which might be hard for beginners. This guide is meant to make it easy for beginners to understand. Let's start!

Raspberry Pi Pico button

※ NOTE THAT:

Before we start using a button with Raspberry Pi Pico, please be aware of two typical issues that beginners often face:

  1. The floating input problem:
    • Symptom: The state of the input pin connected to a button on the Raspberry Pi Pico is unstable and does not accurately reflect the button's state.
    • Cause: The button's pin lacks a connection to either a pull-down or a pull-up resistor.
    • Solution: Attach a pull-down or pull-up resistor to the input pin. We will explain this more later in the guide.
  • The chattering phenomenon:
    • Symptom: The code in Raspberry Pi Pico reads the button's state to notice presses by detecting state changes (from HIGH to LOW, or vice versa). But it might register multiple presses with just one actual button press.
    • Cause: The mechanical attributes of the button may cause the input pin’s state to quickly switch back and forth multiple times with a single press.
    • Solution: Use a method called debounce. Details will be provided in a later tutorial on debouncing.

    Note, chattering mainly affects situations where you need to count button presses accurately. It might not be a significant issue in other scenarios.

    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 Button
    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 Button

    The push button, sometimes called a tactile button or a momentary switch, is a type of switch that connects when you press and keep it down, and disconnects when you let it go. There are various kinds of push buttons, mainly categorized into two groups:

    • Push buttons suitable for PCB or breadboard installation
    • Push buttons designed for panel installation
    Raspberry Pi Pico Push button

    Pinout

    The PCB-mount buttons usually have four pins.

    Button Pinout

    These pins are paired internally, so we just need to use two of the four pins that are not joined inside.

    There are four methods to connect the button, as shown in the image, but effectively only two since they are similar.

    How To Use Button

    A button has four pins, but we usually use only two. This helps it stay stable on the board and handle pressure well.

    The panel-mount buttons usually have two pins.

    two-pin push button Pinout
    image source: diyables.io

    How It Works

    • When the button is not pressed, pin A and pin B are disconnected.
    • When the button is pressed, pin A and pin B are connected.
    How Button Works

    Raspberry Pi Pico - Button

    One button pin connects to VCC or GND, while the other pin connects to a pin on the Raspberry Pi Pico.

    We can find out if the button is pressed by checking the input pin state on the Raspberry Pi Pico.

    Button State and Pressing State

    The connection between the button's status and when it is pressed is determined by how we link the button to the Raspberry Pi Pico and the configuration of the Raspberry Pi Pico's pin.

    You can use a button with Raspberry Pi Pico in two ways:

    1. Connect one pin of the button to VCC and the other pin to a Raspberry Pi Pico pin. Include a pull-down resistor in this connection.
      • When the button is pressed, the Raspberry Pi Pico pin becomes HIGH. When not pressed, it is LOW.
      • You must use an extra resistor that you add yourself.
  • Connect one pin of the button to GNRD and the other pin to a Raspberry Pi Pico pin. Include a pull-up resistor in this connection.
    • When the button is pressed, the Raspberry Pi Pico pin becomes LOW. When not pressed, it is HIGH.
    • You may use a built-in resistor (internal) or add one yourself (external). To use the built-in resistor, set it up using Raspberry Pi Pico's code.

    ※ NOTE THAT:

    Without a pull-down or pull-up resistor, the input pin is "floating" when the button is not pressed, causing the pin's state to unpredictably switch to HIGH or LOW, leading to inaccurate readings.

    • Poor practice: Configure the Raspberry Pi Pico pin as an input with button = Pin(BUTTON_PIN, Pin.IN) and no pull-down or pull-up resistor.
    • Good practice: Configure the Raspberry Pi Pico pin with an internal pull-up resistor using button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP). This eliminates the need for an external resistor.

    This guide shows you the easiest way to start: we use a simple method where you set up a Raspberry Pi Pico pin with an internal pull-up input. This way, you don't need an external resistor. Beginners should just use the Raspberry Pi Pico code we provide.

    Wiring Diagram

    • Wiring guide for Raspberry Pi Pico and a PCB-mounted button
    The wiring diagram between Raspberry Pi and Pico Button

    This image is created using Fritzing. Click to enlarge image

    • Connect the Raspberry Pi Pico to a panel-mount button following this guide:
    The wiring diagram between Raspberry Pi and Pico two-pin push button

    This image is created using Fritzing. Click to enlarge image

    How To Program For Button

    • Configures pin of the Raspberry Pi Pico for use as an input with an internal pull-up resistor, employing the pinMode() function.
    button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
    • Checks the Raspberry Pi Pico pin state.
    button_state = button.value()

    ※ NOTE THAT:

    There are two common situations:

    • The first: When the input is HIGH, do something. When the input is LOW, do the opposite.
    • The second: When the input changes from LOW to HIGH (or from HIGH to LOW), do something.

    Depending on the goal, we pick one of these options. For example, when using a button to control a light (LED):

    • If we need the light to turn ON when pressing the button and turn OFF when not pressing it, we should use the first situation.
    • If we want the light to toggle ON and OFF each time the button is pressed, we should use the second situation.

    Raspberry Pi Pico Code - Reading the state of button

    from machine import Pin import time BUTTON_PIN = 0 # The Raspberry Pi Pico pin connected to the button (GP0) # Set up the button pin. The Pin.PULL_UP enables the internal pull-up resistor. button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP) while True: # Read the state of the button button_state = button.value() # Print the button's state print(button_state) # Wait for half a second before reading the button again time.sleep(0.5)

    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.
    • Connect the button to the Raspberry Pi Pico 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.
    • Check out the message in the Shell at the bottom of Thonny.
    Shell x
    >>> %Run -c $EDITOR_CONTENT
    MPY: soft reboot 1 1 1 0 0 0 0 0 0 1 1 1
    MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

    1 means not pressed, 0 means pressed.

    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

    You can find the explanation in the comments section of the Raspberry Pi Pico code provided earlier.

    Raspberry Pi Pico Code - Detecting the button's press and release event

    Let's modify the code so it can detect when buttons are pressed and released.

    """ 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 """ from machine import Pin import time BUTTON_PIN = 0 # The Raspberry Pi Pico pin connected to the button (GP0) # Setup the button pin. The Pin.PULL_UP enables the internal pull-up resistor. button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP) # Initialize the previous state of the button prev_button_state = 1 # Assuming the button is unpressed initially while True: # Read the current state of the button button_state = button.value() # Check if button was released if prev_button_state == 0 and button_state == 1: print("The button is released") # Check if button was pressed if prev_button_state == 1 and button_state == 0: print("The button is pressed") # Save the current state as the previous state for the next loop iteration prev_button_state = button_state

    Detailed Instructions

    • Copy the code above and paste it into Thonny IDE.
    • Click the green Run button (or press F5) to run the script. The script will execute.
    • Press the button and then let it go.
    • Check out the message in the Shell at the bottom of Thonny.
    Shell x
    >>> %Run -c $EDITOR_CONTENT
    MPY: soft reboot The button is pressed The button is released
    MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

    ※ NOTE THAT:

    • Sometimes, when you press and release a button once, the Thonny's Shell may show that it happened many times. This common issue is called "chattering phenomenon." More information and solution is available in the Raspberry Pi Pico - Button Debounce tutorial.

    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!