tutorialspinouttools
Sensors · ESP32-C3

DS3231 on OLED: Real-Time Clock + Temperature

Free ESP32 & Arduino tutorial · wiring, code & parts · gokul.builds

Overview & Wiring

What it is: The DS3231 is a very accurate real-time clock (±2 ppm, about a minute a year) with a coin-cell backup so it keeps time even when your project is unplugged. It also exposes its internal temperature, which it uses to stay accurate. How it works: I2C device at 0x68. On first upload the sketch sets the clock to your computer's compile time; after that the coin cell keeps it running. The library returns the date, time and chip temperature. Wiring (shared I2C bus with the OLED): DS3231 SDA -> GPIO4 DS3231 SCL -> GPIO5 DS3231 VCC -> 3V3 DS3231 GND -> GND OLED VCC=3V3, GND=GND, SDA=GPIO4, SCL=GPIO5 Libraries: "RTClib" by Adafruit (installs "Adafruit BusIO"), plus "Adafruit SSD1306" and "Adafruit GFX Library". On screen / Serial: the current time (HH:MM:SS), the date, and the chip temperature in °C. Gotchas: Insert a CR2032 backup battery or it forgets the time on power loss. The sketch sets the time to the compile moment only when it detects a power loss — to force a reset, uncomment the rtc.adjust line. Cheap modules sometimes try to recharge a non-rechargeable CR2032; that's a known quirk but harmless for hobby use.

Circuit Diagram

DS3231 on OLED: Real-Time Clock + Temperature wiring diagram for ESP32-C3

Parts You Need

ESP32-C3 SuperMiniBuy →
0.96" SSD1306 OLEDBuy →

Source Code (Arduino / ESP32)

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

Adafruit_SSD1306 display(128, 64, &Wire, -1);
RTC_DS3231 rtc;

void setup() {
  Serial.begin(115200);
  Wire.begin(4, 5);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  if (!rtc.begin()) {
    Serial.println("DS3231 not found");
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 28); display.println("RTC not found");
    display.display();
    while (1) delay(10);
  }
  if (rtc.lostPower()) {                       // first run or dead battery
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // set to compile time
  }
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // uncomment to force-reset
}

void loop() {
  DateTime now = rtc.now();
  float t = rtc.getTemperature();

  char timeStr[9], dateStr[11];
  sprintf(timeStr, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
  sprintf(dateStr, "%04d-%02d-%02d", now.year(), now.month(), now.day());

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(2);
  display.setCursor(0, 4);  display.println(timeStr);
  display.setTextSize(1);
  display.setCursor(0, 34); display.println(dateStr);
  display.setCursor(0, 50); display.printf("chip %.1f C", t);
  display.display();

  Serial.printf("%s %s  (%.1f C)\n", dateStr, timeStr, t);
  delay(1000);
}
Browse all tutorials ESP32-C3 pinout Calculators