Arduino UNO R4 Button Debounce
Khi lập trình Arduino Uno R4 để phát hiện sự kiện nhấn nút (button press), bạn có thể thấy rằng một lần nhấn được phát hiện nhiều lần. Điều này xảy ra vì do các yếu tố cơ học, button hoặc switch có thể chuyển đổi nhanh chóng giữa LOW và HIGH nhiều lần. Hiện tượng này được gọi là "chattering" (rung phím). Chattering có thể khiến một lần nhấn nút được phát hiện như nhiều lần nhấn, điều này có thể gây ra lỗi trong một số ứng dụng. Tutorial này giải thích cách khắc phục vấn đề này, một quá trình được gọi là debouncing button.

| 1 | × | Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima | |
| 1 | × | (Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi | | |
| 1 | × | Arduino UNO R4 Minima (Thay thế) | | |
| 1 | × | Cáp USB Type-C | | |
| 1 | × | Button gắn Breadboard có nắp | | |
| 1 | × | Bộ Kit Button gắn Breadboard | | |
| 1 | × | Nút Nhấn gắn Panel | | |
| 1 | × | mô-đun nút nhấn | | |
| 1 | × | breadboard | | |
| 1 | × | Dây Jumper | | |
| 1 | × | (Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4 | | |
| 1 | × | (Khuyến nghị) Breadboard Shield for Arduino UNO R4 | | |
| 1 | × | (Khuyến nghị) Enclosure for Arduino UNO R4 | | |
| 1 | × | (Khuyến nghị) Power Splitter for Arduino UNO R4 | | |
| 1 | × | (Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO | | |
Or you can buy the following kits:
| 1 | × | DIYables STEM V4 IoT Starter Kit (Arduino included) | | |
| 1 | × | DIYables Sensor Kit (30 sensors/displays) | | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) | | |
Tìm hiểu về button (pinout, hoạt động, lập trình) trong các tutorial sau nếu bạn chưa quen thuộc với chúng:

This image is created using Fritzing. Click to enlarge image
Hãy cùng kiểm tra và so sánh code Arduino UNO R4 không có và có debounce, và quan sát hoạt động của chúng.
Trước khi chúng ta tìm hiểu về debouncing, hãy xem code không có debounce và quan sát cách hoạt động của nó.
#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;
}
Thực hiện theo các hướng dẫn từng bước sau:
Đấu nối các linh kiện theo sơ đồ được cung cấp.
Kết nối board Arduino Uno R4 với máy tính của bạn bằng cáp USB.
Khởi động Arduino IDE trên máy tính của bạn.
Chọn board Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và COM port.
Sao chép code trên và mở trong Arduino IDE.
Nhấn nút Upload trong Arduino IDE để gửi code đến Arduino UNO R4.
The button is pressed
The button is pressed
The button is pressed
The button is released
The button is released
Như bạn có thể thấy, bạn chỉ nhấn và thả button một lần. Tuy nhiên, Arduino nhận biết nó như nhiều lần nhấn và thả.
※ Lưu ý:
Giá trị của DEBOUNCE_TIME thay đổi tùy theo từng ứng dụng khác nhau. Mỗi ứng dụng có thể sử dụng một giá trị riêng.
#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;
}
}
Sao chép code trên và mở với Arduino IDE.
Nhấn nút Upload trong Arduino IDE để gửi code đến Arduino UNO R4.
Mở Serial Monitor.
Nhấn giữ button trong vài giây trước khi thả ra.
Kiểm tra kết quả trong Serial Monitor.
The button is pressed
The button is released
Như bạn có thể thấy, bạn nhấn và thả button một lần. Arduino phát hiện chính xác như một lần nhấn và thả, loại bỏ mọi hiện tượng chattering.
Chúng tôi đã tạo ra một cách đơn giản hơn cho người mới bắt đầu sử dụng nhiều button bằng cách tạo một thư viện tên là ezButton. Bạn có thể tìm hiểu thêm về thư viện ezButton tại đây.
Hãy xem một số code ví dụ.
#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");
}
Hãy debounce cho 3 button. Đây là sơ đồ đấu nối giữa Arduino UNO R4 và ba button:

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");
}
Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.