Embedded SystemsBeginnerPRJ-141

Digital Clock with RTC and OLED Using Arduino

An accurate clock that keeps time even when the power is off.

Built and tested by Anju.A · Industrial Robotics Institute

Arduino Uno

Arduino IDE, RTClib library, Adafruit GFX library, Adafruit SSD1306 library

LevelBeginner
Build time1–2 hours
BoardArduino Uno
Parts7 components

What is the Digital Clock with RTC and OLED?

A digital clock that shows the time and date on a 0.96" OLED. A DS3231 Real-Time Clock module keeps accurate time with its own backup battery, so the clock is still correct after a power cut. The Arduino reads the RTC over I2C and updates the display every second.

How it works

  1. The DS3231 RTC keeps counting time on its backup battery.
  2. The Arduino reads the current time and date over I2C.
  3. It shows HH:MM:SS in large text and the date underneath on the OLED.
  4. The display refreshes every second.

Parts list

ComponentQty
Arduino Uno1
RTC Module (DS3231 / DS1307)1
OLED Display (SSD1306, 0.96", I2C)1
RTC Backup Battery (CR2032)1
Breadboard1
Jumper WiresAs needed
USB Cable1

Software

  • Arduino IDE
  • RTClib library
  • Adafruit GFX library
  • Adafruit SSD1306 library

Wiring and connections

FromConnect to
RTC VCC5V
RTC GNDGND
RTC SDAA4
RTC SCLA5
OLED VCC5V
OLED GNDGND
OLED SDAA4 (shared)
OLED SCLA5 (shared)

How to build the Digital Clock with RTC and OLED

  1. Connect the RTC and the OLED to the same I2C pins: SDA to A4 and SCL to A5.
  2. Insert the backup battery in the RTC.
  3. Install the RTClib, Adafruit GFX and Adafruit SSD1306 libraries.
  4. The first time, un-comment the rtc.adjust line, upload once to set the time, then comment it out again and re-upload.
  5. The clock now keeps time even when unplugged.

Digital Clock with RTC and OLED code

Complete, tested Arduino C++ code for this project. Copy it into Arduino IDE and upload it to the Arduino Uno.

#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

RTC_DS3231 rtc;

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void print2(int v) {          // show leading zero, e.g. 09
  if (v < 10) display.print('0');
  display.print(v);
}

void setup() {
  Serial.begin(9600);

  if (!rtc.begin()) {
    Serial.println("RTC not found!");
    while (1);
  }

  // Set time only once, then comment this line and upload again
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("OLED not found!");
    while (1);
  }
  display.clearDisplay();
}

void loop() {
  DateTime now = rtc.now();

  display.clearDisplay();
  display.setTextColor(WHITE);

  display.setTextSize(2);
  display.setCursor(10, 10);
  print2(now.hour());   display.print(":");
  print2(now.minute()); display.print(":");
  print2(now.second());

  display.setTextSize(1);
  display.setCursor(20, 40);
  print2(now.day());    display.print("/");
  print2(now.month());  display.print("/");
  display.print(now.year());

  display.display();
  delay(1000);
}

Real-world uses

  • Desk and wall clocks
  • Time-stamping data loggers
  • School bell and timer projects
  • Learning I2C devices

Frequently asked questions

What is a Digital Clock with RTC and OLED?

A digital clock that shows the time and date on a 0.96" OLED. A DS3231 Real-Time Clock module keeps accurate time with its own backup battery, so the clock is still correct after a power cut.

What components are needed for the Digital Clock with RTC and OLED?

You need: Arduino Uno (1), RTC Module (DS3231 / DS1307) (1), OLED Display (SSD1306, 0.96", I2C) (1), RTC Backup Battery (CR2032) (1), Breadboard (1), Jumper Wires, USB Cable (1). Software: Arduino IDE, RTClib library, Adafruit GFX library, Adafruit SSD1306 library.

Is the Digital Clock with RTC and OLED project good for beginners?

Yes. It is rated beginner and takes about 1–2 hours to build, using the Arduino Uno. The guide on this page explains every connection and step.

Where can I get the code for the Digital Clock with RTC and OLED?

The complete, tested Arduino C++ code is free on this page. Copy it into Arduino IDE and upload it to the board.

Can I buy a kit for the Digital Clock with RTC and OLED?

Yes. IRI sells a kit with the parts for this project (PRJ-141). Message IRI on WhatsApp at +91 86060 82634 for the kit price and delivery details.

Industrial Robotics Institute
Logo