When a button is pressed/released or when a switch is toggled between ON and OFF, its state is changed from LOW to HIGH ( or HIGH to LOW) once. Is this correct?
⇒ No, it is not. That is because in the physical world. when you do a single press on a button, the state of the button is quickly toggled between LOW and HIGH several times rather than once. This is the mechanical and physical characteristic. This phenomenon is known with a name: chattering. The chattering phenomenon makes MCU (e.g. ESP32) read multiple button presses in response to a single actual press. This results in a malfunction. The process to eliminate this phenomenon is called debounce. This tutorial shows how to do it.
This tutorial provides:
How to debounce for a button on Arduino Nano ESP32 code
How to debounce for a button on Arduino Nano ESP32 code using library
How to debounce for multiple buttons on Arduino Nano ESP32 code using library
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
We have specific tutorials about button. The tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, Arduino Nano ESP32 code... Learn more about them at the following links:
Wire the components according to the provided diagram.
Connect the Arduino Nano ESP32 board to your computer using a USB cable.
Launch the Arduino IDE on your computer.
Select the Arduino Nano ESP32 board and its corresponding COM port.
Copy the below code and paste it to Arduino IDE.
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-debounce */#define BUTTON_PIN D2 // Arduino Nano ESP32 pin D2 pin connected to buttonint prev_state = LOW; // The previous state from the input pinint button_state; // The current reading from the input pinvoidsetup() {// Initialize the Serial to communicate with the Serial Monitor.Serial.begin(9600);// initialize the button pin as an pull-up input (HIGH when the switch is open and LOW when the switch is closed)pinMode(BUTTON_PIN, INPUT_PULLUP);}voidloop() {// read the state of the switch/button: button_state = digitalRead(BUTTON_PIN);if (prev_state == HIGH && button_state == LOW)Serial.println("The button is pressed");elseif (prev_state == LOW && button_state == HIGH)Serial.println("The button is released");// save the the last state prev_state = button_state;}
Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
Open Serial Monitor on Arduino IDE
Press the button once but keep it several seconds, and then release it.
Check out the result on the Serial Monitor. It looks like the below:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
The button is pressed
The button is pressed
The button is pressed
The button is released
The button is released
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2
⇒ As you can see, you did only a single press and release, but Arduino Nano ESP32 read multiple presses and releases.
※ NOTE THAT:
The chattering phenomenon does not happen all times. If it does not happen, please try the above test several time.
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-debounce */#define BUTTON_PIN D2 // Arduino Nano ESP32 pin D2 pin connected to button#define DEBOUNCE_TIME 50 // The debounce time in millisecond, increase this time if it still chattersint prev_state_steady = LOW; // The previous steady state from the input pinint prev_state_flick = LOW; // The previous flickerable state from the input pinint button_state; // The current reading from the input pinunsignedlong last_debounce_time = 0; // The last time the output pin was toggledvoidsetup() {// Initialize the Serial to communicate with the Serial Monitor.Serial.begin(9600);// initialize the button pin as an pull-up input (HIGH when the switch is open and LOW when the switch is closed)pinMode(BUTTON_PIN, INPUT_PULLUP);}voidloop() {// read the state of the switch/button: button_state = digitalRead(BUTTON_PIN);// check to see if you just pressed the button// (i.e. the input went from LOW to HIGH), and you've waited long enough// since the last press to ignore any noise:// If the switch/button changed, due to noise or pressing:if (button_state != prev_state_flick) {// reset the debouncing timer last_debounce_time = millis();// save the the last flickerable state prev_state_flick = button_state; }if ((millis() - last_debounce_time) > DEBOUNCE_TIME) {// whatever the reading is at, it's been there for longer than the debounce// delay, so take it as the actual current state:// if the button state has changed:if(prev_state_steady == HIGH && button_state == LOW)Serial.println("The button is pressed");elseif(prev_state_steady == LOW && button_state == HIGH)Serial.println("The button is released");// save the the last steady state prev_state_steady = button_state; }}
Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
Open Serial Monitor on Arduino IDE
Keep pressing the button several seconds and then release it.
Check out the result on the Serial Monitor. It looks like the below:
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
The button is pressed
The button is released
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2
⇒ As you can see, you did one press and release, and Arduino Nano ESP32 read one press and release. The chattering is eliminated.
We Made It Simple - Arduino Nano ESP32 Button Debounce Code with Library
To make it easy for newbies, especially when deboucing for multiple buttons, we made a button library, called ezButton. You can learn about ezButton library here.
Arduino Nano ESP32 Button Debounce Code for A Single Button
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-debounce */#include <ezButton.h>#define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chattersezButtonbutton(D2); // create ezButton object for pin D2voidsetup() {Serial.begin(9600);button.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds}voidloop() {button.loop(); // MUST call the loop() function firstif (button.isPressed())Serial.println("The button is pressed");if (button.isReleased())Serial.println("The button is released");}
Arduino Nano ESP32 Button Debounce Code for A Multiple Buttons
Let's write debounce code for three buttons.
The wiring diagram
This image is created using Fritzing. Click to enlarge image
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-debounce */#include <ezButton.h>#define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chattersezButtonbutton1(D6); // create ezButton object for pin D6ezButtonbutton2(D7); // create ezButton object for pin D7ezButtonbutton3(D8); // create ezButton object for pin D8voidsetup() {Serial.begin(9600);button1.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 millisecondsbutton2.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 millisecondsbutton3.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds}voidloop() {button1.loop(); // MUST call the loop() function firstbutton2.loop(); // MUST call the loop() function firstbutton3.loop(); // MUST call the loop() function firstif (button1.isPressed())Serial.println("The button 1 is pressed");if (button1.isReleased())Serial.println("The button 1 is released");if (button2.isPressed())Serial.println("The button 2 is pressed");if (button2.isReleased())Serial.println("The button 2 is released");if (button3.isPressed())Serial.println("The button 3 is pressed");if (button3.isReleased())Serial.println("The button 3 is released");}
Video Tutorial
Additional Knowledge
DEBOUNCE_TIME value depends on the hardware. Different hardware may use different values.
The debounce should also apply for on/off switch, limit switch, reed switch, touch sensor ...
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!