Jump to content

RTC Module DS3231

From cognifront

Interfacing DS3231 RTC Module with ESP32

Interfacing DS3231 RTC Module with ESP32 demonstrates how a DS3231 Real-Time Clock (RTC) module can be interfaced with an ESP32 board using the I²C communication protocol to read and display the current date and time.

The DS3231 is a highly accurate real-time clock module that keeps track of date and time information.

DS3231 RTC → I²C → ESP32 → Serial Monitor

Objectives

  • To study the working of the DS3231 RTC module.
  • To interface the DS3231 RTC module with the ESP32 board.
  • To communicate with the RTC using the I²C protocol.
  • To read the current date and time from the DS3231.
  • To display the date and time on the Serial Monitor.

Software

  • Arduino IDE
  • RTClib Library

Hardware Modules

  • ESP32 Board
  • DS3231 RTC Module
  • PC / Laptop
  • Connecting wires

Theory

Introduction to RTC

RTC stands for Real-Time Clock.

A real-time clock is an electronic device used to keep track of the current date and time.

The DS3231 is a real-time clock module that provides date and time information including:

  • Seconds
  • Minutes
  • Hours
  • Day
  • Month
  • Year

The DS3231 communicates with the ESP32 using the I²C communication protocol.

DS3231 → Date & Time Data → ESP32

The RTC module can maintain the date and time even when the main power supply is removed, provided that its backup battery is available.

I²C Communication

The DS3231 communicates with the ESP32 using two I²C signal lines:

Signal ESP32 Pin Function
SDA GPIO 21 Serial Data
SCL GPIO 22 Serial Clock

The SDA line is used to transfer data between the ESP32 and DS3231.

The SCL line provides the clock signal required for I²C communication.

RTC Module with attached cell CR2032 connected to ESP32

Connections

DS3231 Pin ESP32 Function
SDA GPIO 21 I²C Data
SCL GPIO 22 I²C Clock
VCC 3.3V Power supply
GND GND Ground

RTClib Library

The program uses the RTClib library to communicate with the DS3231 RTC module.

The following header file is included:

#include <RTClib.h>

The DS3231 RTC object is created using:

RTC_DS3231 rtc;

The library provides functions for initializing the RTC and reading the current date and time.

RTC Initialization

The ESP32 initializes the I²C communication using:

Wire.begin(SDA_PIN, SCL_PIN);

where:

Parameter Value
SDA GPIO 21
SCL GPIO 22

The program then checks whether the DS3231 RTC is detected.

If the RTC is not detected, the following message is displayed:

DS3231 NOT FOUND!

If the RTC is detected successfully, the following message is displayed:

DS3231 Ready

RTC Lost Power

The program checks whether the RTC has lost its power using:

rtc.lostPower()

If the RTC has lost power, the program displays:

RTC lost power!

The RTC is then adjusted using the date and time obtained during program compilation:

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

This sets the RTC to the date and time available when the program is compiled.

Reading Date and Time

The current date and time are obtained using:

DateTime now = rtc.now();

The program reads the following values:

Function Information
now.day() Day
now.month() Month
now.year() Year
now.hour() Hour
now.minute() Minute
now.second() Second

Algorithm

The following algorithm is used to interface the DS3231 RTC module with the ESP32:

  1. Start the Arduino IDE.
  2. Include the Wire library for I²C communication.
  3. Include the RTClib library.
  4. Define GPIO 21 as the SDA pin.
  5. Define GPIO 22 as the SCL pin.
  6. Create the DS3231 RTC object.
  7. In the void setup() function, start Serial communication at 115200 baud.
  8. Initialize I²C communication using GPIO 21 and GPIO 22.
  9. Check whether the DS3231 RTC is detected.
  10. If the RTC is not detected, display an error message and stop execution.
  11. Check whether the RTC has lost power.
  12. If the RTC has lost power, adjust the RTC using the compilation date and time.
  13. Display the DS3231 Ready message.
  14. In the void loop() function, read the current date and time using rtc.now().
  15. Display the date on the Serial Monitor.
  16. Display the time on the Serial Monitor.
  17. Wait for 1000 ms.
  18. Repeat the process continuously.

The overall operation is:

DS3231 → I²C → ESP32 → Date & Time → Serial Monitor

Working

The ESP32 communicates with the DS3231 RTC module through the I²C interface.

The I²C pins used in this practical are:

SDA = GPIO 21

SCL = GPIO 22

After initialization, the ESP32 checks whether the DS3231 is available.

If the RTC is detected, the program reads the current date and time.

DS3231 → Current Date & Time → ESP32

The ESP32 then displays the information on the Serial Monitor.

The date is displayed in the following format:

Date: DD/MM/YYYY

The time is displayed in the following format:

Time: HH:MM:SS

The reading is updated once every second.

Serial Monitor Output

The Serial Monitor operates at 115200 baud.

A typical output format is:

DS3231 Ready
Date: 4/9/2026  Time: 13:25:10
Date: 4/9/2026  Time: 13:25:11
Date: 4/9/2026  Time: 13:25:12

If the RTC is not detected, the output is:

DS3231 NOT FOUND!

If the RTC has lost power, the output includes:

RTC lost power!

Timing

The program reads and displays the RTC date and time once every 1000 ms.

The sequence is:

Read RTC → Display Date & Time → 1 Second Delay → Read RTC → Repeat

Program

// © 2026, Cognifront
// DS3231 RTC with ESP32
// SDA = GPIO 21, SCL = GPIO 22

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

#define SDA_PIN 21
#define SCL_PIN 22

RTC_DS3231 rtc;

void setup() {
  Serial.begin(115200);
  Wire.begin(SDA_PIN, SCL_PIN);

  if (!rtc.begin()) {
    Serial.println("DS3231 NOT FOUND!");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  Serial.println("DS3231 Ready");
}

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

  Serial.print("Date: ");
  Serial.print(now.day());
  Serial.print("/");
  Serial.print(now.month());
  Serial.print("/");
  Serial.print(now.year());

  Serial.print("  Time: ");
  Serial.print(now.hour());
  Serial.print(":");
  Serial.print(now.minute());
  Serial.print(":");
  Serial.println(now.second());

  delay(1000);
}

Observation

Observe the output on the Serial Monitor at a baud rate of 115200.

The current date and time obtained from the DS3231 RTC are displayed.

The time values change continuously, with the output being updated once every second.

The date is displayed in the format:

Day / Month / Year

The time is displayed in the format:

Hour : Minute : Second

Result

The ESP32 successfully interfaces with the DS3231 Real-Time Clock module using I²C communication.

The ESP32 reads the current date and time from the RTC and displays the information on the Serial Monitor.