Arduino UNO Q를 프로그래밍하여 버튼 누르기를 감지할 때, 한 번의 누르기가 여러 번 감지될 수 있습니다. 이는 기계식 접점이 LOW와 HIGH 사이에서 빠르게 튕기기 때문이며, 이 현상을 채터링(chattering)이라고 합니다. 이 튜토리얼은 디바운싱(debouncing)을 통해 이 문제를 해결하는 방법을 보여줍니다.
Arduino UNO Q는 두 개의 프로세서를 가지고 있습니다: STM32 MCU(실시간 하드웨어 제어 담당)와 Qualcomm MPU(Debian Linux 실행). 이 섹션에서는 STM32 MCU만 프로그래밍됩니다 — Linux 측은 유휴 상태로 유지됩니다. 나중 섹션에서 두 프로세서가 함께 작동하는 방법을 보여줍니다.
이것은 아두이노 우노 Q - 버튼에서 디바운싱 없는 버전입니다. 한 번의 누르기가 여러 이벤트로 등록될 수 있음을 주목하세요:
/* * 이 Arduino UNO Q 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino UNO Q 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-q/arduino-uno-q-button-debounce */#define BUTTON_PIN 7 // The Arduino UNO Q pin connected to the buttonint button_state;int prev_button_state = HIGH; // HIGH = not pressed (pull-up)voidsetup() {// initialize the pushbutton pin as a pull-up inputpinMode(BUTTON_PIN, INPUT_PULLUP);}voidloop() {// 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 } elseif (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;}
빠른 단계
처음 Arduino UNO Q를 사용하나요? 진행하기 전에 아두이노 우노 Q 시작하기 튜토리얼을 따라 개발 환경을 준비하세요.
버튼 연결: 배선도에 따라 버튼 한쪽 핀을 GND에 다른 쪽을 핀 7에 연결하세요.
연결: USB-C 케이블로 Arduino UNO Q를 컴퓨터에 연결하세요.
Arduino App Lab 열기: Arduino App Lab을 실행하고 Arduino UNO Q를 감지할 때까지 기다리세요.
새 앱 만들기:Create New App 버튼을 클릭하세요.
앱에 이름을 지정하세요. 예: DIYables_ButtonDebounce
Create를 클릭하여 확인하세요.
새 앱 내에 생성된 폴더 및 파일 세트가 표시됩니다.
sketch/sketch.ino 파일을 찾으세요 — MCU 스케치를 붙여넣을 위치입니다.
스케치 붙여넣기: 위의 디바운싱 없는 코드를 복사하여 스케치 파일에 붙여넣으세요. 다른 파일은 기본값으로 유지하세요.
업로드: Arduino App Lab의 Run 버튼을 클릭하여 컴파일하고 STM32에 업로드하세요.
버튼을 한 번 누른 후 놓으세요 — 여러 이벤트가 기록될 수 있음을 관찰하세요 (다음 섹션의 Bridge Monitor를 통해 볼 수 있습니다).
주의: 이것이 채터링 효과입니다 — 다음 섹션에서 이를 해결합니다.
MCU 코드 — 디바운싱 포함 (millis 사용)
이 버전은 millis()를 사용하여 신호가 안정화될 때까지 기다린 후 실제 이벤트로 취급합니다. DEBOUNCE_TIME 밀리초 이상 지속되는 상태 변화만 허용됩니다:
/* * 이 Arduino UNO Q 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino UNO Q 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-q/arduino-uno-q-button-debounce */#define BUTTON_PIN 7 // The Arduino UNO Q pin connected to the button#define DEBOUNCE_TIME 50 // The debounce time in milliseconds; increase if output flickersint last_steady_state = LOW; // the previous stable state from the input pinint last_flickerable_state = LOW; // the previous flickerable state from the input pinint current_state; // the current reading from the input pinunsignedlong last_debounce_time = 0; // the last time the state changedvoidsetup() {// initialize the pushbutton pin as a pull-up inputpinMode(BUTTON_PIN, INPUT_PULLUP);}voidloop() {// read the current state of the button current_state = digitalRead(BUTTON_PIN);// if the state changed (due to noise or a press/release), reset the debounce timerif (current_state != last_flickerable_state) { last_debounce_time = millis(); last_flickerable_state = current_state; }if ((millis() - last_debounce_time) > DEBOUNCE_TIME) {// the state has been stable for DEBOUNCE_TIME ms — treat it as realif (last_steady_state == HIGH && current_state == LOW) {// TO DO: button pressed action here } elseif (last_steady_state == LOW && current_state == HIGH) {// TO DO: button released action here } last_steady_state = current_state; }}
작동 원리: 버튼 상태가 빠르게 변하면 (튕김), 매번 타이머가 재설정됩니다. 상태가 50ms 동안 안정적일 때만 실제 누르기 또는 놓기로 취급됩니다.
팁: 버튼이 여전히 여러 이벤트를 표시하면 DEBOUNCE_TIME을 75 또는 100으로 증가시켜보세요.
※ 주의:
최적의 DEBOUNCE_TIME은 버튼 품질과 응용 프로그램에 따라 다릅니다. 50ms는 대부분의 푸시 버튼에 대한 안전한 기본값입니다.
ezButton 라이브러리로 더 간단한 디바운싱
초보자 및 다중 버튼 프로젝트의 경우, ezButton 라이브러리를 사용합니다 — INPUT_PULLUP, 디바운싱, 엣지 감지를 자동으로 처리합니다.
ezButton으로 단일 버튼:
/* * 이 Arduino UNO Q 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino UNO Q 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-q/arduino-uno-q-button-debounce */#include <ezButton.h>ezButtonbutton(7); // create ezButton object attached to pin 7voidsetup() {button.setDebounceTime(50); // set debounce time to 50 milliseconds}voidloop() {button.loop(); // MUST call the loop() function firstif (button.isPressed()) {// TO DO: button pressed action here }if (button.isReleased()) {// TO DO: button released action here }}
Install the library: Click the Add sketch library button (the open book icon with a + sign) in the left sidebar.
Search for ezButton created by ArduinoGetStarted.com and click the Install button.
My Apps/DIYables Apps
Run
Bricks
No bricks added...
Sketch Libraries
No sketch libra...
Files
python
sketch
.gitignore
README.md
app.yaml
sketch.ino
Add sketch library
ezButton
ezButtonArduinoGetStarted.com
Button library supports debounce, pressed/released events and the press counting. It is easy to use with multiple buttons. The library can be used for push-button, momentary switches, toggle switch, magnetic contact switch (door sensor)... It is designed for not only beginners but also experienced users.
1.0.6
Install
More Info
Search for Arduino_RouterBridge created by Arduino and click the Install button.
My Apps/DIYables Apps
Run
Bricks
No bricks added...
Sketch Libraries
No sketch libra...
Files
python
sketch
.gitignore
README.md
app.yaml
sketch.ino
Add sketch library
Arduino_RouterBridge
Arduino_RouterBridgeArduino
This library provides a simple RPC bridge for Arduino UNO Q boards, allowing communication between the board and other devices using MsgPack serialization.
0.4.1
Install
More Info
작동 원리:button.loop()는 매 루프 사이클마다 호출되어야 합니다. 그러면 button.isPressed()는 물리적 누르기당 정확히 한 번 true를 반환하고, button.isReleased()는 놓기당 정확히 한 번 true를 반환합니다 — 채터링이 없습니다.
Arduino UNO Q는 함께 작동하는 두 개의 프로세서를 가지고 있습니다: MPU (Qualcomm, Debian Linux 실행) 및 MCU (STM32, Arduino 스케치를 실행하는 Zephyr OS). 이들은 Arduino_RouterBridge 라이브러리를 통해 RPC를 사용하여 통신합니다 — 절대 원본 시리얼 포트를 통해서는 안됩니다.
버튼이 MCU (STM32)에 연결됩니다 — STM32의 디지털 입력 핀에 배선됩니다. MCU는 ezButton 라이브러리를 사용하여 버튼 누르기를 읽고 디바운싱합니다.
MPU는 버튼을 직접 읽을 수 없습니다 — Bridge.call()을 통해 MCU에 데이터를 요청해야 합니다. MCU는 누르기 횟수로 응답하거나 재설정합니다.
MPU는 Wi-Fi를 가지고 있습니다 — MPU가 Wi-Fi와 함께 전체 Debian Linux를 실행하므로, Telegram을 통해 누르기 횟수를 보고할 수 있습니다.
통신: Linux 측의 Bridge.call()은 MCU 측의 Bridge.provide() 함수를 호출합니다
⚠️ 예약됨:/dev/ttyHS1 (Linux) 및 Serial1 (MCU)은 Arduino Router에서 사용됩니다 — 절대 직접 열면 안됩니다
/* * 이 Arduino UNO Q 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino UNO Q 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-q/arduino-uno-q-button-debounce */#include"Arduino_RouterBridge.h"#include <ezButton.h>ezButtonbutton(7);int press_count = 0;void get_press_count() { Monitor.println("Press count: " + String(press_count));}void reset_count() { press_count = 0; Monitor.println("Press count reset to 0");}voidsetup() {button.setDebounceTime(50);Bridge.begin(); Monitor.begin();Bridge.provide("get_press_count", get_press_count);Bridge.provide("reset_count", reset_count); Monitor.println("Button Debounce Bridge ready");}voidloop() {button.loop();if (button.isPressed()) { press_count++; Monitor.println("Button PRESSED — count: " + String(press_count)); }if (button.isReleased()) Monitor.println("Button RELEASED");}
/* * 이 Arduino UNO Q 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino UNO Q 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-q/arduino-uno-q-button-debounce */from arduino.app_utils import *import timedef loop():whileTrue: count = Bridge.call("get_press_count")print(f"Current press count: {count}") time.sleep(3)App.run(user_loop=loop)
참고: MCU 스케치에서 Bridge.begin()을 호출했는지 확인하고, Python 스크립트를 Linux 측에서 실행하기 전에 스케치를 업로드했는지 확인하세요.
⚠️ 경고: 절대 Linux에서 /dev/ttyHS1을 직접 열거나 MCU에서 Serial1을 코드에서 사용하지 마세요 — 이들은 Arduino Router에서 사용되며 접근하면 Bridge가 손상됩니다.
빠른 단계
MCU 스케치 업로드: Arduino App Lab을 열고, 새 앱을 만들고, 위의 Bridge MCU 스케치를 sketch/sketch.ino에 붙여넣고, ezButton 및 Arduino_RouterBridge 라이브러리를 설치하고, Run을 클릭하세요.
Python 스크립트 추가: 위의 Python 코드를 같은 앱의 Python 탭에 붙여넣으세요.
앱 실행: Run을 클릭하세요 — Python 측이 3초마다 누르기 횟수를 폴링합니다.
버튼을 여러 번 누르세요.
콘솔 확인: Console 탭 → MCU Monitor 서브탭을 열어 누르기/놓기 이벤트를 확인하세요. Python Console 서브탭을 열어 폴링된 횟수를 확인하세요.
App Lab 콘솔 출력
DIYables_Apps
Stop
sketch.ino
1#include"Arduino_RouterBridge.h"
Serial Monitor
Python
Message (Enter to send a message to "Newbiely" on usb(2820070321))
New Line
9600 baud
Button Debounce Bridge ready
Button PRESSED — count: 1
Button RELEASED
Button PRESSED — count: 2
Button RELEASED
Button PRESSED — count: 3
Button RELEASED
/* * 이 Arduino UNO Q 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino UNO Q 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-q/arduino-uno-q-button-debounce */from arduino.app_utils import *import requestsimport timeBOT_TOKEN = "YOUR_BOT_TOKEN"API_URL = f"https://api.telegram.org/bot{BOT_TOKEN}"last_update_id = 0def send_message(chat_id, text): requests.post(f"{API_URL}/sendMessage", json={"chat_id": chat_id, "text": text})def get_updates():global last_update_id resp = requests.get(f"{API_URL}/getUpdates", params={"offset": last_update_id + 1, "timeout": 5})return resp.json().get("result", [])def loop():global last_update_id updates = get_updates()for update in updates: last_update_id = update["update_id"] msg = update.get("message", {}) chat_id = msg.get("chat", {}).get("id") text = msg.get("text", "").strip()if text == "/count": count = Bridge.call("get_press_count") send_message(chat_id, f"Button pressed {count} time(s) since last reset")elif text == "/reset": Bridge.call("reset_count") send_message(chat_id, "Press count reset to 0")else: send_message(chat_id, "Commands:\n/count — get button press count\n/reset — reset the count to 0") time.sleep(1)App.run(user_loop=loop)
참고:YOUR_BOT_TOKEN을 Telegram에서 @BotFather로부터 얻은 토큰으로 바꾸세요.
/count를 보내 전원 켜기 이후 또는 마지막 재설정 이후 버튼을 몇 번 눌렀는지 확인하세요.
/reset을 보내 카운터를 0으로 재설정하세요.
빠른 단계
MCU 스케치 업로드: 이전 섹션의 Bridge MCU 스케치를 사용하세요 (아직 하지 않았다면 먼저 업로드하세요).
Telegram 스크립트 붙여넣기: 위의 Python 코드를 Arduino App Lab의 앱 Python 탭에 복사하세요.
토큰 설정: 스크립트의 YOUR_BOT_TOKEN을 실제 봇 토큰으로 바꾸세요.
앱 실행: Run을 클릭하세요 — 봇은 즉시 Telegram 메시지 수신을 시작합니다.
테스트하기: 버튼을 5번 누른 후 Telegram에서 /count를 보내세요 — 5를 받아야 합니다. 그 다음 /reset을 보내고 /count를 다시 보내 재설정되는지 확인하세요.
팁: 이를 방문자 계수, 기계 사이클, 또는 트리거된 경고 계수에 사용하세요.
App Lab 콘솔 출력
DIYables_Apps
Stop
sketch.ino
1#include"Arduino_RouterBridge.h"
Serial Monitor
Python
[2026-04-29 12:00:01] Telegram: /count
[2026-04-29 12:00:01] Button pressed 5 time(s) since last reset
[2026-04-29 12:01:30] Telegram: /reset
[2026-04-29 12:01:30] Press count reset to 0
[2026-04-29 12:01:45] Telegram: /count
[2026-04-29 12:01:45] Button pressed 0 time(s) since last reset