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 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.

Arduino UNO R4 chattering phenomenon

Phần Cứng Cần Thiết

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)

Về Button

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:

Sơ Đồ Đấu Nối

Arduino UNO R4 nút nhấn sơ đồ đấu dây

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.

Arduino Uno R4 - Button không có Debounce

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ó.

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino-uno-r4/arduino-uno-r4-button-debounce */ #define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button int button_state; // the current state of button int prev_button_state = LOW; // the previous state of button void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as a pull-up input pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: 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"); // save the last state prev_button_state = button_state; }

Các Bước Thực Hiện

Thực hiện theo các hướng dẫn từng bước sau:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo tutorial về Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Đấ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.
tải mã lên Arduino IDE
  • Mở Serial Monitor.
  • Nhấn và giữ button trong vài giây, sau đó thả ra.
  • Kiểm tra kết quả trong Serial Monitor.
COM6
Send
The button is pressed The button is pressed The button is pressed The button is released The button is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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.

Arduino Uno R4 - Button có Debounce

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino-uno-r4/arduino-uno-r4-button-debounce */ #define BUTTON_PIN 7 // The Arduino UNO R4 pin connected to the button #define DEBOUNCE_TIME 50 // The debounce time; increase if the output flickers int last_steady_state = LOW; // the previous steady state from the input pin int last_flickerable_state = LOW; // the previous flickerable state from the input pin int current_state; // the current reading from the input pin unsigned long last_debounce_time = 0; // the last time the output pin was toggled void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as a pull-up input pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: current_state = digitalRead(BUTTON_PIN); // If the switch/button changed, due to noise or pressing: if (current_state != last_flickerable_state) { // reset the debouncing timer last_debounce_time = millis(); // save the the last flickerable state last_flickerable_state = current_state; } if ((millis() - last_debounce_time) > DEBOUNCE_TIME) { // if the button state has changed: 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"); // save the the last steady state last_steady_state = current_state; } }

Các Bước Thực Hiện

  • 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.
COM6
Send
The button is pressed The button is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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 Đã Làm Cho Nó Đơn Giản: Code Arduino UNO R4 Button Debounce Sử Dụng Thư Viện

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ụ.

Code Arduino UNO R4 Button Debounce Cho Một Button Đơn

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library * * This example reads the state of a button with debounce and print it to Serial Monitor. */ #include <ezButton.h> ezButton button(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) Serial.println("The button is pressed"); if(button.isReleased()) Serial.println("The button is released"); }

Code Arduino UNO R4 Button Debounce Cho Nhiều Button

Hãy debounce cho 3 button. Đây là sơ đồ đấu nối giữa Arduino UNO R4 và ba button:

Arduino UNO R4 nút nhấn thư viện sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino-uno-r4/arduino-uno-r4-button-debounce */ #include <ezButton.h> ezButton button_1(6); // initialize ezButton object connected to pin 6 ezButton button_2(7); // initialize ezButton object connected to pin 7 ezButton button_3(8); // initialize ezButton object connected to pin 8 void setup() { Serial.begin(9600); button_1.setDebounceTime(50); // configure debounce time for button_1 to 50ms button_2.setDebounceTime(50); // configure debounce time for button_2 to 50ms button_3.setDebounceTime(50); // configure debounce time for button_3 to 50ms } void loop() { button_1.loop(); // update button_1 state button_2.loop(); // update button_2 state button_3.loop(); // update button_3 state 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"); }

Video Tutorial

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.