Building a clock with an Arduino, an OLED display, and the DS1307 Real-Time Clock (RTC) module is a fun and practical project. The DS1307 RTC module keeps accurate time even when the Arduino is powered off, while the OLED provides a sleek interface for displaying the current time. This tutorial will guide you through the process step by step.
What You Will Need
- Arduino Board (e.g., Uno, Mega, Nano)
- 0.91" OLED Display (I2C interface)
- DS1307 RTC Module
- Breadboard and Jumper Wires
- A computer with the Arduino IDE installed
- Libraries: Adafruit SSD1306, Adafruit GFX, and RTClib
Step 1: Wiring the Components
OLED Display Wiring (I2C)
OLED Pin | Arduino Pin |
---|---|
VCC | 5V |
GND | GND |
SDA | A4 |
SCL | A5 |
DS1307 RTC Module Wiring (I2C)
RTC Pin | Arduino Pin |
---|---|
VCC | 5V |
GND | GND |
SDA | A4 |
SCL | A5 |
Note: If using an Arduino board with dedicated SDA and SCL pins (e.g., Mega), connect the OLED and RTC SDA/SCL pins to those instead.
Step 2: Installing the Required Libraries
To work with the OLED display and DS1307 RTC, you need the following libraries:
- Adafruit SSD1306 (for OLED)
- Adafruit GFX (graphics library for OLED)
- RTClib (for RTC functionality)
Installing Libraries
- Open the Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries.
- Search for and install:
- "Adafruit SSD1306"
- "Adafruit GFX Library"
- "RTClib"
Step 3: Uploading the Code
Here’s the code to display the current time on the OLED using the DS1307 RTC module:
Example Code
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
// OLED display width and height
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
// Create an instance of the display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Create an instance of the RTC
RTC_DS1307 rtc;
void setup() {
Serial.begin(9600);
// Initialize the OLED display
if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
// Initialize the RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC is running
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running! Setting the time...");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now(); // Get the current time
// Clear the display
display.clearDisplay();
// Display the time
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print(now.hour(), DEC);
display.print(":");
if (now.minute() < 10) {
display.print("0");
}
display.print(now.minute(), DEC);
// Display the date
display.setTextSize(1);
display.setCursor(0, 20);
display.print(now.day(), DEC);
display.print("/");
display.print(now.month(), DEC);
display.print("/");
display.print(now.year(), DEC);
// Show the display buffer
display.display();
delay(1000); // Update every second
}
Step 4: Adjusting the Time
If the RTC is not running or the time needs to be updated, the following line in the setup()
function sets the RTC to the current time:
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
-
F(__DATE__)
andF(__TIME__)
are macros that automatically set the date and time from your computer when uploading the code.
To set a specific time manually, use:
rtc.adjust(DateTime(2025, 1, 1, 12, 0, 0)); // YYYY, MM, DD, HH, MM, SS
Step 5: Customizing the Display
You can customize the clock display by:
-
Changing Text Size: Use
display.setTextSize()
to adjust the font size. - Adding Graphics: Use the Adafruit GFX library to draw shapes like lines or rectangles.
- Formatting Time: Add AM/PM or 24-hour format as needed.
Example: Adding AM/PM
int hour = now.hour();
String period = "AM";
if (hour >= 12) {
period = "PM";
if (hour > 12) hour -= 12;
}
if (hour == 0) hour = 12;
display.print(hour);
display.print(":");
if (now.minute() < 10) {
display.print("0");
}
display.print(now.minute());
display.print(" ");
display.print(period);
Troubleshooting
-
OLED Not Displaying:
- Ensure the I2C address matches (commonly
0x3C
or0x3D
). - Check wiring connections.
- Ensure the I2C address matches (commonly
-
RTC Not Found:
- Verify SDA and SCL connections.
- Ensure the RTC battery is inserted correctly.
-
Incorrect Time:
- Use
rtc.adjust()
to set the correct time.
- Use
Applications
- DIY desk clock
- Timers and alarms
- IoT-enabled clock with additional sensors
- Educational projects
Conclusion
By combining the Arduino, a 0.91" OLED, and the DS1307 RTC, you can create an accurate and visually appealing clock. With a few tweaks, you can customize the clock to suit your needs or integrate it into larger projects. Experiment with additional features like alarms or internet synchronization to take your project to the next level!