Summary
DS3231 or DS1307? This guide compares both RTC modules on accuracy, voltage and features, then shows how to wire a DS3231 to Arduino, set and read time with RTClib, use alarms and the temperature sensor, and avoid the coin cell charging problem on common ZS-042 boards. Our pick: the DS3231.

The DS3231 wins for almost every project. It has a built-in temperature-compensated crystal accurate to about ±2 ppm, which is roughly a minute a year. It runs on 3.3V or 5V and adds alarms and a temperature sensor. The DS1307 drifts more and needs 5V. For most projects, we pick the DS3231.
| Feature | DS3231 | DS1307 |
|---|---|---|
| Crystal | Built-in, temperature-compensated (TCXO) | External 32.768 kHz crystal |
| Accuracy | ±2 ppm (0 to 40°C), about 1 minute/year | Depends on the crystal and temperature, often minutes/month |
| Supply voltage | 2.3V to 5.5V (3.3V and 5V boards) | 4.5V to 5.5V (5V only) |
| I2C speed | Up to 400 kHz | Up to 100 kHz |
| I2C address | 0x68 | 0x68 |
| Alarms | 2 programmable alarms + interrupt pin | None |
| Temperature sensor | Yes, ±3°C | No |
| Battery-backed RAM | None (module adds AT24C32 EEPROM) | 56 bytes + module EEPROM |
| Best for | Data loggers, alarms, ESP32, outdoor builds | Budget 5V clocks where small drift is fine |
Pick your module
- DS3231 RTC Module (Real Time Clock, I2C): Our pick for most builds. Includes AT24C32 EEPROM. Coin cell not included. Buy now
- SmartElex DS3231 RTC Module with EEPROM (AT24C32): Same accurate DS3231 chip, ships with a CR2032 coin cell. View product
- DS1307 Real Time Clock Module: The budget option for simple 5V clocks. Includes 56 bytes of RAM and 4 KB EEPROM. Buy now.
What an RTC Module Does and Why Arduino Needs One
A real time clock module is a tiny board that counts seconds, minutes, hours, days, months, and years on its own. It has its own coin cell, so it keeps counting even when your Arduino is switched off.
An Arduino can count time with millis(), but it has two weaknesses:
- It forgets. Every reset or power cut starts the count from zero again.
- It drifts. The board's resonator is not built for precise timekeeping, so the count wanders over days.
The RTC module solves both. Your Arduino simply asks, "What time is it?" over I2C, and gets the answer back in a few microseconds.
How it keeps time: The RTC chip counts pulses from a 32.768 kHz quartz crystal. That number is 2¹⁵, so the chip divides it down to exactly one tick per second. The difference between our two chips is how steady that crystal stays. The DS1307 uses a separate crystal on the board, and its frequency shifts as the room heats up and cools down. The DS3231 has the crystal sealed inside the chip and measures its own temperature every 64 seconds to correct for that shift.
Where you will use one:
- Data loggers that timestamp sensor readings
- Digital clocks and alarm clocks
- Timers that switch pumps, lights, or relays at set times
- Attendance and access-control systems
- Battery projects that sleep and wake up on a schedule

Components and Supplies
DS3231 Pinout and Wiring (I2C 0x68)
The DS3231 module talks over I2C, so you need just four wires to get started. The common ZS-042 board has a 6-pin header on one side and a 4-pin pass-through header on the other, for chaining more I2C devices.

| Pin | Function | Connect it? |
|---|---|---|
| VCC | Power, 3.3V or 5V | Yes |
| GND | Ground | Yes |
| SDA | I2C data | Yes |
| SCL | I2C clock | Yes |
| SQW | Square wave or alarm interrupt output (active LOW) | Only for alarms |
| 32K | 32.768 kHz clock output | Usually no |
I2C addresses: The DS3231 sits at 0x68. The AT24C32 EEPROM on the same board sits at 0x57 by default. The DS1307 also uses 0x68, so you cannot run both on the same bus.
Wiring to Arduino Uno
- VCC → 5V
- GND → GND
- SDA → A4
- SCL → A5
- SQW → D2 (optional, for alarms)

The dashed SQW wire is only needed for alarms. D2 is used because it is one of the Uno's two interrupt pins (D2 and D3).
On an Uno R3, the SDA and SCL pins next to AREF are the same as A4 and A5, so either pair works. Check our Arduino Uno pin diagram guide if you are unsure where they are.
| Board | SDA | SCL | VCC |
|---|---|---|---|
| Arduino Uno / Nano | A4 | A5 | 5V |
| Arduino Mega 2560 | 20 | 21 | 5V |
| Arduino Leonardo | 2 | 3 | 5V |
| ESP32 | GPIO 21 | GPIO 22 | 3.3V |
The module already has pull-up resistors on SDA and SCL, so you do not need to add your own. The DS1307 module wires up exactly the same way, but it must get 5V.
Most communication problems come from swapped SDA and SCL wires, and not from faulty modules. If the code cannot find the RTC, check those two first.
Code: Set and Read Time With RTClib
The easiest way to use an RTC module with Arduino is the RTClib library by Adafruit. It supports the DS3231, DS1307, and PCF8523 with almost identical code.
Install it:
- Open the Arduino IDE and go to Sketch → Include Library → Manage Libraries.
- Search for RTClib and install the one by Adafruit.
- Click Install All when it asks about dependencies.
Now upload this sketch:
// DS3231 RTC: set and read time with RTClib
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
const char daysOfWeek[7][12] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find DS3231. Check SDA/SCL wiring!");
while (1) delay(10);
}
// Set the time only when the RTC has lost power (new module or dead cell)
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting time to compile time...");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Or set an exact time: rtc.adjust(DateTime(2026, 11, 6, 10, 30, 0));
}
}
void loop() {
DateTime now = rtc.now();
char buf[] = "YYYY-MM-DD hh:mm:ss";
Serial.print(now.toString(buf));
Serial.print(" ");
Serial.println(daysOfWeek[now.dayOfTheWeek()]);
delay(1000);
}
Open the Serial Monitor at 9600 baud. On a brand-new module, you should see this:
RTC lost power, setting time to compile time...
2026-11-06 10:19:28 Friday
2026-11-06 10:19:29 Friday
2026-11-06 10:19:30 Friday
2026-11-06 10:19:31 Friday
2026-11-06 10:19:32 Friday

How the code works
-
rtc.begin()checks that the module answers at address 0x68. If it does not, you get the wiring error. -
rtc.lostPower()returns true on a new module or after the coin cell dies. Only then do we set the clock, so a reset never overwrites a good time. -
DateTime(F(__DATE__), F(__TIME__))is the moment your sketch was compiled. Uploading takes a few seconds, so the clock may start a few seconds behind. For exact time, use the commented line with your own values. -
rtc.now()returns aDateTimeobject. Read parts with.year(),.month(),.day(),.hour(),.minute()and.second(), or get a Unix timestamp with.unixtime()for data logs.
Using a DS1307 instead? Change RTC_DS3231 rtc; to RTC_DS1307 rtc; and replace rtc.lostPower() with !rtc.isrunning(). Everything else stays the same.
Using Alarms and the Temperature Sensor
This is where the DS3231 pulls ahead. It has two alarms that pull the SQW pin LOW at a set time, which can trigger an interrupt on your Arduino. That means no constant time checking in loop(), and your board can even sleep until the alarm wakes it.
The sketch below logs the temperature once a minute. Wire SQW to D2 first.
// DS3231 RTC: once-a-minute alarm interrupt + temperature logging
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
const byte ALARM_PIN = 2; // D2 = interrupt pin on Uno/Nano
volatile bool alarmFired = false;
void onAlarm() {
alarmFired = true; // keep the ISR short: just set a flag
}
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find DS3231. Check SDA/SCL wiring!");
while (1) delay(10);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
rtc.disable32K(); // 32K pin not needed
rtc.writeSqwPinMode(DS3231_OFF); // SQW pin = alarm interrupt output
rtc.clearAlarm(1);
rtc.clearAlarm(2);
rtc.disableAlarm(2);
// Alarm 1 fires whenever seconds == 00, i.e. once a minute
// (only the seconds field matters in this mode)
rtc.setAlarm1(DateTime(2000, 1, 1, 0, 0, 0), DS3231_A1_Second);
pinMode(ALARM_PIN, INPUT_PULLUP); // SQW is open-drain, goes LOW on alarm
attachInterrupt(digitalPinToInterrupt(ALARM_PIN), onAlarm, FALLING);
Serial.println("Logging temperature once a minute...");
}
void loop() {
if (alarmFired) {
alarmFired = false;
rtc.clearAlarm(1); // must clear, or SQW stays LOW
DateTime now = rtc.now();
char buf[] = "YYYY-MM-DD hh:mm:ss";
Serial.print(now.toString(buf));
Serial.print(" Temp: ");
Serial.print(rtc.getTemperature());
Serial.println(" C");
}
}
Serial Monitor output:
Logging temperature once a minute...
2026-11-06 10:43:00 Temp: 27.25 C
2026-11-06 10:44:00 Temp: 27.25 C
2026-11-06 10:45:00 Temp: 27.50 C
Alarm modes you can use
| Mode | Fires | Example |
|---|---|---|
DS3231_A1_PerSecond |
Every second | Blink or heartbeat |
DS3231_A1_Second |
When seconds match (once a minute) | Logging every minute |
DS3231_A1_Minute |
When minutes + seconds match (once an hour) | Hourly readings |
DS3231_A1_Hour |
When hours + minutes + seconds match (once a day) | 6:00 AM plant watering |
For a daily 6:00 AM alarm, use rtc.setAlarm1(DateTime(2000, 1, 1, 6, 0, 0), DS3231_A1_Hour);.
Two rules to remember:
- Always call
rtc.clearAlarm(1)after an alarm. If you skip it, SQW stays LOW and the alarm never fires again. - Use an interrupt pin. On the Uno and Nano, only D2 and D3 support
attachInterrupt().
About the temperature sensor: rtc.getTemperature() reads the chip's own sensor, which the DS3231 uses to correct its crystal. It updates every 64 seconds in 0.25°C steps and is accurate to about ±3°C. It is handy for a rough room reading, but use a DHT22 or DS18B20 if you need a precise one.
Coin Cell Battery: Which One and the Charging Circuit Warning
The coin cell is what keeps your RTC ticking when the Arduino is off. Pick the wrong one, and you risk a swollen or leaking battery.
| CR2032 | LIR2032 | |
|---|---|---|
| Type | Non-rechargeable lithium | Rechargeable lithium-ion |
| Voltage | 3.0V | 3.6V to 3.7V nominal (4.2V when full) |
| Capacity | About 220 mAh | About 40 mAh |
| Safe to charge? | No | Yes, up to 4.2V |
| Easy to buy? | Very | Less common |
The problem with common DS3231 modules
Most DS3231 boards, including the popular ZS-042, include a simple charging circuit. It is just a 200 Ω resistor (often marked "201") and a diode, feeding VCC into the battery. It was designed for an LIR2032.
Here is the catch:
- With a CR2032 fitted: this circuit tries to push current into a battery that must never be charged. Over time, that can make it swell or leak.
- Even with an LIR2032 at 5V: the circuit has no charge control, so the cell can be pushed past its safe 4.2V limit.
- At 3.3V: the voltage after the diode is too low to properly charge an LIR2032 anyway.

The simple fix
- Find the 200 Ω resistor (marked "201") or the diode next to it near the 6-pin header.
- Remove it with a soldering iron, or carefully cut one of its legs.
- Fit a regular CR2032. The DS3231 draws so little from the battery that a fresh CR2032 should last several years.
This turns off charging completely and is the setup we recommend for most projects.
Some DS1307 "Tiny RTC" boards also have a charging circuit meant for an LIR2032. Check your board's listing or schematic before fitting a CR2032.
Build Next
Your RTC is running. Put it to work in a real project:
- I Built a Digital Clock Using Arduino: Pair your RTC with a 16x2 I2C LCD for a desk clock that never needs resetting.
- I Built a Smart Alarm Clock Using Arduino: Put the DS3231 alarms you just learned to use.
- Internet Clock Using ESP32 and OLED Display: Sync time from the internet, then add a DS3231 as an offline backup.
Final Thoughts
When it comes to DS3231 vs DS1307, the answer is simple for most makers. The DS3231 costs a little more but keeps time to about a minute a year, works with both 3.3V and 5V boards, and gives you alarms for free. The DS1307 still works for basic 5V clocks on a tight budget.
Whichever you choose, remember the two lessons from this guide: set the time only when lostPower() says so, and check that charging circuit before you fit a CR2032.
Grab a DS3231 RTC module and start building. Got a question or a project to share? Drop it in the comments below.
Excerpt
Frequently Asked Questions
1. Which is better, DS3231 or DS1307?
The DS3231 is better for almost every project. It is far more accurate, works on 3.3V and 5V, and adds alarms and a temperature sensor. The DS1307 only makes sense for a very low-cost 5V build where a little drift is fine.
2. How accurate is the DS3231?
The DS3231 is accurate to ±2 ppm between 0°C and 40°C, and ±3.5 ppm from −40°C to 85°C. In practice, that is roughly a minute of drift per year.
3. What is the I2C address of DS3231?
The DS3231 uses I2C address 0x68. The AT24C32 EEPROM on most DS3231 modules uses 0x57. You can confirm both with an I2C scanner sketch.
4. Does the RTC keep time without power?
Yes. When main power is removed, the module switches to its coin cell and keeps counting. It will have the correct time when your Arduino powers up again.
5. Can we use a DS3231 with ESP32?
Yes. The DS3231 runs at 3.3V, so connect VCC to 3.3V, SDA to GPIO 21 and SCL to GPIO 22. The same RTClib code works. For other pins, call Wire.begin(SDA_PIN, SCL_PIN); before rtc.begin().
6. Why is my RTC showing the wrong time?
The usual causes are:
- A few seconds behind: the time was set at compile time, before uploading finished.
- Resets on every upload:
rtc.adjust() runs every time. Keep it inside the lostPower() check.
- Off by hours: the RTC stores whatever time you gave it. It does not handle time zones or daylight saving.
- Forgets after unplugging: the coin cell is missing, dead or fitted the wrong way.
- Shows 2165-165-165: the I2C read failed. Check wiring and power.
7. How long does the coin cell last in a DS3231 module?
A fresh CR2032 should last several years, because the DS3231 draws only a few microamps from it. Remove the charging resistor first if you use a CR2032 (see the battery section above).
8. Will my DS1307 code work on a DS3231?
Mostly yes. Both use address 0x68 and similar time registers. With RTClib, change RTC_DS1307 to RTC_DS3231 and swap isrunning() for lostPower().
Wire.begin(SDA_PIN, SCL_PIN); before rtc.begin().rtc.adjust() runs every time. Keep it inside the lostPower() check.RTC_DS1307 to RTC_DS3231 and swap isrunning() for lostPower().





