Arduino - Button - Debounce

When a button is pressed/released or when a switch is toggled, you may think that its state is simply changed from LOW to HIGH or HIGH to LOW. In practice, it is not exactly like that. The state of the button (or switch) is quickly toggled between LOW and HIGH several times, due to mechanical and physical issues. This is called the chattering phenomenon. This phenomenon makes a single press that may be read as multiple presses, resulting in a malfunction in some kinds of applications. This tutorial shows how to eliminate this phenomenon (called debounce the input).

Arduino chattering phenomenon

About Button

Wiring Diagram

Arduino Button Wiring Diagram

Image is developed using Fritzing

Reading Button without Debounce

Before learning about debouncing, just see the code without debouncing and its behavior.

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Copy the below code and open with Arduino IDE
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-debounce */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as an pull-up input // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); if(lastState == HIGH && currentState == LOW) Serial.println("The button is pressed"); else if(lastState == LOW && currentState == HIGH) Serial.println("The button is released"); // save the the last state lastState = currentState; }
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Arduino IDE Upload Code
  • Open Serial Monitor
  • Press button one second and then release
  • See the result on Serial Monitor
  • COM6
    Send
    The button is pressed The button is pressed The button is pressed The button is released The button is released
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

⇒ As you can see, you pressed and released the button just the once. However, Arduino recognizes it as multiple presses and releases.

Reading Button with Debounce

Quick Steps

  • Copy the below code and open with Arduino IDE
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-debounce */ // constants won't change. They're used here to set pin numbers: const int BUTTON_PIN = 7; // the number of the pushbutton pin const int DEBOUNCE_DELAY = 50; // the debounce time; increase if the output flickers // Variables will change: int lastSteadyState = LOW; // the previous steady state from the input pin int lastFlickerableState = LOW; // the previous flickerable state from the input pin int currentState; // 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. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as an pull-up input // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = 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 (currentState != lastFlickerableState) { // reset the debouncing timer lastDebounceTime = millis(); // save the the last flickerable state lastFlickerableState = currentState; } if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) { // 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 && currentState == LOW) Serial.println("The button is pressed"); else if(lastSteadyState == LOW && currentState == HIGH) Serial.println("The button is released"); // save the the last steady state lastSteadyState = currentState; } }
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Open Serial Monitor
  • Press button one second and then release
  • See the result on Serial Monitor
  • COM6
    Send
    The button is pressed The button is released
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

⇒ As you can see, you pressed and released the button just the once. Arduino recognizes it as the single press and release. The chatter is eliminated.

Video

incoming

Additional Knowledge

DEBOUNCE_DELAY value depends on the applications. Different applications may use different values.

Extendability

The debounce method can apply for switch, touch sensor ...

Button on Commercial Products

Most electronic products have a reset button. Additionally, the button also keeps other functionalities in many products.

Comments

If you have any questions or suggestions, please comment on our YouTube channel