ESP32 Đèn Giao Thông

Trong hướng dẫn này, chúng ta sẽ khám phá cách sử dụng ESP32 để điều khiển module đèn giao thông. Cụ thể, chúng ta sẽ học:

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×Module Đèn Giao Thông
1×Dây Jumper
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ề Module Đèn Giao Thông

Sơ Đồ Chân

Module đèn giao thông bao gồm 4 chân:

  • Chân GND: Chân mass, kết nối chân này với GND của ESP32.
  • Chân R: Chân điều khiển đèn đỏ, kết nối chân này với một chân digital output của ESP32.
  • Chân Y: Chân điều khiển đèn vàng, kết nối chân này với một chân digital output của ESP32.
  • Chân G: Chân điều khiển đèn xanh, kết nối chân này với một chân digital output của ESP32.
traffic light sơ đồ chân

Cách Hoạt Động

Sơ Đồ Kết Nối

  • Cách kết nối ESP32 và đèn giao thông sử dụng breadboard
ESP32 traffic light sơ đồ đấu dây

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.

how to connect ESP32 and traffic light

Cách Lập Trình Cho Module Đèn Giao Thông

  • Cấu hình các chân của ESP32 thành chế độ digital output bằng cách sử dụng hàm pinMode()
pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT);
  • Lập trình để bật đèn đỏ bằng cách sử dụng hàm digitalWrite():
digitalWrite(PIN_RED, HIGH); // turn on RED digitalWrite(PIN_YELLOW, LOW); // digitalWrite(PIN_GREEN, LOW); delay(RED_TIME); // keep red led on during a period of time

Code ESP32

/* * 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-traffic-light */ #define PIN_RED 25 // The ESP32 pin GPIO25 connected to R pin of traffic light module #define PIN_YELLOW 26 // The ESP32 pin GPIO26 connected to Y pin of traffic light module #define PIN_GREEN 27 // The ESP32 pin GPIO27 connected to G pin of traffic light module #define RED_TIME 4000 // RED time in millisecond #define YELLOW_TIME 4000 // YELLOW time in millisecond #define GREEN_TIME 4000 // GREEN time in millisecond void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // the loop function runs over and over again forever void loop() { // red light on digitalWrite(PIN_RED, HIGH); // turn on digitalWrite(PIN_YELLOW, LOW); // turn off digitalWrite(PIN_GREEN, LOW); // turn off delay(RED_TIME); // keep red light on during a period of time // yellow light on digitalWrite(PIN_RED, LOW); // turn off digitalWrite(PIN_YELLOW, HIGH); // turn on digitalWrite(PIN_GREEN, LOW); // turn off delay(YELLOW_TIME); // keep yellow light on during a period of time // green light on digitalWrite(PIN_RED, LOW); // turn off digitalWrite(PIN_YELLOW, LOW); // turn off digitalWrite(PIN_GREEN, HIGH); // turn on delay(GREEN_TIME); // keep green light on during a period of time }

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

  • Nếu đây là lần đầu bạn sử dụng ESP32, hãy xem ESP32 - Cài Đặt Phần Mềm.
  • Thực hiện kết nối như hì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 của bạn.
  • Chọn đúng board ESP32 (ví dụ: ESP32 Dev Module) và cổng COM.
  • Sao chép code trên và mở bằng Arduino IDE
  • Nhấp vào nút Upload trên Arduino IDE để upload code lên ESP32
  • Kiểm tra module đèn giao thông

Điều quan trọng cần lưu ý là cách hoạt động chính xác của đèn giao thông có thể khác nhau tùy thuộc vào thiết kế cụ thể và công nghệ được sử dụng ở các khu vực và giao lộ khác nhau. Các nguyên lý được mô tả ở trên cung cấp hiểu biết chung về cách đèn giao thông hoạt động để quản lý giao thông và tăng cường an toàn trên đường.

Code trên demonstratesratives điều khiển từng đèn riêng biệt. Bây giờ, hãy cải thiện code để tối ưu hóa tốt hơn.

Tối Ưu Hóa Code ESP32

  • Hãy cải thiện code bằng cách thực hiện một hàm để điều khiển đè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-traffic-light */ #define PIN_RED 25 // The ESP32 pin GPIO25 connected to R pin of traffic light module #define PIN_YELLOW 26 // The ESP32 pin GPIO26 connected to Y pin of traffic light module #define PIN_GREEN 27 // The ESP32 pin GPIO27 connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN }; const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME }; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // the loop function runs over and over again forever void loop() { // red light on trafic_light_on(RED); delay(times[RED]); // keep red light on during a period of time // yellow light on trafic_light_on(YELLOW); delay(times[YELLOW]); // keep yellow light on during a period of time // green light on trafic_light_on(GREEN); delay(times[GREEN]); // keep green light on during a period of time } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }
  • Hãy cải thiện code bằng cách sử dụng vòng lặp for.
/* * 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-traffic-light */ #define PIN_RED 25 // The ESP32 pin GPIO25 connected to R pin of traffic light module #define PIN_YELLOW 26 // The ESP32 pin GPIO26 connected to Y pin of traffic light module #define PIN_GREEN 27 // The ESP32 pin GPIO27 connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = {PIN_RED, PIN_YELLOW, PIN_GREEN}; const int times[] = {RED_TIME, YELLOW_TIME, GREEN_TIME}; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // the loop function runs over and over again forever void loop() { for (int light = RED; light <= GREEN; light ++) { trafic_light_on(light); delay(times[light]); // keep light on during a period of time } } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i ++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }
  • Hãy cải thiện code bằng cách sử dụng hàm millis() thay cho 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-traffic-light */ #define PIN_RED 25 // The ESP32 pin GPIO25 connected to R pin of traffic light module #define PIN_YELLOW 26 // The ESP32 pin GPIO26 connected to Y pin of traffic light module #define PIN_GREEN 27 // The ESP32 pin GPIO27 connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN }; const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME }; unsigned long last_time = 0; int light = RED; // start with RED light void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); trafic_light_on(light); last_time = millis(); } // the loop function runs over and over again forever void loop() { if ((millis() - last_time) > times[light]) { light++; if (light >= 3) light = RED; // new circle trafic_light_on(light); last_time = millis(); } // TO DO: your other code } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }

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.

Bình Luận