How to wire a DS3231 RTC to an ESP32
To wire a DS3231 RTC to an ESP32, connect VCC to 3V3, GND to GND, SCL to GPIO 22 (D22) and SDA to GPIO 21 (D21). The module carries its own I2C pull-ups, so the bus needs no extra resistors, and the chip answers at the fixed address 0x68. The sketch below uses the RTClib (RTClib.h) library. It sets the clock if it lost power and prints the date, the time and the chip temperature every second.
- 3.3 V logic
- Library: RTClib.h
- 4 wires
DS3231 RTC to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| DS3231 RTC pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| VCC | 3V3 | red | Most DS3231 modules run from 3.3 to 5.5 V. |
| GND | GND | blue | Common ground. |
| SCL | GPIO 22 (D22) | cyan | I2C clock. The module has its own pull-up resistors. |
| SDA | GPIO 21 (D21) | violet | I2C data. |
Power and logic levels for DS3231 RTC and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The DS3231 RTC takes 3.3 V from 3V3. That pin is the ESP32’s own regulated 3.3 V output.
This pair
On an ESP32 DevKit, Wire.begin() with no arguments puts SDA on GPIO 21 and SCL on GPIO 22, labeled D21 and D22. The module carries its own I2C pull-ups, and the chip answers at the fixed address 0x68. The 32K and SQW pins are outputs this page leaves unwired.
Pins to leave alone
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
The working code for the ESP32
Library: RTClib (RTClib.h). This is the sketch the circuit above runs.
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc; // on I2C: SDA GPIO 21 (D21), SCL GPIO 22 (D22), address 0x68
void setup() {
Serial.begin(115200);
if (!rtc.begin()) {
Serial.println("No DS3231 found");
for (;;) {}
}
if (rtc.lostPower()) {
Serial.println("Clock lost power, setting it");
rtc.adjust(DateTime(2026, 11, 7, 9, 30, 0));
}
}
void loop() {
DateTime now = rtc.now();
char text[24];
snprintf(text, sizeof(text), "%04d-%02d-%02d %02d:%02d:%02d",
now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
Serial.print(text);
Serial.print(" Chip: ");
Serial.print(rtc.getTemperature(), 2);
Serial.println(" C");
delay(1000);
}
What you should see
Open the serial monitor at 115200 baud. If the clock lost power the sketch says so and sets it to a fixed time; after that it prints the date and time and the temperature of the chip once a second.
The 32K and SQW pins are outputs this guide leaves unwired.
The serial monitor prints this, recorded from the circuit above running for a few seconds:
Clock lost power, setting it
2026-11-07 09:30:00 Chip: 25.00 C
2026-11-07 09:30:01 Chip: 25.00 C
2026-11-07 09:30:02 Chip: 25.00 CCommon mistakes wiring a DS3231 RTC to an ESP32
Setting the time on every boot
A sketch that calls rtc.adjust() unconditionally resets the clock each time the board restarts. Guard it with lostPower(), as above, so a real module with a battery keeps its time.
Another chip at 0x68
The MPU6050 also answers at 0x68. Two such parts on one bus clash; the DS3231 has no address pin to move it.
Using compile time on an old build
DateTime(F(__DATE__), F(__TIME__)) sets the time the sketch was compiled, not now. If you upload a day later, the clock is a day behind.
Build your own DS3231 RTC project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.