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.
DS3231 at a glance
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.
Pins and parts of the module
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 |
Warning
The CR2032 charging trap. Most ZS-042 boards include a 200 Ω resistor and a diode that charge the coin cell from VCC. That suits a rechargeable LIR2032, but the modules usually ship with a standard CR2032, which must not be charged: it can swell or leak, especially on a 5 V supply. If you use a CR2032, desolder the 200 Ω resistor (or the diode) next to the battery holder. The clock works exactly the same without it.
Why the DS3231 instead of a DS1307 or millis()?
An Arduino can count time with millis(), but its ceramic resonator is only accurate to about 0.5 %, and the count restarts at every reset. A DS1307 keeps time through power cuts, but its external crystal drifts with temperature. The DS3231 solves both:
RTC options compared
DS3231
- Typical drift
- ±2 ppm, about 1 minute a year
- Best for
- Clocks, timers, loggers, anything that must stay right
DS1307
- Typical drift
- ±20 ppm and temperature dependent, minutes a month
- Best for
- Cheap clocks that are corrected often
PCF8563
- Typical drift
- Crystal dependent, similar to the DS1307
- Best for
- Low-power 3.3 V designs
ESP32 + NTP
- Typical drift
- Exact while online
- Best for
- Connected projects; pair with a DS3231 for offline time
millis()
- Typical drift
- About 0.5 %, resets with the board
- Best for
- Short intervals, not wall-clock time
Alarms and the SQW pin
The DS3231 has two alarms. Alarm 1 can match seconds, minutes, hours and the day or date; Alarm 2 works the same but without seconds. When an alarm matches, the chip pulls SQW LOW and holds it there until your code clears the alarm flag. Because SQW is open-drain, enable a pull-up on the input pin.
That makes the DS3231 a good wake-up source: an Arduino can sleep and wake on an interrupt, and an ESP32 can wake from deep sleep on a GPIO, so a battery project runs for a few seconds per hour and sleeps the rest. Alarms only fire while VCC is present: on battery alone, the chip keeps time but does not drive SQW.
Troubleshooting
Time resets to 2000 after power off
- Likely cause
- Flat or missing backup cell
- Fix
- Fit a fresh CR2032; check the cell voltage (about 3 V)
Time set wrong after every upload
- Likely cause
- Sketch calls rtc.adjust() on every start
- Fix
- Only adjust when rtc.lostPower() is true
"Couldn't find RTC"
- Likely cause
- SDA and SCL swapped, or wrong pins
- Fix
- Uno: SDA A4, SCL A5. ESP32: SDA 21, SCL 22. Run an I2C scanner
Two addresses in an I2C scan
- Likely cause
- Normal: 0x68 is the clock, 0x57 is the EEPROM
- Fix
- Use 0x68 for the RTC
Alarm never fires
- Likely cause
- SQW needs a pull-up, or alarm flag not cleared
- Fix
- Use INPUT_PULLUP and call clearAlarm() after each alarm
Temperature reads a few degrees high
- Likely cause
- Self-heating and ±3 °C sensor accuracy
- Fix
- Use a BME280 or DS18B20 for real measurements
Frequently asked questions
How long does the coin cell last? A CR2032 keeps a DS3231 running for several years, because the chip draws only microamps on battery.
Does it handle daylight saving time? No. It counts plain time with leap years. Keep it on UTC or standard time and apply the daylight-saving offset in your code.
Can I use it at 3.3 V? Yes. The chip runs from 2.3 to 5.5 V, and the module's pull-ups go to VCC, so at 3.3 V the I2C lines are safe for an ESP32.
Can several I2C devices share the bus? Yes. An OLED (0x3C), a BME280 (0x76/0x77) and the DS3231 (0x68) can sit on the same SDA and SCL. See ESP32 I2C pins.
Tip
Board guides: DS3231 with Arduino Uno sets the clock, prints the time and temperature, and fires an alarm on an interrupt. DS3231 with ESP32 syncs the RTC from internet time and keeps it accurate offline.
Wiring & code
Board integration
Library
RTClib by Adafruit
Arduino IDE → Library Manager: search "RTClib" and install RTClib by Adafruit (it installs Adafruit BusIO too).
This guide connects a DS3231 RTC module to an Arduino Uno, sets the clock from your computer, prints the date, time and temperature every second, and then uses the DS3231's alarm to trigger an interrupt. Read the DS3231 overview first if you use a CR2032 cell: the module's charging resistor should come off.
Wiring: DS3231 to Arduino Uno
| DS3231 pin | Uno pin | Note |
|---|---|---|
| VCC | 5V | 3.3 V also works |
| GND | GND | Common ground |
| SDA | A4 | I2C data (also the SDA pin near AREF on R3 boards) |
| SCL | A5 | I2C clock |
| SQW | D2 | Only for the alarm example; uses INPUT_PULLUP |
- 1Fit the CR2032 in the holder, flat (+) side up.
- 2Connect VCC, GND, SDA and SCL as in the table.
- 3Install RTClib by Adafruit from the Library Manager.
- 4Upload the first sketch and open the Serial Monitor at 9600 baud.
Sketch 1: set the clock and print the time
The first time it runs (or whenever the backup cell has been removed), rtc.lostPower() is true and the sketch sets the clock to the moment the sketch was compiled. After that it leaves the time alone, so re-uploading or resetting the Uno does not change it.
#include <RTClib.h>
RTC_DS3231 rtc;
const char* DAYS[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find the DS3231 - check SDA (A4) and SCL (A5)");
while (true) {}
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting it to the compile time");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
rtc.disable32K(); // the 32K output isn't used here
}
void loop() {
DateTime now = rtc.now();
char buf[32];
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
DAYS[now.dayOfTheWeek()], now.year(), now.month(), now.day(),
now.hour(), now.minute(), now.second());
Serial.print(buf);
Serial.print(" ");
Serial.print(rtc.getTemperature(), 2);
Serial.println(" C");
delay(1000);
}The compile time is a few seconds behind the real time by the time the upload finishes. For an exact setting, use rtc.adjust(DateTime(2026, 11, 26, 14, 30, 0)) once with the correct values, then remove that line and upload again.
Sketch 2: an alarm on an interrupt
Connect SQW to D2. The sketch sets Alarm 1 to fire whenever the seconds match: ten seconds from now, then once a minute at that second. The DS3231 pulls SQW LOW, the interrupt sets a flag, and the loop prints the time and clears the alarm so it can fire again. Replace the print with whatever should happen on schedule, such as switching a relay or taking a reading.
#include <RTClib.h>
RTC_DS3231 rtc;
const byte ALARM_PIN = 2; // SQW -> D2
volatile bool alarmFlag = false;
void onAlarm() { alarmFlag = true; }
void setup() {
Serial.begin(9600);
if (!rtc.begin()) { Serial.println("No RTC"); while (true) {} }
if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
rtc.disable32K();
rtc.clearAlarm(1);
rtc.clearAlarm(2);
rtc.writeSqwPinMode(DS3231_OFF); // SQW acts as the alarm output
rtc.disableAlarm(2);
pinMode(ALARM_PIN, INPUT_PULLUP); // SQW is open-drain
attachInterrupt(digitalPinToInterrupt(ALARM_PIN), onAlarm, FALLING);
// Fire when the seconds match: 10 s from now, then once a minute
rtc.setAlarm1(rtc.now() + TimeSpan(10), DS3231_A1_Second);
Serial.println("Alarm set - waiting...");
}
void loop() {
if (alarmFlag) {
alarmFlag = false;
DateTime t = rtc.now();
Serial.print("Alarm at ");
Serial.println(t.timestamp(DateTime::TIMESTAMP_TIME));
rtc.clearAlarm(1); // releases SQW so it can fire again
}
}To trigger at a fixed time instead, use DS3231_A1_Hour with a DateTime holding the hour, minute and second you want: rtc.setAlarm1(DateTime(2026, 1, 1, 7, 0, 0), DS3231_A1_Hour) fires every day at 07:00:00.
Troubleshooting
Couldn't find the DS3231
- Likely cause
- SDA/SCL swapped or loose
- Fix
- SDA to A4, SCL to A5; run an I2C scanner, expect 0x68
Time jumps back after a reset
- Likely cause
- rtc.adjust() runs every time
- Fix
- Keep it inside if (rtc.lostPower())
Shows 2000-01-01 after unplugging
- Likely cause
- Backup cell flat or missing
- Fix
- Fit a fresh CR2032
Alarm fires once only
- Likely cause
- Alarm flag not cleared
- Fix
- Call rtc.clearAlarm(1) after each alarm
Alarm never fires
- Likely cause
- SQW not on D2, or no pull-up
- Fix
- Check the wire; keep INPUT_PULLUP
Where to go next
- Show the time on an LCD 1602 I2C or a TM1637 display: both share the I2C or use two spare pins.
- Log sensor readings with timestamps: the module's AT24C32 EEPROM (address 0x57) holds a few hundred readings.
- Switch a relay module at set times with Sketch 2.