When a button is pressed or released, or a switch is flipped, newbies often assume that its state changes from LOW to HIGH or HIGH to LOW. In reality, this is not the case. Due to mechanical and physical characteristics, the state of the button (or switch) may fluctuate between LOW and HIGH multiple times per single user's action. This is known as chattering. Chattering can cause a single press to be read as multiple presses, leading to malfunctioning in certain applications.
The method to prevent this problem is referred as debouncing or debounce. This tutorial instructs you how to do it when using the button with ESP8266. We will learn though the below steps:
ESP8266 code without debouncing a button.
ESP8266 code with debouncing a button.
ESP8266 code with debouncing a button by using the ezButton library.
ESP8266 code with debouncing for multiple buttons.
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 are not familiar with buttons (including pinouts, functioning, and programming), the following tutorials can help you:
Connect the ESP8266 board to your computer using a USB cable.
Open Arduino IDE on your computer.
Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
Connect your ESP8266 to your computer using a USB cable.
Launch the Arduino IDE, select the appropriate board and port.
Copy the code below and open it in the Arduino IDE.
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-button-debounce */#define BUTTON_PIN D7 // The ESP8266 pin D7 connected to buttonint prev_button_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);// Configure the ESP8266 pin as a pull-up input: HIGH when the button is open, LOW when pressed.pinMode(BUTTON_PIN, INPUT_PULLUP);}voidloop() {// read the state of the switch/button: button_state = digitalRead(BUTTON_PIN);if (prev_button_state == HIGH && button_state == LOW)Serial.println("The button is pressed");elseif (prev_button_state == LOW && button_state == HIGH)Serial.println("The button is released");// save the the last state prev_button_state = button_state;}
Click the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
Open the Serial Monitor.
Press and hold the button for a few seconds, then let go.
Check the output in the Serial Monitor.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' 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
Nodemcu 1.0 (ESP-12E Module) on COM15
2
You may see that, sometime, you only pressed and released the button one time. Nevertheless, ESP8266 perceives it as multiple presses and releases. This is the chattering phenomenon mentioned at the beginning of the tutorial. Let's see how to fix it in the next part.
Reading Button with Debounce
The below code applies the method called debounce to prevent the chattering phenomenon.
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-button-debounce */#define BUTTON_PIN D7 // The ESP8266 pin D7 connected to button#define DEBOUNCE_TIME 50 // The debounce time in millisecond, increase this time if it still chattersint lastSteadyState = LOW; // The previous steady state from the input pinint lastFlickerableState = LOW; // The previous flickerable state from the input pinint button_state; // The current reading from the input pin// The following variables are unsigned longs because the time, measured in// milliseconds, will quickly become a bigger number than can be stored in an int.unsignedlong lastDebounceTime = 0; // The last time the output pin was toggledvoidsetup() {// Initialize the Serial to communicate with the Serial Monitor.Serial.begin(9600);// Configure the ESP8266 pin as a pull-up input: HIGH when the button is open, LOW when pressed.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 != lastFlickerableState) {// reset the debouncing timer lastDebounceTime = millis();// save the the last flickerable state lastFlickerableState = button_state; }if ((millis() - lastDebounceTime) > 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(lastSteadyState == HIGH && button_state == LOW)Serial.println("The button is pressed");elseif(lastSteadyState == LOW && button_state == HIGH)Serial.println("The button is released");// save the the last steady state lastSteadyState = button_state; }}
Detailed Instructions
Wire the components as shown in the diagram.
Connect the ESP8266 board to your computer using a USB cable.
Open Arduino IDE on your computer.
Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
Copy the code above and open it with the Arduino IDE.
Click the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
Open the Serial Monitor.
Hold down the button for a few seconds, then let go.
Check the Serial Monitor to view the outcome.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
The button is pressed
The button is released
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2
As you can observe, you only pressed and released the button once. ESP8266 is able to detect it as a single press and release, thus eliminating any unnecessary chatter.
We Made It Simple - ESP8266 Button Debounce Code with Library
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-button-debounce */#include <ezButton.h>ezButtonbutton(D7); // create ezButton object for pin D7voidsetup() {Serial.begin(9600);button.setDebounceTime(50); // 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");}
ESP8266 Button Debounce Code for A Multiple Buttons
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-button-debounce */#include <ezButton.h>ezButtonbutton1(D5); // create ezButton object for pin D5ezButtonbutton2(D6); // create ezButton object for pin D6ezButtonbutton3(D7); // create ezButton object for pin D7voidsetup() {Serial.begin(9600);button1.setDebounceTime(50); // set debounce time to 50 millisecondsbutton2.setDebounceTime(50); // set debounce time to 50 millisecondsbutton3.setDebounceTime(50); // 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");}
The schematic for the code above:. The illustration of the wiring for the code:. The representation of the connections for the code:
This image is created using Fritzing. Click to enlarge image
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!