ESP32 Đồng hồ OLED

Trong hướng dẫn này, chúng tôi sẽ hướng dẫn bạn qua quy trình tạo một chiếc đồng hồ OLED bằng ESP32 thông qua các bước sau:

Bạn có sự linh hoạt để lựa chọn giữa hai module RTC: DS3231 và DS1307. Để giúp bạn đưa ra quyết định sáng suốt, bạn có thể tham khảo so sánh được nêu trong DS3231 vs DS1307.

Hướng dẫn này sẽ cung cấp một hướng dẫn toàn diện để thực hiện một chiếc đồng hồ OLED, trình bày sự tích hợp của ESP32 với module RTC DS3231 hoặc DS1307 để hiển thị thông tin thời gian chính xác trên màn hình OLED.

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×Màn hình OLED SSD1306 I2C 128x64
1×Màn hình OLED SSD1306 I2C 128x32
1×Module Real-Time Clock DS3231
1×Module Real-Time Clock DS1307 (Tùy chọn)
1×Pin CR2032
1×breadboard
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ề OLED, DS3231 và module RTC DS1307

Chưa quen với OLED, DS3231 và DS1307, bao gồm pinout, chức năng và lập trình? Khám phá các hướng dẫn toàn diện về những chủ đề này dưới đây:

Cài Đặt Thư Viện OLED và RTC

  • Nhấp vào biểu tượng Libraries trên thanh bên trái của Arduino IDE.
  • Tìm kiếm "SSD1306", sau đó tìm thư viện SSD1306 của Adafruit
  • Nhấp nút Install để cài đặt thư viện.
ESP32 oLED thư viện
  • Bạn sẽ được yêu cầu cài đặt một số thư viện phụ thuộc khác
  • Nhấp nút Install All để cài đặt tất cả thư viện phụ thuộc.
ESP32 adafruit gfx sensor thư viện
  • Tìm kiếm "RTClib", sau đó tìm thư viện RTC của Adafruit
  • Nhấp nút Install để cài đặt thư viện RTC.
ESP32 rtc thư viện
  • Bạn có thể được yêu cầu cài đặt các thư viện phụ thuộc
  • Cài đặt tất cả thư viện phụ thuộc bằng cách nhấp nút Install All.
ESP32 adafruit busio thư viện

Đọc thời gian từ module RTC DS3231 và hiển thị trên OLED

Sơ Đồ Kết Nối

ESP32 ds3231 oLED 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.

Code ESP32 - DS3231 và OLED

/* * 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-oled-clock */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <RTClib.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // // create SSD1306 display object connected to I2C RTC_DS3231 rtc; String time; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(1); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display // SETUP RTC MODULE if (! rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (true); } // automatically sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); time.reserve(10); // to avoid fragmenting memory when using String } void loop() { DateTime now = rtc.now(); time = ""; time += now.hour(); time += ':'; time += now.minute(); time += ':'; time += now.second(); oledDisplayCenter(time); } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

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

  • Mới sử dụng ESP32? Hãy bắt đầu với hướng dẫn ESP32 - Cài Đặt Phần Mềm để học các kiến thức cơ bản trước.
  • Thực hiện kết nối dây như hình trên.
  • Kết nối bo mạch ESP32 với PC qua cáp micro USB
  • Mở Arduino IDE trên PC.
  • Chọn đúng bo mạch ESP32 (ví dụ: ESP32 Dev Module) và cổng COM.
  • Sao chép code trên và mở bằng Arduino IDE
  • Nhấp nút Upload trên Arduino IDE để upload code lên ESP32
  • Xem kết quả trên OLED

Đọc thời gian từ module RTC DS1307 và hiển thị trên OLED

Sơ Đồ Kết Nối

ESP32 ds1307 oLED sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code ESP32 - DS1307 và OLED

/* * 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-oled-clock */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <RTClib.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // // create SSD1306 display object connected to I2C RTC_DS1307 rtc; String time; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(1); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display // SETUP RTC MODULE if (! rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (true); } // automatically sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); time.reserve(10); // to avoid fragmenting memory when using String } void loop() { DateTime now = rtc.now(); time = ""; time += now.hour(); time += ':'; time += now.minute(); time += ':'; time += now.second(); oledDisplayCenter(time); } void oledDisplayCenter(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // display on horizontal and vertical center oled.clearDisplay(); // clear display oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

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

  • Mới sử dụng ESP32? Hãy bắt đầu với hướng dẫn ESP32 - Cài Đặt Phần Mềm để học các kiến thức cơ bản trước.
  • Thực hiện kết nối dây như hình trên.
  • Kết nối bo mạch ESP32 với PC qua cáp micro USB
  • Mở Arduino IDE trên PC.
  • Chọn đúng bo mạch ESP32 (ví dụ: ESP32 Dev Module) và cổng COM.
  • Sao chép code trên và mở bằng Arduino IDE
  • Nhấp nút Upload trên Arduino IDE để upload code lên ESP32
  • Xem kết quả trên OLED

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