ESP8266 Đồng hồ OLED

Hướng dẫn này chỉ cho bạn cách tạo đồng hồ OLED bằng ESP8266, module RTC và màn hình OLED. Hướng dẫn cung cấp các bước cho cả hai module RTC DS3231 và DS1307. Cụ thể:

Bạn có thể chọn một trong hai module RTC: DS3231 và DS1307. Để biết thêm thông tin, vui lòng tham khảo DS3231 vs DS1307.

Phần cứng cần chuẩn bị

1×ESP8266 NodeMCU ESP-12E
1×Recommended: ESP8266 NodeMCU ESP-12E (Uno-form)
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×SSD1306 I2C OLED Display 128x64
1×SSD1306 I2C OLED Display 128x32
1×Real-Time Clock DS3231 Module
1×(Tùy chọn) Real-Time Clock DS1307 Module
1×CR2032 battery
1×breadboard
1×dây jumper
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP8266
1×(Khuyến nghị) Power Splitter for ESP8266 Type-C

Or you can buy the following kits:

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

Về mô-đun OLED, DS3231 và DS1307 RTC

Nếu bạn chưa quen với OLED, DS3231 và DS1307 (bố trí chân, chức năng, lập trình ...), các bài hướng dẫn sau đây có thể giúp bạn:

Cài đặt thư viện OLED và RTC

  • Nhấp vào biểu tượng Thư viện ở thanh bên trái của Arduino IDE.
  • Tìm kiếm “SSD1306” và tìm thấy thư viện SSD1306 từ Adafruit.
  • Sau đó, nhấn nút Cài đặt để hoàn tất quá trình cài đặt.
thư viện oLED cho ESP8266 NodeMCU
  • Bạn sẽ được nhắc cài đặt thêm các phụ thuộc thư viện.
  • Để cài đặt chúng đồng thời, hãy nhấp vào nút Cài đặt Tất cả.
thư viện cảm biến adafruit gfx cho ESP8266 NodeMCU
  • Tìm kiếm “RTClib” và xác định thư viện RTC từ Adafruit. Thư viện này tương thích với cả DS3231 và DS1307.
  • Nhấn nút Cài đặt để cài đặt thư viện RTC.
thư viện rtc cho ESP8266 NodeMCU

Đọc thời gian từ mô-đun DS3231 RTC và hiển thị thời gian lên OLED

Sơ đồ mạch điện

sơ đồ kết nối ESP8266 NodeMCU ds3231 oLED

This image is created using Fritzing. Click to enlarge image

Xem thêm Sơ đồ chân ESP8266Cách cấp nguồn cho ESP8266.

Mã ESP8266 - DS3231 và OLED

/* * Mã ESP8266 NodeMCU này được phát triển bởi newbiely.vn * Mã ESP8266 NodeMCU 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/esp8266/esp8266-oled-clock */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <RTClib.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels Adafruit_SSD1306 oled(OLED_WIDTH, OLED_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(); oled_display_center(time); } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Hướng dẫn từng bước

Để bắt đầu với ESP8266 trên Arduino IDE, hãy làm theo các bước sau:

  • Hãy xem hướng dẫn ESP8266 - Cài đặt phần mềm nếu đây là lần đầu bạn sử dụng ESP8266.
  • Gắn các thành phần như được thể hiện trong sơ đồ.
  • Kết nối bo mạch ESP8266 với máy tính của bạn bằng cáp USB.
  • Mở Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch ESP8266 phù hợp, chẳng hạn như (ví dụ NodeMCU 1.0 (ESP-12E Module)), và cổng COM tương ứng của nó.
  • Sao chép mã và mở nó bằng Arduino IDE.
  • Nhấp nút Tải lên trong Arduino IDE để gửi mã đến ESP8266.
  • Đặt cảm biến vào nước nóng và nước lạnh, hoặc giữ nó trong tay.
  • Xem kết quả trên OLED.

Đọc thời gian từ mô-đun RTC DS1307 và hiển thị thời gian lên OLED

Sơ đồ dây điện

sơ đồ nối ESP8266 NodeMCU ds1307 oLED

This image is created using Fritzing. Click to enlarge image

Mã ESP8266 - DS1307 và OLED

/* * Mã ESP8266 NodeMCU này được phát triển bởi newbiely.vn * Mã ESP8266 NodeMCU 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/esp8266/esp8266-oled-clock */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <RTClib.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels Adafruit_SSD1306 oled(OLED_WIDTH, OLED_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(); oled_display_center(time); } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Hướng dẫn từng bước

  • Nối các thành phần như thể hiện trong sơ đồ.
  • Kết nối bo mạch ESP8266 với máy tính bằng cáp USB.
  • Mở Arduino IDE trên máy tính của bạn.
  • Chọn bo mạch ESP8266 phù hợp, ví dụ (NodeMCU 1.0 (ESP-12E Module)), và cổng COM tương ứng.
  • Sao chép mã và mở nó trong Arduino IDE.
  • Nhấp nút Upload để tải mã lên ESP8266.
  • Đặt cảm biến vào nước nóng và nước lạnh hoặc cầm nó trong tay.
  • 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.