ESP32 MicroPython Button Debounce

Sometimes, when you connect a button to an ESP32, it might seem like the button is pressed many times when you only press it once. This is because the button can quickly switch between being pressed and not pressed. This is called bouncing, also known as chattering. Bouncing can cause problems in your program.

This guide will show you how to fix this problem for ESP32 by using a technique called debouncing with MicroPython. Debouncing helps the ESP32 recognize a single button press as just one press.

ESP32 MicroPython chattering phenomenon

We will learn though the below steps:

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Push Button
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

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

If you're not yet familiar with button (including their pinout, how it works, interfacing with the ESP32, and writing MicroPython code for the ESP32 to interact with them), you can learn more at:

Wiring Diagram

  • How to connect ESP32 and button using breadboard
The wiring diagram between ESP32 MicroPython Button

This image is created using Fritzing. Click to enlarge image

The wiring diagram between ESP32 MicroPython two-pin push button

This image is created using Fritzing. Click to enlarge image

Let's compare the ESP32 MicroPython code without and with debouncing to see how debouncing affects the behavior.

ESP32 - Button without Debounce

First, let's examine the MicroPython code for ESP32 without debouncing to understand its behavior.

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-button-debounce """ from machine import Pin import time BUTTON_PIN = 21 # The ESP32 pin GPIO21 connected to the button # 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

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the button to the ESP32 as shown in the diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Press and hold the button for a few seconds, then release.
  • Check out the message in the Shell at the bottom of Thonny IDE.
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 (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

As you can see in the log, you pressed the button only once, but the ESP32 detected multiple presses.

ESP32 Debounce for a Button

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-button-debounce """ from DIYables_MicroPython_Button import Button import time # Initialize buttons connected to GPIO pins GPIO21 button = Button(21) 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
  • In 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.
ESP32 MicroPython Button library
  • Copy the given code and paste it into the Thonny IDE's editor.
  • Save the script on your ESP32 board.
  • Click the green Run button (or press F5) to start the script. The script will start running.
  • Press the button.
  • Check out the message in the Shell at the bottom of Thonny IDE.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The button is pressed The button is released
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

You pressed the button once, and the ESP32 correctly identified it as a single push and release, with no additional disturbances.

※ NOTE THAT:

Different apps use different DEBOUNCE_TIME settings. Each app might have its own specific value.

ESP32 Debounce for multiple Buttons

Let's add debouncing to four buttons using ESP32 with MicroPython. Here's how to connect them to your ESP32:

The wiring diagram between ESP32 MicroPython 4 buttons

This image is created using Fritzing. Click to enlarge image

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-button-debounce """ from DIYables_MicroPython_Button import Button import time button_1 = Button(25) # Initialize buttons connected to ESP32 pin GPIO25 button_2 = Button(26) # Initialize buttons connected to ESP32 pin GPIO26 button_3 = Button(27) # Initialize buttons connected to ESP32 pin GPIO27 button_4 = Button(14) # Initialize buttons connected to ESP32 pin GPIO14 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 button_4.set_debounce_time(100) # Set debounce time to 100 milliseconds while True: button_1.loop() button_2.loop() button_3.loop() button_4.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") if button_4.is_pressed(): print("The button 4 is pressed") if button_4.is_released(): print("The button 4 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!