Arduino UNO Q - Button - Debounce

by DIYables in Circuits > Arduino

63 Views, 0 Favorites, 0 Comments

Arduino UNO Q - Button - Debounce

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-button-debounce

A single button press can register as several presses because mechanical contacts bounce rapidly between LOW and HIGH when they make or break contact. This guide shows how to fix that chattering on Arduino UNO Q, first with a plain millis() timer, then with the ezButton library, and finally how to read a debounced press count from Linux over Bridge and report it through Telegram.

Supplies

Wiring

Connect one pin of the button to GND and the other pin to digital pin 7 on the Arduino UNO Q, as shown in the wiring diagram. The sketch configures pin 7 with INPUT_PULLUP, so the pin reads HIGH when the button is open and LOW when it is pressed, no external resistor needed.

How It Works

Mechanical contacts bounce for a few milliseconds when pressed or released, so a plain digitalRead() can see several HIGH/LOW transitions from one press. The millis()-based fix starts a timer whenever the pin state changes and only accepts the new state as real once it has stayed stable for DEBOUNCE_TIME (50ms by default). The ezButton library does the same job automatically, handling INPUT_PULLUP, debounce timing, and edge detection internally.

Code

The basic sketch reads BUTTON_PIN with digitalRead() and compares it to the previous state to detect HIGH-to-LOW and LOW-to-HIGH transitions, but without debouncing this can fire multiple times per press. The debounced version adds a millis() timer that only accepts a transition once it has held for DEBOUNCE_TIME. The ezButton version replaces this logic with button.loop() plus button.isPressed()/isReleased(), which each fire exactly once per physical press or release.

arduino-uno-q-button-debounce.ino - the sketch from the tutorial, unchanged.

/*
* This Arduino UNO Q code was developed by newbiely.com
*
* This Arduino UNO Q code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-button-debounce
*/

#define BUTTON_PIN 7 // The Arduino UNO Q pin connected to the button

int button_state;
int prev_button_state = HIGH; // HIGH = not pressed (pull-up)

void setup() {
// initialize the pushbutton pin as a pull-up input
pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
// read the state of the button (LOW = pressed, HIGH = not pressed)
button_state = digitalRead(BUTTON_PIN);

if (prev_button_state == HIGH && button_state == LOW) {
// button was just pressed (HIGH → LOW transition)
// TO DO: add your press action here
} else if (prev_button_state == LOW && button_state == HIGH) {
// button was just released (LOW → HIGH transition)
// TO DO: add your release action here
}

// save the last state
prev_button_state = button_state;
}

Things That Go Wrong

If you skip debouncing, expect a single press to sometimes log as two or three events — this is the chattering the tutorial addresses. If debounced code still shows extra events, raise DEBOUNCE_TIME to 75 or 100ms. When using Bridge, never open /dev/ttyHS1 on Linux or Serial1 on the MCU directly, since the Arduino Router reserves them for MPU-MCU communication; use Bridge.call() and Bridge.provide() instead.

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-button-debounce