Arduino Uno DS3231 RTC Simulator: Wiring, Code and a Live Circuit
This is a DS3231 real-time clock wired to an Arduino Uno over I2C, read live with Adafruit's RTClib: the date and time every second, and the chip's own temperature.
- 5 V logic
- Library: RTClib.h
- I2C address 0x68
- SDA A4, SCL A5
Wiring the DS3231 RTC to the Arduino Uno
Four wires: 5V, GND, SDA to pin A4 and SCL to pin A5. The sketch sets the clock to a fixed time, so the first line is always the same.
Every connection in the circuit above, with the board’s own pin names.
| DS3231 RTC pin | Arduino Uno pin | Why |
|---|---|---|
| VCC | 5V | Most DS3231 modules run from 3.3 to 5.5 V. |
| GND | GND | Common ground. |
| SCL | pin A5 | I2C clock. The module has its own pull-up resistors. |
| SDA | pin A4 | I2C data. |
Power
On an Uno the I2C bus is A4 (SDA) and A5 (SCL). The SDA and SCL pins beside AREF are the same two wires, so either pair works. 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 on the Arduino Uno
Pins 0 and 1 are the USB serial port on a real Uno, so anything wired there fights the upload and the serial monitor. Pin 13 carries the onboard LED.
On a real bench
On a real bench the Uno stands beside the breadboard: the DS3231 RTC goes into the breadboard and male-to-male jumpers run from the Uno’s female headers to its rows. Power the Uno from USB while you build, and unplug it before you move a wire.
The code, and what it does.
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc; // on I2C: SDA pin A4, SCL pin A5, address 0x68
void setup() {
Serial.begin(9600);
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);
}
rtc.begin() looks for the chip at 0x68. lostPower() reads the oscillator-stop flag, which a DS3231 sets whenever it has lost its time. On a real module with a coin cell it is clear after the first set, so a sketch sets the clock once. The simulated chip has no coin cell and starts every run with the flag set, so this sketch sets it each run.
rtc.now() reads all seven time registers in one transfer and converts them from BCD, so the sketch never sees a BCD byte. getTemperature() is the thermometer inside the chip, which it uses to correct its crystal; it measures the chip, not the room.
Serial.begin(9600) opens the serial monitor. Serial goes over the USB cable, and the serial monitor in the editor reads it at whatever baud rate the sketch sets.
Common mistakes with the DS3231 RTC on the Arduino Uno
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.
Questions and answers
Can I simulate a DS3231 with an Arduino Uno?
Yes. The circuit at the top of this page is an Arduino Uno with a DS3231 real-time clock, running in your browser on a simulated ATmega328P. The simulated chip counts real seconds and answers the library over I2C, registers and all. It has no coin cell, so it forgets the time between runs. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Where does the DS3231 connect on the Arduino Uno?
On an Uno the I2C bus is A4 (SDA) and A5 (SCL). The SDA and SCL pins beside AREF are the same two wires, so either pair works.
How accurate is a DS3231?
It corrects its crystal for temperature and keeps time to within about a minute a year, where a DS1307 can drift that much in a month.
Does it keep time when the board is off?
A real module does, from a coin cell on the back. The simulated one has no battery, so it starts each run at the time the sketch sets.
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.