Arduino Uno Real Time Clock
This project utilizes an Arduino Uno and the DS3231 Real Time Clock (RTC) module to accurately keep track of date and time.
The DS3231 module communicates with the Arduino via I2C protocol to provide precise timekeeping capabilities.
How It Works
A microcontroller has no idea what time it is. millis() counts milliseconds since power-up and resets every time the board restarts, so any project that must log when something happened needs a real-time clock.
An RTC module keeps time independently using a 32.768 kHz crystal — chosen because dividing it by 2 fifteen times yields exactly one pulse per second — and a backup coin cell that keeps the clock running for years while the main board is unpowered.
The two common parts differ far more than their price suggests. The DS1307 uses an external crystal whose frequency varies with temperature, drifting by minutes per month. The DS3231 integrates the crystal with a temperature-compensated oscillator and holds about ±2 minutes per year — a hundredfold improvement for a small extra cost.
Both communicate over I²C at address 0x68, so swapping one for the other needs almost no code change. The DS3231 additionally exposes its temperature reading and provides two programmable alarms.
Components Needed
- Arduino Uno
- DS3231 RTC Module
- Jumper Wires
- Arduino Uno
Wiring to the Arduino Uno
Connect SDA and SCL to A4 (SDA) and A5 (SCL), with VCC on the 5 V rail and GND to ground. I²C pin assignment is fixed by hardware, so wiring copied from a different Arduino will not necessarily match this board.
I²C requires pull-up resistors on both lines. Breakout boards almost always include them, but stacking several modules puts those pull-ups in parallel and can overload the bus — remove the extras if the bus becomes unreliable.
Run an I²C scanner sketch first. Confirming the device answers at the expected address takes a minute and eliminates the most common cause of a module that "does not work".
Fit the backup battery before setting the time, and check it is a CR2032 non-rechargeable cell in a DS3231 module unless the charging circuit has been disabled. Many cheap modules attempt to charge a non-rechargeable cell, which is why they sometimes leak or swell.
| Module pin | Arduino Uno pin | Function |
|---|---|---|
| SDA | A4 | I²C data |
| SCL | A5 | I²C clock |
| VCC | 5V | Supply |
| GND | GND | Common ground |
| SQW / INT | any digital pin | Optional alarm or 1 Hz output |
Example Code
Reading and conditionally setting a DS3231, with timestamped output. Upload it with the board set to Arduino Uno and open the Serial Monitor at 9600 baud.
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!rtc.begin()) {
Serial.println("RTC not found — check wiring and the 0x68 address");
while (1) delay(1000);
}
// Only set the time if the clock actually lost power — otherwise
// every reset would overwrite a correct time with the compile time
if (rtc.lostPower()) {
Serial.println("RTC lost power — setting to compile time");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
char stamp[20];
sprintf(stamp, "%04d-%02d-%02d %02d:%02d:%02d",
now.year(), now.month(), now.day(),
now.hour(), now.minute(), now.second());
Serial.print(stamp);
Serial.print(" die temp: ");
Serial.print(rtc.getTemperature(), 2);
Serial.println(" C");
delay(1000);
}
Applications
A ds3231 real time clock (rtc) turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Data loggers that must timestamp every reading
- Scheduling — turning equipment on and off at set times
- Alarm clocks and reminder devices
- Attendance and access logging
- Any project whose events must be correlated after the fact
Working with the Arduino Uno
The Arduino Uno is built around the ATmega328P and runs on 5 V logic with 2 KB of SRAM and 32 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The Uno runs at 5 V, so most hobby sensor modules connect directly with no level shifting.
With only 2 KB of SRAM, avoid large buffers and prefer the F() macro for constant strings.
The single hardware UART is shared with the USB connection, so heavy Serial printing competes with uploads.
| Arduino Uno characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 5 V | Matches most hobby modules directly |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | A0–A5 (six channels) | Determines how many analog sensors can share the board |
| PWM outputs | D3, D5, D6, D9, D10 and D11 | Needed for brightness, speed and tone control |
| I²C pins | A4 (SDA) and A5 (SCL) | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | D2 and D3 only | Required for counting fast or asynchronous events |
| Serial | a single hardware UART shared with USB | Monitor runs at 9600 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- The time resets on every power cycle — the backup cell is dead, missing, or inserted the wrong way round.
- The time is always the compile time —
rtc.adjust()is being called unconditionally. Guard it withlostPower(). - A DS1307 drifts noticeably within weeks — that is inherent to its uncompensated crystal. Use a DS3231.
- The module is not found — run an I²C scanner; some boards use a different address or have a faulty pull-up.
- Time jumps by hours — a timezone or daylight-saving offset is being applied twice.
- Code written for an ESP board gives odd analog values — the Arduino Uno uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
- An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Uno I²C is on A4 (SDA) and A5 (SCL).
Taking It Further on the Arduino Uno
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Uno specifically:
The Uno’s shield ecosystem is its real advantage. Once the circuit works on a breadboard, a prototyping shield turns it into something permanent that still stacks with a data-logging or Ethernet shield without rewiring.
Logging to an SD card via a shield is the natural next step, since the Uno has no onboard storage and no network. Timestamp each reading with a DS3231 real-time clock so the log survives power cuts with correct times.
Because SRAM is limited to 2 KB, keep logged strings short and write them out immediately rather than buffering. Building a long String in memory is the most common cause of an Uno sketch that runs for hours and then freezes.
Notes and Practical Limits
The lostPower() guard is the detail that distinguishes a working logger from a frustrating one. Setting the time on every boot silently destroys the clock's accuracy and is extremely common in beginner sketches.
An RTC stores local wall time with no timezone information. For anything that may cross timezones or daylight-saving boundaries, store UTC and convert at display time.