Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » ESP32 & DS3231 Based Real Time Clock (RTC) on OLED
ESP32 Projects

ESP32 & DS3231 Based Real Time Clock (RTC) on OLED

Mamtaz AlamBy Mamtaz AlamUpdated:August 21, 20223 Comments4 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

In this project, we will interface Real-Time Clock (RTC) Module DS3231 with ESP32 Board and display the Time and Date value on 0.96″ OLED Display.


Overview

In this project we will learn how to interface the RTC Module DS3231 with ESP32 Board and 0.96″ I2C OLED Display. We will use DS3231 Real Time Clock (RTC) module to keep track of the correct time & date & display it on OLED Display using ESP32 as our microcontroller.

The alternative IC for DS3231 is DS1307. The DS3231 RTC has a built-in alarm functions as well as temperature sensor with a resolution of 0.25 and an accuracy of ±3°C which make this project more easier.

You can refer to our earlier post if you wanna use DS3231 with Arduino Boards:
1. Arduino DS3231 Real Time Clock (RTC) with Temperature Monitor
2. Arduino DS3231 Real Time Clock with Alarm & Temperature
3. ESP8266 and DS3231 Based Real Time Clock (RTC)


Bill of Materials

Following are the components required for making this project. All the components can be easily purchased from Amazon. The purchase link is given as well.

S.N.Components NameQuantityPurchase Links
1ESP32 Board1Amazon | AliExpress
2RTC Module DS32311Amazon | AliExpress
3OLED Display1Amazon | AliExpress
4Connecting Wires5Amazon | AliExpress
5Breadboard1Amazon | AliExpress



DS3231 RTC Module

The DS3231 is a low-cost, extremely accurate I²C real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal. The device incorporates a battery input and maintains accurate timekeeping when the main power to the device is interrupted.

Real Time Clock DS3231

The RTC maintains seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an active-low AM/PM indicator. Two programmable time-of-day alarms and a programmable square-wave output are provided.

A precision temperature-compensated voltage reference and comparator circuit monitor the status of VCC to detect power failures, to provide a reset output, and to automatically switch to the backup supply when necessary. Additionally, the active-low RST pin is monitored as a pushbutton input for generating a µP reset.

Key Features:

1. Highly Accurate RTC Completely Manages All Timekeeping Functions
2. Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year, with Leap-Year Compensation Valid Up to 2100
3. Accuracy ±2ppm from 0°C to +40°C
4. Accuracy ±3.5ppm from -40°C to +85°C
5. Digital Temp Sensor Output: ±3°C Accuracy
6. Register for Aging Trim
7. Active-Low RST Output/Pushbutton Reset Debounce Input
8. Two Time-of-Day Alarms
9. Programmable Square-Wave Output Signal
10. Simple Serial Interface Connects to Most Microcontrollers
11. Fast (400kHz) I2C Interface
12. Battery-Backup Input for Continuous Timekeeping
13. Low Power Operation Extends Battery-Backup Run Time
14. 3.3V Operation
15. Operating Temperature Ranges: Commercial (0°C to +70°C) and Industrial (-40°C to +85°C)
16. Underwriters Laboratories® (UL) Recognized



Circuit: ESP32 & DS3231 Based Real Time Clock

Here is the circuit diagram for interfacing DS3231 Module with ESP32 & OLED. The connection is fairly simple. You can assemble the circuit in breadboard as well.

Circuit Diagram ESP32 DS3231

The DS3231 & the OLED Display both are I2C Module. So we just need 2 pins for connection. So, connect the Serial Data (SDA) pins to ESP32 GPIO21 pin & Serial Clock (SCL) to ESP32 GPIO22 pin. Supply 3.3V to OLED & RTC Module through 3.3V pin of ESP32. You can also use 5V supply for both DS3231 & OLED Module.

DS3231 ESP32 OLED


Source Code/Program

The source code for ESP32 & DS3231 RTC Module is given below. You can copy the code and upload directly to the ESP32 Board. But before that you will need few libraries. So download and add the libraries first.

1. Adafruit_SSD1306 : https://github.com/adafruit/Adafruit_SSD1306
2. Adafruit_GFX : https://github.com/adafruit/Adafruit-GFX-Library
3. RTClib : https://github.com/adafruit/RTClib



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
 
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OLED_RESET    -1  // Reset pin # (or -1 if sharing reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
 
void setup()
{
 
Serial.begin(9600);
 
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
 
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
}
 
rtc.adjust(DateTime(__DATE__, __TIME__));
 
display.display();
delay(2);
display.clearDisplay();
 
 
display.clearDisplay();
display.setTextColor(WHITE);
//display.startscrollright(0x00, 0x0F);
display.setTextSize(2);
display.setCursor(0,5);
display.print("  Clock ");
display.display();
delay(3000);
}
 
void loop()
{
DateTime now = rtc.now();
 
display.clearDisplay();
display.setTextSize(2);
display.setCursor(75,0);
display.println(now.second(), DEC);
 
display.setTextSize(2);
display.setCursor(25,0);
display.println(":");
 
display.setTextSize(2);
display.setCursor(65,0);
display.println(":");
 
display.setTextSize(2);
display.setCursor(40,0);
display.println(now.minute(), DEC);
 
display.setTextSize(2);
display.setCursor(0,0);
display.println(now.hour(), DEC);
 
display.setTextSize(2);
display.setCursor(0,20);
display.println(now.day(), DEC);
 
display.setTextSize(2);
display.setCursor(25,20);
display.println("-");
 
display.setTextSize(2);
display.setCursor(40,20);
display.println(now.month(), DEC);
 
display.setTextSize(2);
display.setCursor(55,20);
display.println("-");
 
display.setTextSize(2);
display.setCursor(70,20);
display.println(now.year(), DEC);
 
display.setTextSize(2);
display.setCursor(0,40);
display.print(daysOfTheWeek[now.dayOfTheWeek()]);
 
display.display();
}


ESP32 & DS3231 Based Real Time Clock

Once the code is uploaded the RTC Module will start working. The time and date will be displayed in OLED Display. There is no other setting required neither there is need of any extra buttons or switch.

DS3231 ESP32 OLED RTC

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleESP8266 & LoRa SX1278 Transmitter Receiver with DHT11
Next Article ESP8266 & MPU6050 Tilt Angle Monitor on IoT Blynk

Related Posts

IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

Updated:May 10, 20261K
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

Updated:May 2, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

Updated:April 27, 20261K
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

Updated:April 27, 20262K
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

Updated:February 1, 20261K
View 3 Comments

3 Comments

  1. Wolfgang on February 11, 2022 6:48 PM

    Hello, I modified the sketch to my needs. There’s just one thing I can’t figure out, how do I get 9:1:5 to 09:12:51 for example?? I don’t get a 0 in front of the date either. please support me
    Greetings Wolfgang

    Reply
  2. Steve Riley on March 20, 2023 5:43 AM

    I use this and it works
    String getPadded(int num) {
    char buff[6];
    char padded[7];
    //sprintf function will convert the long to a string
    sprintf(buff, “%.2u”, num); // buff will be “01238”
    padded[0] = buff[0];
    padded[1] = buff[1];
    padded[2] = buff[2];
    padded[3] = buff[3];
    padded[4] = buff[4];
    padded[5] = buff[5];
    padded[6] = ‘\0’; // The terminating NULL
    return String(padded);
    }

    Reply
  3. marius on August 16, 2023 10:17 AM

    the most stupid project ever,esp32 comes with an rtc build in itś core,using an another rtc is like using a clock to make a clock,how smart is that ?

    Reply

CommentsCancel reply

Latest Posts
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

May 31, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

February 1, 2026
Top Posts & Pages
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • IoT Based Drinking Water Quality Monitoring with ESP32
    IoT Based Drinking Water Quality Monitoring with ESP32
  • LD2410 Sensor with ESP32 - Human Presence Detection
    LD2410 Sensor with ESP32 - Human Presence Detection
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • DIY IoT Water pH Meter using pH Sensor & ESP32
    DIY IoT Water pH Meter using pH Sensor & ESP32
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
  • How to use Modbus RTU with ESP32 to read Sensor Data
    How to use Modbus RTU with ESP32 to read Sensor Data
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (204)
    • ESP32 MicroPython (7)
    • ESP32 Projects (81)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.