Digital clock with RTC module and display: schematic, assembly, and commented code

ARDUINO COURSE

Building a precise, always-synchronized clock is a staple of hobby electronics. In this guide you’ll find everything you need to create a stable and reliable device thanks to an RTC (Real Time Clock) module and an I2C display. The heart of the project is an RTC chip such as the DS3231, known for its excellent accuracy and for the backup battery that keeps date and time even when the device is off. We will display the time on a 0.96″ (128×64) SSD1306 OLED via the I2C bus, which minimizes wiring and simplifies assembly.

In addition to the wiring diagram and bill of materials, you’ll find a heavily commented Arduino code example, designed to be understood, reused, and customized. The result will be an elegant, reliable project that can be easily expanded with extra features such as displaying the DS3231’s built-in temperature, 12/24h format, or alarms.

Why use an RTC module

A microcontroller by itself doesn’t keep time accurately: small instabilities of the internal clock can cause seconds or minutes of error over days. A dedicated RTC such as the DS3231 integrates a very stable oscillator and, thanks to the backup battery, preserves date and time even without power. This means instant startups and remarkable accuracy without frequent synchronizations.

Required components

  • Arduino Uno/Nano (or I2C-compatible)
  • DS3231 RTC module (with CR2032 battery)
  • SSD1306 128×64 I2C OLED display (address 0x3C or 0x3D)
  • Male-to-female jumper wires
  • 5V power supply (USB or adapter)
  • Optional: breadboard for prototyping

Wiring diagram

The I2C bus lets you connect multiple devices (RTC and OLED) using only two data lines: SDA and SCL, plus power. On the Arduino Uno/Nano the I2C lines are A4 (SDA) and A5 (SCL). On other boards (e.g., Mega, Leonardo) the dedicated I2C pins are labeled on the silkscreen.

Wiring map

  • DS3231 VCC → 5V
  • DS3231 GND → GND
  • DS3231 SDA → A4 (SDA)
  • DS3231 SCL → A5 (SCL)
  • OLED VCC → 5V (or 3.3V depending on the module)
  • OLED GND → GND
  • OLED SDA → A4 (SDA)
  • OLED SCL → A5 (SCL)

Many SSD1306 I2C OLED modules accept 3.3–5V, but check your component’s specs. If the screen shows nothing, the I2C address may be set to 0x3D instead of 0x3C: in that case update the code.

Reference ASCII schematic

Arduino UNO/Nano             DS3231 RTC           OLED SSD1306 I2C
+5V -----------------------> VCC                  VCC  GND                  GND  SDA                  SDA  SCL                  SCL <-------------- A5 (SCL)

Library installation

To keep things simple, we’ll use widely used, maintained libraries:

  • RTClib (Adafruit) to handle DS3231/DS1307
  • Adafruit GFX and Adafruit SSD1306 for the display

Open Arduino IDE → Tools → Library Manager and install “RTClib”, “Adafruit GFX Library” and “Adafruit SSD1306”. Then restart the IDE if prompted.

Commented Arduino code

The following sketch reads the date and time from the DS3231 and shows them on the OLED display. It includes an initial clock-setting function using the compile time: just set the indicated constant to true, upload the sketch once, then set it back to false and upload again.

// Clock with DS3231 (RTC) and SSD1306 OLED I2C
// Required libraries: RTClib, Adafruit GFX, Adafruit SSD1306

#include 
#include 
#include 
#include "RTClib.h"

// OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

// Constructor for SSD1306 via I2C (reset pin = -1 because not used)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// RTC object for DS3231
RTC_DS3231 rtc;

// Set to true to sync the RTC to the compile date/time.
// WARNING: use ONLY ONCE, then set back to false and re-upload.
const bool SET_RTC_AT_COMPILE_TIME = false;

// Set true for 24h format, false for 12h
const bool TWENTY_FOUR_HOUR = true;

const char* DAYS_IT[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

void setup() {
  // Start serial for debug (optional)
  Serial.begin(9600);
  delay(50);

  // Start display (address 0x3C or 0x3D)
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    // If it doesn't respond at 0x3C, try 0x3D by uncommenting the following line:
    // if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { for(;;); }
    for (;;) {
      // Halt if the display fails to initialize
    }
  }
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("Initializing...");
  display.display();

  // Start RTC
  if (!rtc.begin()) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print("RTC not found!");
    display.display();
    while (1) { delay(10); }
  }

  // If the RTC lost power, initialize the time
  if (rtc.lostPower() || SET_RTC_AT_COMPILE_TIME) {
    // Set date/time to compilation
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  // Ready message
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Ready");
  display.display();
  delay(500);
}

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

  // 12/24-hour format handling
  int hour = now.hour();
  bool pm = false;
  if (!TWENTY_FOUR_HOUR) {
    pm = (hour >= 12);
    hour = hour 
    if (hour == 0) hour = 12;
  }

  // Blink the colon to give the display "life"
  bool blink = (millis() / 1000) 
  char timeStr[10];
  // HH:MM:SS with blink on the HH:MM separator
  snprintf(timeStr, sizeof(timeStr), "
           hour, blink ? ':' : ' ', now.minute(), now.second());

  // Date string "Thu 05/10/2025"
  const char* dayName = DAYS_IT[now.dayOfTheWeek()];
  char dateStr[24];
  snprintf(dateStr, sizeof(dateStr), "
           dayName, now.day(), now.month(), now.year());

  // DS3231 internal temperature (approximate)
  float tempC = rtc.getTemperature();

  // Draw on OLED
  display.clearDisplay();

  // Large time at the top
  display.setTextSize(2);
  display.setCursor(0, 0);
  display.print(timeStr);
  if (!TWENTY_FOUR_HOUR) {
    display.setTextSize(1);
    display.setCursor(100, 2);
    display.print(pm ? "PM" : "AM");
  }

  // Date
  display.setTextSize(1);
  display.setCursor(0, 30);
  display.print(dateStr);

  // Temperature
  display.setCursor(0, 45);
  display.print("Temp: ");
  display.print(tempC, 1);
  display.print(" C");

  display.display();

  // Small delay to limit updates
  delay(200);
}

How to set the time correctly

On the first power-up, or if the RTC battery is drained, you may need to set the time. In the code, the constant SET_RTC_AT_COMPILE_TIME, when set to true, synchronizes the clock with the computer’s date and time at compile time. Upload the sketch once with true, then set it back to false and upload again to avoid overwriting at every reset.

Assembly and power tips

Use a breadboard for a prototype; for a final project, solder the connections on perfboard or a PCB and provide an enclosure with a window for the display. Keep I2C wires short to reduce noise and use clean power supplies. Make sure the DS3231’s CR2032 battery is fresh: it’s crucial to keep time when the system is off.

Quick troubleshooting

  • Blank display: check the I2C address (0x3C/0x3D) and correct power.
  • Wrong time or “restarting” time: CR2032 battery drained or not inserted.
  • Flicker: wires too long or unstable power; reduce the update delay only if necessary.
  • Compilation failed: ensure you have the latest RTClib, Adafruit GFX, and Adafruit SSD1306 libraries installed.

Possible extensions

  • Alarms and a buzzer with an audible signal at set times.
  • Light sensor to automatically adjust display brightness.
  • 12/24h format and time zone handling with buttons.
  • NTP synchronization via Wi‑Fi module (ESP8266/ESP32) for “perfect” time.
  • Calendar with weeks and weather icons if you integrate an external sensor.

Conclusions

With a few components and a shared I2C bus, you can build an elegant, accurate, and potentially expandable system. The DS3231 module offers stability and time persistence, while the SSD1306 OLED ensures crisp graphics and low power consumption. The provided code is an excellent starting point to enrich the project with custom features, from reminders to Internet synchronization.

Put it into practice now

Download the libraries, make the connections, and upload the sketch: in a few minutes you’ll have a functional clock on your desk. If this guide was helpful, share it with the community and subscribe to the newsletter to receive more practical, well-documented projects!

Se vuoi farmi qualche richiesta o contattarmi per un aiuto riempi il seguente form

    Comments