Arduino UNO R4 - Button - Debounce

When programming the Arduino Uno R4 to detect a button press event, you may find that a single press is detected multiple times. This happens because, due to mechanical factors, the button or switch can quickly switch between LOW and HIGH several times. This is called "chattering." Chattering can cause one button press to be detected as multiple presses, which can cause errors in some applications. This tutorial explains how to fix this issue, a process known as debouncing the button.

Arduino UNO R4 chattering phenomenon

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Push Button
1×(Optional) Panel-mount Push Button
1×Breadboard
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino UNO R4
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

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. We appreciate your support.

Overview of Button

Learn about buttons (pinout, operation, programming) in the following tutorials if you're not familiar with them:

Wiring Diagram

The wiring diagram between Arduino UNO R4 Button

This image is created using Fritzing. Click to enlarge image

Let's examine and compare the Arduino UNO R4 code without and with debounce, and observe their behaviors.

Arduino Uno R4 - Button without Debounce

Before we learn about debouncing, let's look at the code without it and see how it behaves.

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-button-debounce */ #define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button int button_state; // the current state of button int prev_button_state = LOW; // the previous state of button void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as a pull-up input 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 last state prev_button_state = button_state; }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the above code and open it in the Arduino IDE.
  • Click the Upload button in Arduino IDE to send code to Arduino UNO R4.
Arduino IDE Upload Code
  • Open the Serial Monitor.
  • Press and hold the button for a few seconds, then release it.
  • Check the Serial Monitor for the result.
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 only once. However, the Arduino recognizes it as multiple presses and releases.

※ NOTE THAT:

The value of DEBOUNCE_TIME varies with different applications. Each application might use a unique value.

Arduino Uno R4 - Button with Debounce

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-button-debounce */ #define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button #define DEBOUNCE_TIME 50 // The debounce time; increase if the output flickers int last_steady_state = LOW; // the previous steady state from the input pin int last_flickerable_state = LOW; // the previous flickerable state from the input pin int current_state; // the current reading from the input pin unsigned long last_debounce_time = 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 a pull-up input pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: current_state = digitalRead(BUTTON_PIN); // If the switch/button changed, due to noise or pressing: if (current_state != last_flickerable_state) { // reset the debouncing timer last_debounce_time = millis(); // save the the last flickerable state last_flickerable_state = current_state; } if ((millis() - last_debounce_time) > DEBOUNCE_TIME) { // if the button state has changed: if (last_steady_state == HIGH && current_state == LOW) Serial.println("The button is pressed"); else if (last_steady_state == LOW && current_state == HIGH) Serial.println("The button is released"); // save the the last steady state last_steady_state = current_state; } }

Detailed Instructions

  • Copy the above code and open it with Arduino IDE.
  • Press the Upload button in the Arduino IDE to send the code to the Arduino UNO R4.
  • Open the Serial Monitor.
  • Hold down the button for a few seconds before letting go.
  • Check out the 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 once. The Arduino correctly detects it as a single press and release, eliminating any chattering.

We Made It Simple: Arduino UNO R4 Button Debounce Code Using a Library

We made a simpler way for beginners who use many buttons by creating a library named ezButton. You can find out more about the ezButton library here.

Let's see some example codes.

Arduino UNO R4 Button Debounce Code for A Single Button

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library * * This example reads the state of a button with debounce and print it to Serial Monitor. */ #include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; 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 UNO R4 Button Debounce Code for A Multiple Buttons

Let's debouce for 3 buttons. Here is the wiring diagram between Arduino UNO R4 and three buttons:

The wiring diagram between Arduino UNO R4 Button Library

This image is created using Fritzing. Click to enlarge image

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-button-debounce */ #include <ezButton.h> ezButton button_1(6); // initialize ezButton object connected to pin 6 ezButton button_2(7); // initialize ezButton object connected to pin 7 ezButton button_3(8); // initialize ezButton object connected to pin 8 void setup() { Serial.begin(9600); button_1.setDebounceTime(50); // configure debounce time for button_1 to 50ms button_2.setDebounceTime(50); // configure debounce time for button_2 to 50ms button_3.setDebounceTime(50); // configure debounce time for button_3 to 50ms } void loop() { button_1.loop(); // update button_1 state button_2.loop(); // update button_2 state button_3.loop(); // update button_3 state if(button_1.isPressed()) Serial.println("The button 1 is pressed"); if(button_1.isReleased()) Serial.println("The button 1 is released"); if(button_2.isPressed()) Serial.println("The button 2 is pressed"); if(button_2.isReleased()) Serial.println("The button 2 is released"); if(button_3.isPressed()) Serial.println("The button 3 is pressed"); if(button_3.isReleased()) Serial.println("The button 3 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!