ESP32 LED Nhấp Nháy Không Sử Dụng Delay

Một trong những chương trình đầu tiên mà người mới bắt đầu chạy là làm nhấp nháy một LED. Cách đơn giản nhất để nhấp nháy LED là sử dụng hàm delay(). Hàm này làm chặn ESP32 không thể thực hiện những việc khác. Nó sẽ ổn nếu bạn chỉ muốn nhấp nháy một LED duy nhất. Tuy nhiên, nếu bạn muốn nhấp nháy nhiều LED hoặc thực hiện các công việc khác song song, bạn không thể sử dụng hàm delay(). Chúng ta cần một giải pháp khác. Hướng dẫn này sẽ chỉ bạn cách thực hiện nhiều tác vụ mà không sử dụng hàm delay. Cụ thể hơn, chúng ta sẽ học cách nhấp nháy LED và kiểm tra trạng thái của nút nhấn.

Chúng ta sẽ thực hiện ba ví dụ dưới đây và so sánh sự khác biệt giữa chúng:

Phương pháp này có thể được áp dụng để ESP32 thực hiện nhiều tác vụ cùng lúc. Nhấp nháy LED chỉ là một ví dụ.

Linh Kiện 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×LED Kit
1×LED (red)
1×LED Module
1×Điện trở 220 ohm
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 Tấm
1×mô-đun nút nhấn
1×breadboard
1×Dây Jumper
1×(Tùy chọn) Jack Nguồn DC
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ề LED và Nút Nhấn

Chúng tôi có các hướng dẫn cụ thể về LED và nút nhấn. Mỗi 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 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 LED 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.

Hãy so sánh code ESP32 nhấp nháy LED có và không sử dụng hàm delay()

Code ESP32 - Có Sử Dụng Delay

/* * 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-led-blink-without-delay */ #define LED_PIN 21 // ESP32 pin GPIO21 connected to LED #define BUTTON_PIN 18 // ESP32 pin GPIO18 connected to button #define BLINK_INTERVAL 1000 // interval at which to blink LED (milliseconds) // Variables will change: int ledState = LOW; // ledState used to set the LED int previousButtonState = LOW; // will store last time button was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // if the LED is off turn it on and vice-versa: ledState = (ledState == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN, ledState); delay(BLINK_INTERVAL); // If button is pressed during this time, Arduino CANNOT detect int currentButtonState = digitalRead(BUTTON_PIN); if (currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

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

  • 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 bo mạch ESP32 với PC của bạn qua cáp micro USB
  • Mở Arduino IDE trên PC của bạn.
  • Chọn đúng bo mạch ESP32 (ví dụ: ESP32 Dev Module) và cổng COM.
  • Sao chép code trên và dán vào Arduino IDE.
  • Biên dịch và upload code lên bo mạch ESP32 bằng cách nhấn nút Upload trên Arduino IDE
cách upload code ESP32 trên Arduino ide
  • Mở Serial Monitor trên Arduino IDE
cách mở serial monitor trên Arduino ide
  • Nhấn nút 4 lần
  • Quan sát LED: LED chuyển đổi giữa BẬT/TẮT định kỳ mỗi giây
  • Xem kết quả trên Serial Monitor
COM6
Send
1 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Trên Serial Monitor, bạn sẽ không thấy đủ bốn lần trạng thái thay đổi về 0 (4 lần nhấn). Đó là vì, trong thời gian delay, ESP32 KHÔNG THỂ phát hiện sự thay đổi.

Code ESP32 - Không Sử Dụng Delay

/* * 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-led-blink-without-delay */ #define LED_PIN 21 // ESP32 pin GPIO21 connected to LED #define BUTTON_PIN 18 // ESP32 pin GPIO18 connected to button #define BLINK_INTERVAL 1000 // interval at which to blink LED (milliseconds) // Variables will change: int ledState = LOW; // ledState used to set the LED int previousButtonState = LOW; // will store last time button was updated unsigned long previousMillis = 0; // will store last time LED was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // check to see if it's time to blink the LED; that is, if the difference // between the current time and last time you blinked the LED is bigger than // the interval at which you want to blink the LED. unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= BLINK_INTERVAL) { // if the LED is off turn it on and vice-versa: ledState = (ledState == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN, ledState); // save the last time you blinked the LED previousMillis = currentMillis; } // check button state's change int currentButtonState = digitalRead(BUTTON_PIN); if (currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

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

  • 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.
  • Chạy code trên và nhấn nút 4 lần
  • Quan sát LED: LED chuyển đổi giữa BẬT/TẮT định kỳ mỗi giây
  • Xem kết quả trên Serial Monitor
COM6
Send
1 0 1 0 1 0 1 0
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Tất cả các sự kiện nhấn nút đều được phát hiện.

Giải Thích Code Từng Dòng

Code ESP32 trên có giải thích từng dòng. Vui lòng đọc các comment trong code!

Thêm Nhiều Tác Vụ Hơn

Code dưới đây làm nhấp nháy hai LED với các khoảng thời gian khác nhau và kiểm tra trạng thái của nút nhấn.

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

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-led-blink-without-delay */ #define LED_PIN_1 22 // ESP32 pin GPIO22 connected to LED #define LED_PIN_2 21 // ESP32 pin GPIO21 connected to LED #define BUTTON_PIN 18 // ESP32 pin GPIO18 connected to button #define BLINK_INTERVAL_1 1000 // interval at which to blink LED 1 (milliseconds) #define BLINK_INTERVAL_2 500 // interval at which to blink LED 2 (milliseconds) // Variables will change: int ledState_1 = LOW; // ledState used to set the LED 1 int ledState_2 = LOW; // ledState used to set the LED 2 int previousButtonState = LOW; // will store last time button was updated unsigned long previousMillis_1 = 0; // will store last time LED 1 was updated unsigned long previousMillis_2 = 0; // will store last time LED 2 was updated void setup() { Serial.begin(9600); // set the digital pin as output: pinMode(LED_PIN_1, OUTPUT); pinMode(LED_PIN_2, OUTPUT); // set the digital pin as an input: pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { unsigned long currentMillis = millis(); // check to see if it's time to blink the LED 1 if (currentMillis - previousMillis_1 >= BLINK_INTERVAL_1) { // if the LED is off turn it on and vice-versa: ledState_1 = (ledState_1 == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN_1, ledState_1); // save the last time you blinked the LED previousMillis_1 = currentMillis; } // check to see if it's time to blink the LED 2 if (currentMillis - previousMillis_2 >= BLINK_INTERVAL_2) { // if the LED is off turn it on and vice-versa: ledState_2 = (ledState_2 == LOW) ? HIGH : LOW; // set the LED with the ledState of the variable: digitalWrite(LED_PIN_2, ledState_2); // save the last time you blinked the LED previousMillis_2 = currentMillis; } // check button state's change int currentButtonState = digitalRead(BUTTON_PIN); if (currentButtonState != previousButtonState) { // print out the state of the button: Serial.println(currentButtonState); // save the last state of button previousButtonState = currentButtonState; } // DO OTHER WORKS HERE }

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.

Tài Liệu Tham Khảo Ngôn Ngữ

Bình Luận