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
Everything below is what the build uses. The links go to the parts we make and test.
- Arduino UNO Q
- Breadboard-mount Button with Cap
- Breadboard-mount Button Kit
- Panel-mount Push Button
- Push Button Module
- Breadboard
- Jumper Wires
- Screw Terminal Block Shield for Arduino Uno
- Sensors/Servo Expansion Shield for Arduino Uno
- Breadboard Shield for Arduino Uno
- Enclosure for Arduino Uno
- Prototyping Base Plate & Breadboard Kit for Arduino UNO
- DIYables Sensor Kit (18 sensors/displays)
- DIYables USB Type-C Cable
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.
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