Creating a Clock with the Arduino, a 0.91" OLED, and the DS1307 RTC

Creating a Clock with the Arduino, a 0.91" OLED, and the DS1307 RTC

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

  1. Arduino Board (e.g., Uno, Mega, Nano)
  2. 0.91" OLED Display (I2C interface)
  3. DS1307 RTC Module
  4. Breadboard and Jumper Wires
  5. A computer with the Arduino IDE installed
  6. 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:

  1. Adafruit SSD1306 (for OLED)
  2. Adafruit GFX (graphics library for OLED)
  3. RTClib (for RTC functionality)

Installing Libraries

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. 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__) and F(__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:

  1. Changing Text Size: Use display.setTextSize() to adjust the font size.
  2. Adding Graphics: Use the Adafruit GFX library to draw shapes like lines or rectangles.
  3. 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

  1. OLED Not Displaying:

    • Ensure the I2C address matches (commonly 0x3C or 0x3D).
    • Check wiring connections.
  2. RTC Not Found:

    • Verify SDA and SCL connections.
    • Ensure the RTC battery is inserted correctly.
  3. Incorrect Time:

    • Use rtc.adjust() to set the correct time.

Applications

  1. DIY desk clock
  2. Timers and alarms
  3. IoT-enabled clock with additional sensors
  4. 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!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.