Arduino Mega - Button - Debounce
When you program the Arduino Mega to read a button press, you may see one press counted several times. This happens because the button can bounce and quickly switch between LOW and HIGH. This is called chattering. Chattering can make one press look like multiple presses and cause errors in some programs. This tutorial explains how to fix it, a process called debouncing the button.
Or you can buy the following kits:
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 .
Learn about buttons (pin layout, how they work, and how to program them) in these simple tutorials if you're not familiar with them:

This image is created using Fritzing. Click to enlarge image
Let's compare Arduino Mega code that uses debounce with code that doesn't use debounce, and see how it behaves.
Before we learn about debouncing, let's look at the code without it and see how it works.
#define BUTTON_PIN 7
int button_state;
int prev_button_state = LOW;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
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");
prev_button_state = button_state;
}
Do these steps one by one:
Connect the parts according to the diagram.
Plug the Arduino Mega into your computer with a USB cable.
Open the Arduino IDE on your computer.
Choose the right board (Arduino Mega) and the COM port.
Copy the code above and open it in the Arduino IDE.
Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
The button is pressed
The button is pressed
The button is pressed
The button is released
The button is released
As you can see, you pressed and released the button just once. But the Arduino thinks it was several presses and releases.
※ NOTE THAT:
The DEBOUNCE_TIME value changes for different apps. Each app may use a different value.
#define BUTTON_PIN 7
#define DEBOUNCE_TIME 50
int last_steady_state = LOW;
int last_flickerable_state = LOW;
int current_state;
unsigned long last_debounce_time = 0;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
current_state = digitalRead(BUTTON_PIN);
if (current_state != last_flickerable_state) {
last_debounce_time = millis();
last_flickerable_state = current_state;
}
if ((millis() - last_debounce_time) > DEBOUNCE_TIME) {
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");
last_steady_state = current_state;
}
}
Copy the code above and open it in the Arduino IDE.
Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
Open the Serial Monitor.
Press and hold the reset button for a few seconds, then release it.
See the Serial Monitor.
The button is pressed
The button is released
As you can see, you pressed and released the button once. The Arduino reads it as one press and release, with no bouncing.
We made an easier way for beginners who work with many buttons by creating a library called ezButton. You can learn more about the ezButton library here: https://arduinogetstarted.com/tutorials/arduino-button-library
Let's look at some example code.
#include <ezButton.h>
ezButton button(7);
void setup() {
Serial.begin(9600);
button.setDebounceTime(50);
}
void loop() {
button.loop();
if(button.isPressed())
Serial.println("The button is pressed");
if(button.isReleased())
Serial.println("The button is released");
}
Let's debounce three buttons. Here is the wiring diagram for connecting an Arduino Mega to three buttons:

This image is created using Fritzing. Click to enlarge image
#include <ezButton.h>
ezButton button_1(6);
ezButton button_2(7);
ezButton button_3(8);
void setup() {
Serial.begin(9600);
button_1.setDebounceTime(50);
button_2.setDebounceTime(50);
button_3.setDebounceTime(50);
}
void loop() {
button_1.loop();
button_2.loop();
button_3.loop();
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");
}