DS3231 / AT24C32 Real-Time Clock (RTC)

View on Amazon
Overview
About DS3231 / AT24C32 Real-Time Clock (RTC)
The DS3231 is an ultra-accurate I²C real-time clock (RTC) with a built-in temperature-compensated crystal oscillator (TCXO), ensuring stable timekeeping even under temperature fluctuations. It provides leap-year compensation up to 2100 and supports battery backup for uninterrupted operation.
⚡ Key Features
- High-Precision RTC – More accurate than the DS1307.
- Built-in Temperature Compensation – Ensures consistent timekeeping across temperature changes.
- Battery Backup Support – Keeps time even during power loss.
- I²C Communication – Simple integration with ESP32, Arduino, and other microcontrollers.
With its precision and reliability, the DS3231 is perfect for data logging, alarms, scheduling, and real-time event tracking. 🚀
Get Your DS3231 / AT24C32
💡 Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
DS3231 / AT24C32 Specifications
Complete technical specification details for DS3231 / AT24C32 Real-Time Clock (RTC)
📊 Technical Parameters
DS3231 / AT24C32 Pinout
The DS3231 pinout includes I2C communication pins (SDA, SCL), power supply (VCC, VBAT), ground, interrupt/square wave output (INT/SQW), 32kHz output, and reset pin. It features a built-in temperature-compensated crystal oscillator (TCXO).
Visual Pinout Diagram

Pin Types
Quick Tips
RTC with built-in TCXO (Temperature-Compensated Crystal Oscillator),Accuracy: ±2ppm (0°C to +40°C), ±3.5ppm (-40°C to +85°C),Real-time clock: seconds, minutes, hours, day, date, month, year,Leap year compensation up to 2100
programmable time-of-day alarms,I2C interface with address 0x68,Operating voltage: 2.3V to 5.5V (both VCC and VBAT)
temperature sensor (±3°C accuracy),No external crystal required (built-in TCXO),Battery backup with automatic switchover
Pin Descriptions
| Pin Name | Type | Description | Notes |
|---|---|---|---|
1 VCC | Power | Primary power supply input (2.3V to 5.5V) | Main power source, wide voltage range |
2 GND | Ground | Ground connection | Common ground |
3 SDA | I2C Data | I2C Serial Data line | Bidirectional data line (requires pull-up) |
4 SCL | I2C Clock | I2C Serial Clock line | Clock line (requires pull-up) |
5 VBAT | Backup Power | Battery backup input (2.3V to 5.5V) | CR2032 battery for timekeeping during power loss |
6 INT/SQW | Output | Interrupt or square wave output | Programmable alarm interrupt or square wave |
7 32KHz | Output | 32.768 kHz output | Optional 32kHz clock output |
8 RST | Control | Reset input/output | Optional reset control (active low) |
Wiring DS3231 / AT24C32 to ESP32
Connect the DS3231 to your ESP32 via I2C (SDA and SCL pins). The module has a wide voltage range (2.3V to 5.5V) and includes a built-in temperature-compensated crystal oscillator for superior accuracy. A CR2032 battery provides backup power.
Visual Wiring Diagram

Connection Status
Protocol
Pin Connections
| DS3231 / AT24C32 Pin | Connection | ESP32 Pin | Description |
|---|---|---|---|
1 VCC Required | 3.3V or 5V | Primary power supply (2.3V to 5.5V) | |
2 GND Required | GND | Ground connection | |
3 SDA Required | GPIO21 | I2C data line (with 4.7kΩ pull-up) | |
4 SCL Required | GPIO22 | I2C clock line (with 4.7kΩ pull-up) | |
5 VBAT Optional | CR2032 Battery | Backup battery (3V) | |
6 INT/SQW Optional | GPIO (optional) | Interrupt/square wave output | |
7 32KHz Optional | GPIO (optional) | 32kHz clock output |
address: 0x68 (same as DS1307, cannot coexist on same bus)
with both 3.3V and 5V (ESP32 uses 3.3V)
resistors (4.7kΩ) required on SDA and SCL
modules include pull-up resistors and CR2032 battery holder
more accurate than DS1307 due to TCXO
temperature compensation eliminates crystal drift
external crystal needed (TCXO integrated)
can trigger interrupts for alarms
RTClib or DS3231 library for Arduino/ESP32
choice for precision timekeeping applications
includes AT24C32 EEPROM (32KB) on same module
DS3231 / AT24C32 Troubleshooting
Common issues and solutions to help you get your sensor working
Common Issues
Issue: The DS3231 RTC module displays a constant time or advances time incorrectly.
Possible causes include insufficient power supply, incorrect wiring, or a defective module.
Solution: Ensure that the module is connected to a stable power source, with VCC connected to 5V and GND to ground. Verify that the SDA and SCL pins are correctly connected to the appropriate digital pins on the microcontroller. If the problem persists, consider replacing the DS3231 module, as some units, especially from unreliable sources, may be faulty.
Issue: The DS3231 module displays incorrect or corrupted date and time information.
Possible causes include improper initialization, incorrect data retrieval methods, or communication errors.
Solution: Ensure that the RTC is properly initialized in your code, disabling write protection and setting the clock to run mode. Use reliable libraries and functions to set and retrieve time data. Verify that the communication between the microcontroller and the RTC is functioning correctly, and consider implementing error-checking mechanisms to detect and handle communication issues.
Issue: The DS3231 module becomes excessively hot during operation.
Possible causes include incorrect power connections, short circuits, or defective components.
Solution: Double-check all power connections to ensure they are correct, with VCC connected to the appropriate voltage and GND to ground. Inspect the module and wiring for any signs of short circuits or solder bridges. If the module continues to overheat, it may be defective and should be replaced.
Issue: The DS3231 RTC loses track of time after a power cycle.
Possible causes include a missing or depleted backup battery, or incorrect wiring of the backup power supply.
Solution: Install a backup battery (e.g., a CR2032 coin cell) to the VBAT pin to maintain timekeeping during power loss. Ensure that the battery is fresh and properly connected. Verify that the VCC pin is connected to the main power supply, and that the module is configured to switch to the backup battery when the main power is unavailable.
Debugging Tips
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
DS3231 / AT24C32 Programming Examples
Ready-to-use code examples for different platforms and frameworks
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(2023, 12, 4, 14, 30, 0)); // Set initial date/time
}
}
void loop() {
DateTime now = rtc.now();
Serial.print("Time: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
Serial.print("Date: ");
Serial.print(now.year());
Serial.print("/");
Serial.print(now.month());
Serial.print("/");
Serial.println(now.day());
delay(1000);
}RTClib library. In the setup() function, the RTC is initialized, and if it has lost power, the date and time are set. The loop() function retrieves the current time and date from the RTC and prints them to the Serial Monitor every second.#include <stdio.h>
#include "driver/i2c.h"
#include "ds3231.h"
#define I2C_MASTER_SCL_IO 22
#define I2C_MASTER_SDA_IO 21
void app_main(void) {
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000
};
i2c_param_config(I2C_NUM_0, &i2c_config);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
ds3231_init(I2C_NUM_0);
ds3231_set_datetime(2023, 12, 4, 14, 30, 0);
while (1) {
ds3231_datetime_t now;
ds3231_get_datetime(&now);
printf("Time: %02d:%02d:%02d\n", now.hour, now.minute, now.second);
printf("Date: %04d/%02d/%02d\n", now.year, now.month, now.day);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}ds3231_set_datetime(). The current time and date are retrieved with ds3231_get_datetime() and displayed on the console every second.i2c:
sda: GPIO21
scl: GPIO22
time:
- platform: ds3231
id: ds3231_time
update_interval: 1s
sensor:
- platform: custom
lambda: |-
auto my_sensor = new DS3231Sensor(id(ds3231_time));
return {my_sensor};
sensors:
- name: "DS3231 Date and Time"time platform fetches the date and time at 1-second intervals. A custom sensor processes and displays the time and date in a human-readable format.platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200main.cpp
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA: GPIO21, SCL: GPIO22
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(2023, 12, 4, 14, 30, 0)); // Set initial date/time
}
}
void loop() {
DateTime now = rtc.now();
Serial.print("Time: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
Serial.print("Date: ");
Serial.print(now.year());
Serial.print("/");
Serial.print(now.month());
Serial.print("/");
Serial.println(now.day());
delay(1000);
}loop() retrieves the current time and date and prints them every second.from machine import I2C, Pin
import time
# DS3231 I2C address
DS3231_ADDRESS = 0x68
def bcd_to_decimal(bcd):
return (bcd >> 4) * 10 + (bcd & 0x0F)
def decimal_to_bcd(decimal):
return ((decimal // 10) << 4) | (decimal % 10)
def set_time(i2c, year, month, day, hour, minute, second):
data = [decimal_to_bcd(second), decimal_to_bcd(minute), decimal_to_bcd(hour),
decimal_to_bcd(day), 0, decimal_to_bcd(month), decimal_to_bcd(year - 2000)]
i2c.writeto_mem(DS3231_ADDRESS, 0x00, bytes(data))
def get_time(i2c):
data = i2c.readfrom_mem(DS3231_ADDRESS, 0x00, 7)
second = bcd_to_decimal(data[0])
minute = bcd_to_decimal(data[1])
hour = bcd_to_decimal(data[2])
day = bcd_to_decimal(data[4])
month = bcd_to_decimal(data[5] & 0x1F)
year = bcd_to_decimal(data[6]) + 2000
return year, month, day, hour, minute, second
# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Set initial time
set_time(i2c, 2023, 12, 4, 14, 30, 0)
# Loop to read time
while True:
year, month, day, hour, minute, second = get_time(i2c)
print(f"Time: {hour:02}:{minute:02}:{second:02}, Date: {year:04}/{month:02}/{day:02}")
time.sleep(1)set_time() function sets the date and time on the DS3231 by writing BCD-encoded values to its memory. The get_time() function reads the current date and time from the DS3231, decodes the BCD values into integers, and returns them in a human-readable format. The main loop continuously fetches the current time and date from the DS3231 and prints them every second.Wrapping Up DS3231 / AT24C32
The ESP32 DS3231 / AT24C32 Real-Time Clock (RTC) is a powerful RTC sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
Best Practices
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Safety First
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.
Ready to Start Building?
Now that you have all the information you need, it's time to integrate the DS3231 / AT24C32 into your ESP32 project and bring your ideas to life!
Explore Alternative Sensors
Looking for alternatives to the DS3231 / AT24C32? Check out these similar sensors that might fit your project needs.

DS1302 Real-Time Clock (RTC)
The DS1302 is a cost-effective real-time clock module designed for accurate timekeeping. It operates on a 3-wire serial protocol and...

DS1307 Real-Time Clock (RTC)
The DS1307 is a widely used real-time clock module with I2C communication. It supports leap year compensation, battery-backed operation, and...

PCF8563 Real-Time Clock (RTC)
The PCF8563 is a low-power real-time clock/calendar with I2C interface, offering timekeeping functions, programmable clock output, alarm and...





