ESP32 Nút Nhấn Debounce

Khi một nút nhấn được nhấn/thả hoặc khi một công tắc được chuyển đổi giữa ON và OFF, trạng thái của nó được thay đổi từ LOW sang HIGH (hoặc HIGH sang LOW) một lần. Điều này có đúng không?

⇒ Không, điều đó không đúng. Đó là vì trong thế giới vật lý, khi bạn thực hiện một lần nhấn duy nhất trên nút nhấn, trạng thái của nút nhấn sẽ nhanh chóng chuyển đổi giữa LOW HIGH nhiều lần thay vì chỉ một lần. Đây là đặc tính cơ học và vật lý. Hiện tượng này được biết đến với tên gọi: chattering. Hiện tượng chattering khiến cho MCU (ví dụ: ESP32) đọc được nhiều lần nhấn nút để phản hồi lại một lần nhấn thực tế duy nhất. Điều này dẫn đến trục trặc. Quá trình để loại bỏ hiện tượng này được gọi là debounce. Hướng dẫn này sẽ chỉ bạn cách thực hiện.

hiện tượng chattering ESP32

Hướng dẫn này cung cấp:

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

1×mô-đun phát triển ESP-WROOM-32
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×Cáp USB Type-C
1×Nút Nhấn Gắn Breadboard với Nắp
1×Bộ Kit Nút Nhấn 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×(Tùy chọn) DC Power Jack
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP32
1×(Khuyến nghị) Breakout Expansion Board for ESP32
1×(Khuyến nghị) Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Nút Nhấn

Chúng tôi có các hướng dẫn cụ thể về nút nhấn. Hướng dẫn chứa thông tin chi tiết và hướng dẫn từng bước về pinout phần cứng, nguyên lý hoạt động, kết nối đấu dây với ESP32, code ESP32... Tìm hiểu thêm về chúng tại các liên kết sau:

Sơ Đồ Đấu Nối

sơ Đồ Đấu nối ESP32 nút nhấn

This image is created using Fritzing. Click to enlarge image

Nếu bạn chưa rõ cách cấp nguồn cho ESP32 và các linh kiện khác, xem: Cách Cung Cấp Nguồn Điện Cho ESP32.

Để làm rõ hơn, hãy cùng chạy code ESP32 KHÔNG có và CÓ debounce, và so sánh kết quả của chúng

Đọc Nút Nhấn không có Debounce

Các Bước Nhanh

  • Nếu đây là lần đầu tiên bạn sử dụng ESP32, hãy xem ESP32 - Cài Đặt Phần Mềm.
  • Thực hiện đấu nối như hình ảnh trên.
  • Kết nối board ESP32 với PC của bạn qua cáp micro USB
  • Mở Arduino IDE trên PC.
  • Chọn đúng board ESP32 (ví dụ: ESP32 Dev Module) và cổng COM.
  • Sao chép code dưới đây và dán vào Arduino IDE.
/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 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/esp32/esp32-button-debounce */ #define BUTTON_PIN 21 // GIOP21 pin connected to button // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as an pull-up input // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); if (lastState == HIGH && currentState == LOW) Serial.println("The button is pressed"); else if (lastState == LOW && currentState == HIGH) Serial.println("The button is released"); // save the the last state lastState = currentState; }
  • Biên dịch và upload code lên board ESP32 bằng cách click nút Upload trên Arduino IDE
tải mã lên Arduino IDE
  • Mở Serial Monitor trên Arduino IDE
cách mở serial monitor trên Arduino ide
  • Nhấn nút một lần nhưng giữ trong vài giây, sau đó thả ra.
  • Xem kết quả trên Serial Monitor. Nó sẽ trông như sau:
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ỉ thực hiện một lần nhấn và thả, nhưng ESP32 đã đọc được nhiều lần nhấn và thả.

※ Lưu ý:

Hiện tượng chattering không phải lúc nào cũng xảy ra. Nếu nó không xảy ra, vui lòng thử nghiệm trên nhiều lần.

Đọc Nút Nhấn có Debounce

Các Bước Nhanh

  • Nếu đây là lần đầu tiên bạn sử dụng ESP32, hãy xem ESP32 - Cài Đặt Phần Mềm.
  • Sao chép code dưới đây và dán vào Arduino IDE.
/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 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/esp32/esp32-button-debounce */ #define BUTTON_PIN 21 // GIOP21 pin connected to button #define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters // Variables will change: int lastSteadyState = LOW; // the previous steady state from the input pin int lastFlickerableState = LOW; // the previous flickerable state from the input pin int currentState; // the current reading from the input pin // the following variables are unsigned longs because the time, measured in // milliseconds, will quickly become a bigger number than can be stored in an int. unsigned long lastDebounceTime = 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 an pull-up input // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); // check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited long enough // since the last press to ignore any noise: // If the switch/button changed, due to noise or pressing: if (currentState != lastFlickerableState) { // reset the debouncing timer lastDebounceTime = millis(); // save the the last flickerable state lastFlickerableState = currentState; } if ((millis() - lastDebounceTime) > DEBOUNCE_TIME) { // whatever the reading is at, it's been there for longer than the debounce // delay, so take it as the actual current state: // if the button state has changed: if(lastSteadyState == HIGH && currentState == LOW) Serial.println("The button is pressed"); else if(lastSteadyState == LOW && currentState == HIGH) Serial.println("The button is released"); // save the the last steady state lastSteadyState = currentState; } }
  • Biên dịch và upload code lên board ESP32 bằng cách click nút Upload trên Arduino IDE
  • Mở Serial Monitor trên Arduino IDE
cách mở serial monitor trên Arduino ide
  • Giữ nhấn nút trong vài giây sau đó thả ra.
  • Xem kết quả trên Serial Monitor. Nó sẽ trông như sau:
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 thực hiện một lần nhấn và thả, và ESP32 đọc được một lần nhấn và thả. Hiện tượng chattering đã được loại bỏ.

Chúng Tôi Đã Làm Nó Đơn Giản - Code Debounce Nút Nhấn ESP32 với Thư Viện

Để làm cho việc này dễ dàng hơn cho người mới bắt đầu, đặc biệt là khi thực hiện debounce cho nhiều nút nhấn, chúng tôi đã tạo ra một thư viện nút nhấn, gọi là ezButton. Bạn có thể tìm hiểu về thư viện ezButton tại đây.

Code Debounce Nút Nhấn ESP32 cho Một Nút Đơn

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 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/esp32/esp32-button-debounce */ #include <ezButton.h> #define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters ezButton button(21); // create ezButton object that attach to pin GPIO21 void setup() { Serial.begin(9600); button.setDebounceTime(DEBOUNCE_TIME); // 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 Debounce Nút Nhấn ESP32 cho Nhiều Nút

Hãy cùng viết code debounce cho ba nút nhấn.

Sơ đồ đấu nối

sơ Đồ Đấu nối thư viện nút nhấn ESP32

This image is created using Fritzing. Click to enlarge image

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 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/esp32/esp32-button-debounce */ #include <ezButton.h> #define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters ezButton button1(21); // create ezButton object that attach to pin GPIO21; ezButton button2(22); // create ezButton object that attach to pin GPIO22; ezButton button3(23); // create ezButton object that attach to pin GPIO23; void setup() { Serial.begin(9600); button1.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds button2.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds button3.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds } void loop() { button1.loop(); // MUST call the loop() function first button2.loop(); // MUST call the loop() function first button3.loop(); // MUST call the loop() function first if (button1.isPressed()) Serial.println("The button 1 is pressed"); if (button1.isReleased()) Serial.println("The button 1 is released"); if (button2.isPressed()) Serial.println("The button 2 is pressed"); if (button2.isReleased()) Serial.println("The button 2 is released"); if (button3.isPressed()) Serial.println("The button 3 is pressed"); if (button3.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.

Kiến Thức Bổ Sung

  • Giá trị DEBOUNCE_TIME phụ thuộc vào phần cứng. Các phần cứng khác nhau có thể sử dụng các giá trị khác nhau.
  • Debounce cũng nên được áp dụng cho công tắc on/off, limit switch, reed switch, cảm biến cảm ứng...

Bình Luận