Arduino UNO Q - Button - Debounce
When programming Arduino UNO Q to detect a button press, a single press might be detected multiple times. This happens because mechanical contacts rapidly bounce between LOW and HIGH — a phenomenon called chattering. This tutorial shows you how to fix it through debouncing.


Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
MCU Code — Without Debounce (for comparison)
The Arduino UNO Q has two processors: the STM32 MCU (handles real-time hardware control) and the Qualcomm MPU (runs Debian Linux). In this section, only the STM32 MCU is programmed — the Linux side stays idle. A later section will show how both processors work together.
This is the version without debounce from the Button tutorial. Notice how a single press may register multiple events:
Detailed Instructions
- First time with Arduino UNO Q? Follow the Getting Started with Arduino UNO Q tutorial to get your development environment ready before proceeding.
- Wire the button: Connect one button pin to GND and the other to pin 7 according to the wiring diagram.
- Connect: Plug the Arduino UNO Q into your computer with a USB-C cable.
- Open Arduino App Lab: Launch Arduino App Lab and wait until it detects your Arduino UNO Q.
- Create a new App: Click the Create New App button.

- Give the App a name, for example: DIYables_ButtonDebounce
- Click Create to confirm.
- You will see a set of folders and files generated inside your new App.

- Find the sketch/sketch.ino file — this is where you will paste the MCU sketch.

- Press the button once then release it — observe that multiple events may be logged (visible via the Bridge Monitor in the next section).
- Notice: This is the chattering effect — the next section fixes it.
MCU Code — With Debounce (using millis)
This version uses millis() to wait for the signal to stabilize before treating it as a real event. Only state changes that persist for more than DEBOUNCE_TIME milliseconds are accepted:
- How it works: If the button state changes rapidly (bouncing), the timer resets each time. Only when the state is stable for 50ms is it treated as a real press or release.
- Pro Tip: If your button still shows multiple events, try increasing DEBOUNCE_TIME to 75 or 100.
※ NOTE THAT:
The optimal DEBOUNCE_TIME depends on the button quality and the application. 50ms is a safe default for most push buttons.
Linux + MCU Bridge Programming
The Arduino UNO Q has two processors that work together: the MPU (Qualcomm, runs Debian Linux) and the MCU (STM32, runs Zephyr OS with your Arduino sketch). They communicate using RPC via the Arduino_RouterBridge library — never via raw serial ports.
- The button is connected to the MCU (STM32) — wired to a digital input pin on the STM32. The MCU reads and debounces button presses using the ezButton library.
- The MPU cannot read the button directly — it must request data from the MCU via Bridge.call(). The MCU responds with the press count or resets it.
- The MPU has Wi-Fi — because the MPU runs full Debian Linux with Wi-Fi, it can report press counts via Telegram and accept reset commands remotely.
- Communication: Bridge.call() on the Linux side invokes Bridge.provide() functions on the MCU side
- ⚠️ Reserved: /dev/ttyHS1 (Linux) and Serial1 (MCU) are used by the Arduino Router — never open them directly
In short: MCU debounces button presses and counts them → MPU requests the count → MPU forwards it via Telegram.
MCU sketch — button debounce with press counter and Bridge:
Python script (Arduino App Lab) — poll press count from Linux:
- Note: Make sure Bridge.begin() is called in the MCU sketch and the sketch is uploaded before running the Python script on the Linux side.
- ⚠️ Warning: Never directly open /dev/ttyHS1 (on Linux) or use Serial1 (on MCU) in your code — these are reserved by the Arduino Router and accessing them will break the Bridge.
Detailed Instructions
- Upload the MCU sketch: Open Arduino App Lab, create a new App, paste the Bridge MCU sketch above into sketch/sketch.ino, install the ezButton and Arduino_RouterBridge libraries, and click Run.
- Add the Python script: Paste the Python code above into the Python tab of the same App.
- Run the App: Click Run — the Python side polls the press count every 3 seconds.
- Press the button several times.
- Check the console: Open the Console tab → MCU Monitor subtab to see press/release events. Open the Python Console subtab to see the polled count.
App Lab Console Output
Telegram Integration
You can query the button press count or reset it from anywhere via Telegram.
If you do not have a Telegram bot yet, see How to Create a Telegram Bot to get your bot token before continuing.
MCU sketch: Keep the same MCU sketch from the previous Bridge section — no changes needed. Make sure it is already uploaded and running on the STM32 before proceeding.
Python script (Arduino App Lab) — Telegram bot for button press tracking:
- Note: Replace YOUR_BOT_TOKEN with the token obtained from @BotFather on Telegram.
- Send /count to see how many times the button has been pressed since power-on or last reset.
- Send /reset to reset the counter to 0.
Detailed Instructions
- Upload the MCU sketch: Use the Bridge MCU sketch from the previous section (upload it first if not already done).
- Paste the Telegram script: Copy the Python code above into the Python tab of your App in Arduino App Lab.
- Set your token: Replace YOUR_BOT_TOKEN in the script with your actual bot token.
- Run the App: Click Run — the bot starts listening for Telegram messages immediately.
- Test it: Press the button 5 times, then send /count on Telegram — you should receive 5. Then send /reset and /count again to confirm it resets.
- Pro Tip: Use this as an event counter for counting visitors, machine cycles, or triggered alerts.
App Lab Console Output
ArduinoBot
OpenClaw Integration
OpenClaw integration for Arduino UNO Q button debounce is coming soon.
- Coming Soon: OpenClaw support for button press tracking on Arduino UNO Q will be covered in a future update.
Application/Project Ideas
- Visitor counter: Place a button at a door — press count tracks how many people entered
- Machine cycle counter: Count actuator cycles in a production line, check count via Telegram
- Manual event log: Press button to log a timestamped event on the Linux side (MPU has real clock)
- Multi-mode controller: Each button press cycles to the next mode; check mode via Telegram
- Press-to-order: Press button to send a Telegram "reorder" message for supplies
Challenge Yourself
- Easy: Modify the Bridge sketch to also track how many times the button was released
- Medium: Add a get_last_press_time() Bridge function that returns the number of seconds since the last press
- Advanced: Build a Telegram bot that automatically sends a message when the press count reaches a target number (e.g., "10 presses reached!")

