What is the DS3231?
The DS3231 is the real-time clock to use when your project needs to know the actual time: a data logger that timestamps readings, a clock or alarm, a timer that switches a relay at 7:00, or an ESP32 that must keep time without Wi-Fi. It keeps counting while your microcontroller is off, thanks to a coin cell on the back, and it is far more accurate than the older DS1307.
The difference is the crystal. The DS3231 has a temperature-compensated crystal oscillator (TCXO) built into the chip: it measures its own temperature every 64 seconds and corrects the crystal's frequency. That keeps it within ±2 ppm, about a minute per year, where a plain 32 kHz crystal drifts several times faster and changes with the room temperature. This page covers the pins, the I2C addresses, the charging circuit that catches out most buyers, and how to use the alarms.
Specifications
- Accuracy
- ±2 ppm from 0 to 40 °C (about ±1 minute per year)
- Interface
- I2C, up to 400 kHz
- I2C address
- 0x68 (DS3231), 0x57 (AT24C32 EEPROM on the module)
- Supply voltage
- 2.3–5.5 V, so it works with 3.3 V and 5 V boards
- Current
- about 200 µA active; a few µA on the backup cell
- Backup battery
- CR2032 coin cell (LIR2032 if the charging circuit is kept)
- Timekeeping
- Seconds to year, leap years corrected until 2100
- Alarms
- Two programmable alarms, output on the SQW/INT pin
- Square wave
- 1 Hz, 1.024 kHz, 4.096 kHz or 8.192 kHz on SQW, 32.768 kHz on 32K
- Temperature sensor
- ±3 °C accuracy, 0.25 °C resolution
- EEPROM (module)
- AT24C32, 4 KB, for storing settings or logs
Figures from the Maxim/Analog Devices DS3231 datasheet. The common ZS-042 module adds the EEPROM, pull-ups and the battery holder.
DS3231 module pinout
| Pin | Function | Notes |
|---|---|---|
| 32K | 32.768 kHz output | Open-drain clock output; leave unconnected unless you need it |
| SQW | Square wave or alarm | Open-drain, active LOW for alarms: use a pull-up (INPUT_PULLUP) |
| SCL | I2C clock | Uno A5, ESP32 GPIO22 |
| SDA | I2C data | Uno A4, ESP32 GPIO21 |
| VCC | Supply | 3.3 V or 5 V |
| GND | Ground | Common with the microcontroller |
Wiring & code
Setting up DS3231 with ESP32
Library
RTClib by Adafruit
Arduino IDE → Library Manager: install RTClib by Adafruit. Wi-Fi and NTP support (WiFi.h, time.h) come with the ESP32 core.
An ESP32 can get the exact time from the internet (NTP), but it loses it the moment Wi-Fi drops or the board resets offline. A DS3231 fixes that: sync it from NTP whenever Wi-Fi is available, and read it the rest of the time. The DS3231 then keeps the time to within about a minute a year, even while the ESP32 is off. Read the DS3231 overview first if your module came with a CR2032.
Wiring: DS3231 to ESP32
| DS3231 pin | ESP32 pin | Note |
|---|---|---|
| VCC | 3V3 | Keeps the module's pull-ups at 3.3 V |
| GND | GND | Common ground |
| SDA | GPIO21 | Default I2C data |
| SCL | GPIO22 | Default I2C clock |
| SQW (optional) | GPIO33 | Alarm output, for waking from deep sleep |
Tip
Power it from 3V3, not 5V. The module's I2C pull-up resistors connect to its VCC. At 5 V they would pull the ESP32's SDA and SCL pins to 5 V, above their 3.6 V limit.
The sketch: NTP once, DS3231 always
At start-up the sketch tries Wi-Fi for ten seconds. If it connects, it fetches the time from pool.ntp.org and writes it into the DS3231. With or without Wi-Fi, it then reads the DS3231 every second. The RTC holds UTC; the sketch adds your time-zone offset when printing, which keeps the clock correct through daylight-saving changes.
#include <WiFi.h>
#include <time.h>
#include <RTClib.h>
const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";
const long UTC_OFFSET_S = 5 * 3600 + 30 * 60; // e.g. India, UTC+5:30
RTC_DS3231 rtc;
bool syncFromNtp() {
WiFi.begin(WIFI_SSID, WIFI_PASS);
for (int i = 0; i < 20 && WiFi.status() != WL_CONNECTED; i++) delay(500);
if (WiFi.status() != WL_CONNECTED) return false;
configTime(0, 0, "pool.ntp.org", "time.google.com"); // keep UTC
struct tm t;
if (!getLocalTime(&t, 10000)) return false;
rtc.adjust(DateTime(t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec));
WiFi.disconnect(true);
return true;
}
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
if (!rtc.begin()) {
Serial.println("Couldn't find the DS3231 - check SDA 21 / SCL 22");
while (true) delay(1000);
}
if (syncFromNtp()) Serial.println("RTC synced from NTP");
else if (rtc.lostPower()) Serial.println("No Wi-Fi and RTC lost power: time is wrong");
else Serial.println("No Wi-Fi: using the DS3231 time");
}
void loop() {
DateTime local = rtc.now() + TimeSpan(UTC_OFFSET_S);
Serial.printf("%04d-%02d-%02d %02d:%02d:%02d %.2f C\n",
local.year(), local.month(), local.day(),
local.hour(), local.minute(), local.second(),
rtc.getTemperature());
delay(1000);
}Change UTC_OFFSET_S to your offset in seconds: UTC+1 is 3600, UTC−5 is −18000. Resyncing once a day (call syncFromNtp() from the loop when the hour is 3, say) keeps the DS3231 essentially exact.
Waking from deep sleep on the alarm
For a battery logger, wire SQW to GPIO33, set an alarm as in the Uno guide, and before sleeping call esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0). The ESP32 then sleeps until the DS3231 pulls SQW LOW. The DS3231 needs VCC for its alarm output, so keep the module powered (it draws about 200 µA). After waking, clear the alarm with rtc.clearAlarm(1) so SQW goes HIGH again.
Troubleshooting
Couldn't find the DS3231
- Likely cause
- Wrong pins or 5 V on VCC
- Fix
- SDA 21, SCL 22, VCC on 3V3; scan for 0x68
Time is hours off
- Likely cause
- Time-zone offset
- Fix
- The RTC holds UTC; set UTC_OFFSET_S for your zone
NTP never syncs
- Likely cause
- Wrong credentials or 5 GHz network
- Fix
- The ESP32 needs a 2.4 GHz network
Wakes immediately after sleep
- Likely cause
- Alarm flag still set, SQW held LOW
- Fix
- Call rtc.clearAlarm(1) before going to sleep
Where to go next
- Put the time on a 0.96" OLED on the same I2C bus.
- Learn the sleep side in the ESP32 deep sleep guide.
- More Wi-Fi time options: connect the ESP32 to Wi-Fi.







