Arduino Đồng hồ OLED
Trong bài hướng dẫn này, chúng ta sẽ học cách làm đồng hồ OLED bằng cách:
- Đọc thời gian (giờ, phút, giây) từ mô-đun RTC DS3231 và hiển thị trên OLED
- Đọc thời gian (giờ, phút, giây) từ mô-đun RTC DS1307 và hiển thị trên OLED
Bạn có thể chọn một trong hai mô-đun RTC: DS3231 và DS1307. Xem DS3231 so với DS1307
Phần cứng cần chuẩn bị
| 1 | × | Arduino Uno R3 | ||
| 1 | × | USB 2.0 cable type A/B (for USB-A PC) | ||
| 1 | × | USB 2.0 cable type C/B (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 Block Shield for Arduino Uno | ||
| 1 | × | (Khuyến nghị) Breadboard Shield for Arduino Uno | ||
| 1 | × | (Khuyến nghị) Enclosure for Arduino Uno | ||
| 1 | × | (Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO |
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) | ||
| 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 biết về OLED, DS3231 và DS1307 (bố trí các chân, cách chúng hoạt động, cách lập trình ...), hãy tìm hiểu về chúng trong các bài hướng dẫn sau:
Cài đặt thư viện OLED và RTC
- Điều hướng đến biểu tượng Thư việ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ấn nút Cài đặt để cài đặt thư viện.

- Bạn sẽ được yêu cầu cài đặt một số phụ thuộc thư viện khác.
- Nhấn nút Cài đặt Tất cả để cài đặt toàn bộ các phụ thuộc thư viện.

- Tìm kiếm “RTClib”, sau đó tìm thư viện RTC của Adafruit. Thư viện này hoạt động với cả DS3231 và DS1307
- Nhấn nút Cài đặt để cài đặt thư viện RTC.

- Bạn có thể được yêu cầu cài đặt thêm một số phụ thuộc thư viện khác.
- Nhấn nút Cài đặt Tất cả để cài đặt tất cả các phụ thuộc thư viện.

Đọc thời gian từ mô-đun DS3231 RTC và hiển thị lên OLED
Sơ đồ nối dây

This image is created using Fritzing. Click to enlarge image
Mã Arduino - DS3231 và OLED
/*
* Mã Arduino này được phát triển bởi newbiely.vn
* Mã Arduino 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/arduino/arduino-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();
}
Hướng dẫn từng bước
- Sao chép mã ở trên và mở bằng Arduino IDE.
- Nhấp vào nút Tải lên trên Arduino IDE để tải mã lên Arduino.
- Xem kết quả trên OLED.
Đọc thời gian từ mô-đun DS1307 RTC và hiển thị lên màn hình OLED
Sơ đồ nối dây

This image is created using Fritzing. Click to enlarge image
Mã Arduino - DS1307 và OLED
/*
* Mã Arduino này được phát triển bởi newbiely.vn
* Mã Arduino 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/arduino/arduino-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();
}
Hướng dẫn từng bước
- Sao chép mã ở trên và mở bằng Arduino IDE
- Nhấn nút Tải lên trên Arduino IDE để tải mã lên Arduino
- Xem kết quả trên màn hình 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.