Arduino MicroPython Button Debounce

When you connect a button to an Arduino, it may sometimes appear as if the button has been pressed multiple times, even though you only pressed it once. This happens because the button can rapidly switch between pressed and unpressed states, a phenomenon known as bouncing or chattering. Bouncing can create issues in your Arduino program.

This guide will teach you how to resolve this problem on the Arduino by using a technique called debouncing with MicroPython. Debouncing ensures that the Arduino interprets each button press as a single, distinct action.

Arduino MicroPython Button Debounce

We will go through the following steps:

Hardware Preparation

1×Arduino Giga R1 WiFi
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 Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Mega/Giga
1×(Recommended) Enclosure For Arduino Giga
1×(Recommended) Power Splitter For Arduino Giga

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 Arduino, and writing MicroPython code for the Arduino to interact with them), you can learn more at:

Wiring Diagram

  • How to connect Arduino to breadboard-mount button
The wiring diagram between Arduino MicroPython Button

This image is created using Fritzing. Click to enlarge image

  • How to connect Arduino to a panel-mounted button
The wiring diagram between Arduino MicroPython two-pin push button

This image is created using Fritzing. Click to enlarge image

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

Arduino - Button without Debounce

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

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-button-debounce """ from machine import Pin import time BUTTON_PIN = 'D2' # The Arduino Giga WiFi pin D2 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 run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the button to the Arduino as shown in the diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • 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 (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

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

Arduino Debounce for a Button

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-button-debounce """ from DIYables_MicroPython_Button import Button import time # Initialize buttons connected to pins D2 of Arduino Giga WiFi button = Button('D2') 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.
Arduino MicroPython Button library
  • Copy the given code and paste it into the Thonny IDE's editor.
  • Save the script on your Arduino 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 (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

You pressed the button once, and the Arduino 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.

Arduino Debounce for multiple Buttons

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

The wiring diagram between Arduino MicroPython 5 buttons

This image is created using Fritzing. Click to enlarge image

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-button-debounce """ from DIYables_MicroPython_Button import Button import time button_1 = Button('D2') # Initialize buttons connected to Arduino Giga WiFi pin D2 button_2 = Button('D3') # Initialize buttons connected to Arduino Giga WiFi pin D3 button_3 = Button('D4') # Initialize buttons connected to Arduino Giga WiFi pin D4 button_4 = Button('D5') # Initialize buttons connected to Arduino Giga WiFi pin D5 button_5 = Button('D6') # Initialize buttons connected to Arduino Giga WiFi pin D6 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 button_5.set_debounce_time(100) # Set debounce time to 100 milliseconds while True: button_1.loop() button_2.loop() button_3.loop() button_4.loop() button_5.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") if button_5.is_pressed(): print("The button 5 is pressed") if button_5.is_released(): print("The button 5 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!