Skip to content

RTC · Arduino Uno guide

Interfacing DS3231 Real-Time Clock (RTC) Module with Arduino Uno

A temperature-compensated I2C clock that keeps time to about a minute a year, with two alarms, a coin-cell backup and a built-in thermometer.

Updated November 25, 2026 Arduino C++RTClibAlso forESP32

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

PinFunctionNotes
32K32.768 kHz outputOpen-drain clock output; leave unconnected unless you need it
SQWSquare wave or alarmOpen-drain, active LOW for alarms: use a pull-up (INPUT_PULLUP)
SCLI2C clockUno A5, ESP32 GPIO22
SDAI2C dataUno A4, ESP32 GPIO21
VCCSupply3.3 V or 5 V
GNDGroundCommon with the microcontroller

Wiring & code

Setting up DS3231 with Arduino Uno

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 pinUno pinNote
VCC5V3.3 V also works
GNDGNDCommon ground
SDAA4I2C data (also the SDA pin near AREF on R3 boards)
SCLA5I2C clock
SQWD2Only for the alarm example; uses INPUT_PULLUP
  1. 1Fit the CR2032 in the holder, flat (+) side up.
  2. 2Connect VCC, GND, SDA and SCL as in the table.
  3. 3Install RTClib by Adafruit from the Library Manager.
  4. 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.

sketch.ino
#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.

sketch.ino
#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.

Keep learning

Related guides & projects