Arduino Nano - Button - Debounce

When a button is pressed or released, or a switch is toggled, newbies often assume that its state is changed from LOW to HIGH or HIGH to LOW. In reality, this is not always the case. Due to mechanical and physical characteristics, the state of the button (or switch) may be quickly toggled between LOW and HIGH multiple times in response to a single event. This phenomenon is known as chattering. Chattering can cause a single press to be read as multiple presses, resulting in malfunction in certain applications.

chattering phenomenon

The method to eliminate this problem is referred as debouncing or debounce. This tutorial instructs you how to do it when using the button with Arduino Nano. We will learn though the below steps:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
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 Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano

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 are not familiar with buttons (pinout, functionality, programming, etc.), the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and Button

This image is created using Fritzing. Click to enlarge image

Let us observe and contrast the Arduino Nano code for both WITH and WITHOUT debounce, as well as their respective behaviors.

Reading Button without Debounce

Before delving into the concept of debouncing, take a look at the code without debouncing and observe its behavior.

Detailed Instructions

  • Attach an USB cable to the Arduino Nano and your PC.
  • Launch the Arduino IDE, choose the correct board and port.
  • Copy the code below and open it in the Arduino IDE.
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-debounce */ #define BUTTON_PIN 2 // The number of the pushbutton pin int prev_button_state = LOW; // The previous state from the input pin int button_state; // The current reading from the input pin void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // Configure the Arduino Nano pin as a pull-up input // The pull-up input pin is HIGH when the button is open and LOW when pressed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // 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"); else if(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 in the Arduino IDE to compile and upload the code to the Arduino Nano board.
Arduino IDE Upload Code
  • Open the Serial Monitor.
  • Press and hold the button for a few seconds, then release it.
  • Try several times.
  • Check out the result in the 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  

Sometime, you only pressed and released the button once. Nevertheless, Arduino Nano interprets 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 Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-debounce */ #define BUTTON_PIN 2 // The number of the pushbutton pin const int DEBOUNCE_DELAY = 50; // The debounce time; increase if the output flickers int lastSteadyState = LOW; // The previous steady state from the input pin int lastFlickerableState = LOW; // The previous flickerable state from the input pin int 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. unsigned long lastDebounceTime = 0; // The last time the output pin was toggled void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // Configure the Arduino Nano pin as a pull-up input // The pull-up input pin is HIGH when the button is open and LOW when pressed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // 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_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 && button_state == LOW) Serial.println("The button is pressed"); else if (lastSteadyState == LOW && button_state == HIGH) Serial.println("The button is released"); // save the the last steady state lastSteadyState = button_state; } }

Detailed Instructions

  • 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 Arduino Nano.
  • Open the Serial Monitor.
  • Continue to press the button for a few seconds, then release it.
  • Check out the result on the Serial Monitor.
COM6
Send
The button is pressed The button is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

As you can observe, you pushed and released the button only once. Arduino Nano detects it as a single push and release. The noise is eliminated.

We Made It Simple - Arduino Nano Button Debounce Code with Library

We have designed a library, ezButton, to make it simpler for those who are just starting out, particularly when dealing with multiple buttons. You can find out more about the ezButton library here.

Arduino Nano Button Debounce Code for A Single Button

#include <ezButton.h> ezButton button(2); // create ezButton object for pin 2; void setup() { Serial.begin(9600); button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) Serial.println("The button is pressed"); if(button.isReleased()) Serial.println("The button is released"); }

Arduino Nano Button Debounce Code for A Multiple Buttons

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-debounce */ #include <ezButton.h> ezButton button1(6); // create ezButton object for pin 6; ezButton button2(7); // create ezButton object for pin 7; ezButton button3(8); // create ezButton object for pin 8; void setup() { Serial.begin(9600); button1.setDebounceTime(50); // set debounce time to 50 milliseconds button2.setDebounceTime(50); // set debounce time to 50 milliseconds button3.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button1.loop(); // MUST call the loop() function first button2.loop(); // MUST call the loop() function first button3.loop(); // MUST call the loop() function first if(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 visual representation of the wiring for the code:

The wiring diagram between Arduino Nano and multiple buttons

This image is created using Fritzing. Click to enlarge image

Video Tutorial

Additional Knowledge

  • The value of DEBOUNCE_DELAY varies depending on the physical characteristics of each button. It is possible that different buttons use different values.

Extendability

The debounce technique can be utilized with a switch, touch sensor, and more.

※ 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!