Raspberry Pi Pico - Button - Debounce

When you program the Raspberry Pi Pico to notice when a button is pressed, you might see that one press is read many times. This is because the button can flicker quickly between ON and OFF due to its design. This flickering is known as "chattering." Chattering can make it look like the button was pressed multiple times, which might cause mistakes in some programs. This guide shows how to solve this problem, a method called debouncing the button.

Raspberry Pi Pico chattering phenomenon

We will explore five different examples below and explain the differences between them:

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

Explore how buttons work, including their layout, usage, and how to program them, by checking out these guides.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Button

This image is created using Fritzing. Click to enlarge image

Let's look at and compare the Raspberry Pi Pico code without and with debounce, and see how they behave.

Raspberry Pi Pico - Button without Debounce

Let's first examine the code without debouncing to understand how it works.

""" 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-debounce """ 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

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.
  • Hold the button down for a few seconds and then let 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 pressed The button is pressed The button is released The button is released
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

You pressed and released the button just one time, but the Raspberry Pi Pico thinks it was done many times.

Raspberry Pi Pico - Button with Debounce

""" 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-debounce """ from machine import Pin import utime BUTTON_PIN = 0 # The Raspberry Pi Pico pin connected to the button (GP0) DEBOUNCE_TIME = 100 # debounce time in milliseconds # 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") utime.sleep_ms(DEBOUNCE_TIME) # Check if button was pressed if prev_button_state == 1 and button_state == 0: print("The button is pressed") utime.sleep_ms(DEBOUNCE_TIME) # Save the current state as the previous state for the next loop iteration prev_button_state = button_state

Detailed Instructions

  • Copy the above MicroPython code and paste it into Thonny's editor.
  • Save the code to your Raspberry Pi Pico.
  • Click the green Run button (or press F5) to execute the script.
  • Press the button for a few seconds and then release it.
  • Check out the output 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 ≡

You clicked the button one time, and the Raspberry Pi Pico recognized it accurately as one press and release, without any extra noise.

※ NOTE THAT:

Different applications use different DEBOUNCE_TIME values. Each application may have a specific value.

The above code uses utime.sleep_ms() to debounce the button. This function blocks Raspberry Pico from doing other tasks during the sleep time. Next, we'll look at another example that shows how to debounce without stopping other tasks.

Raspberry Pi Pico - Button with non-blocking Debounce

""" 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-debounce """ from machine import Pin import time BUTTON_PIN = 0 # The Raspberry Pi Pico pin connected to the button (GP0) DEBOUNCE_TIME = 100 # debounce time in milliseconds # Setup the button pin with internal pull-up resistor button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP) # Initialize variables to track the debounce status last_steady_state = button.value() last_flickerable_state = button.value() last_debounce_time = time.ticks_ms() while True: current_state = button.value() current_time = time.ticks_ms() # Check if the current state is different from the last flickerable state if current_state != last_flickerable_state: # Reset the debounce timer last_debounce_time = current_time # Update the last flickerable state last_flickerable_state = current_state # Check if the debounce period has passed since the last state change if time.ticks_diff(current_time, last_debounce_time) > DEBOUNCE_TIME: # If the steady state is different from the current state after the debounce time, update it if current_state != last_steady_state: if last_steady_state == 1 and current_state == 0: print("The button is pressed") elif last_steady_state == 0 and current_state == 1: print("The button is released") # Update the steady state last_steady_state = current_state # Short delay to prevent the loop from running too quickly time.sleep(0.01)

Raspberry Pi Pico - Debounce Using a Library

Debouncing code can seem complex, especially when using multiple buttons. Luckily, there's an easier method for beginners who need to manage several buttons. This method uses a library called DIYables_MicroPython_Button. You can learn more about the DIYables_MicroPython_Button library here.

Let's see some example codes.

Raspberry Pi Pico Button Debounce Code for A Single Button

""" 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-debounce """ from DIYables_MicroPython_Button import Button import time # Initialize buttons connected to GPIO pins GP0 button = Button(0) button.set_debounce_time(100) # Set debounce time to 100 milliseconds while True: button.loop() if button.is_pressed(): print("The button is pressed") if button.is_released(): print("The button is released")
Detailed Instructions
  • On Thonny IDE, navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-Button”, then find the Button library created by DIYables.
  • Click on DIYables-MicroPython-Button, then click Install button to install Button library.
Raspberry Pi Pico Button library
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico.
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Press the button.
  • Check out the message in the Shell at the bottom of Thonny.

Raspberry Pi Pico Button Debounce Code for A Multiple Buttons

Let's debounce for 3 buttons. Here is the wiring diagram between Raspberry Pi Pico and three buttons:

The wiring diagram between Raspberry Pi and Pico Button Library

This image is created using Fritzing. Click to enlarge image

""" 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-debounce """ from DIYables_MicroPython_Button import Button import time # Initialize buttons connected to GPIO pins 7, 8, and 9 button_1 = Button(7) button_2 = Button(8) button_3 = Button(9) button_1.set_debounce_time(100) # Set debounce time to 100 milliseconds button_2.set_debounce_time(100) # Set debounce time to 100 milliseconds button_3.set_debounce_time(100) # Set debounce time to 100 milliseconds while True: button_1.loop() button_2.loop() button_3.loop() if button_1.is_pressed(): print("The button 1 is pressed") if button_1.is_released(): print("The button 1 is released") if button_2.is_pressed(): print("The button 2 is pressed") if button_2.is_released(): print("The button 2 is released") if button_3.is_pressed(): print("The button 3 is pressed") if button_3.is_released(): print("The button 3 is released")

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!