RTClib: keep the date and time with a DS3231 or DS1307
RTClib is Adafruit’s library for real-time clock modules, the coin-cell boards that keep counting while the Arduino is off. It covers the DS3231 (accurate to a couple of minutes a year thanks to its temperature-compensated crystal), the cheaper DS1307 that drifts more, and the PCF8523 and PCF8563. All of them sit on I2C at 0x68 or nearby.
Built in on Mokxi: #include <RTClib.h> and it builds, with nothing to install. Free, and no account needed.
What RTClib does
Everything comes back as a DateTime: rtc.now() returns one, and it has year(), month(), day(), hour(), minute(), second() and dayOfTheWeek(), plus unixtime() for arithmetic. Add or subtract a TimeSpan to work out "in 90 minutes" or "how long since". The DS3231 also reports its own temperature and has two alarms.
Setting the clock is the step people get wrong. The usual line is rtc.adjust(DateTime(F(__DATE__), F(__TIME__))), which sets it to the moment the sketch was compiled. Run that only when the clock has lost its time (lostPower() on a DS3231, !isrunning() on a DS1307), or every reset will set the clock back to the compile time.
| Library Manager name | RTClib |
|---|---|
| Author | Adafruit |
| Include | #include <RTClib.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Real-time clock, DS3231, Real-time clock, DS1307 |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <RTClib.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "RTClib" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add RTClib to lib_deps in PlatformIO. Mokxi follows the calls of release 2.1.4, so a sketch written here builds there too.
What to know in the simulator: RTC_PCF8523 and RTC_PCF8563 are here with their datasheets’ registers, and Mokxi has no part for either, so their begin() finds nothing.
ESP32-C3 · Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · ESP32-C6 · Raspberry Pi Pico · Raspberry Pi Pico W · Seeed XIAO SAMD21 · STM32 · STM32 Nucleo-F411RE · STM32 Blue Pill · micro:bit V2 · Arduino Uno R4 · ESP32 DevKit · ESP32-S3-DevKitC-1 · ESP8266 NodeMCU · Arduino Pro Mini 3.3 V · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · Seeed XIAO ESP32-S3 · Arduino Nano ESP32 · Arduino Nano RP2040 Connect · Seeed XIAO RP2040 · Waveshare RP2040-Zero · Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
RTC_DS3231 rtc;
A DS3231 module. RTC_DS1307 rtc; for a DS1307, and RTC_Millis for a software clock with no chip.
rtc.begin()
Starts I2C and checks the chip answers. Returns false when it does not, the cue to print "Couldn’t find RTC".
rtc.lostPower()
On a DS3231, true when the clock stopped at some point (a new module, or a flat coin cell) and needs setting. The DS1307 has isrunning() instead.
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)))
Sets the clock, here to the time the sketch was compiled. DateTime(2026, 11, 1, 9, 30, 0) sets an exact time.
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}DateTime now = rtc.now();
Reads the current date and time from the chip.
DateTime now = rtc.now();
Serial.print(now.hour()); Serial.print(':');
Serial.println(now.minute());now.dayOfTheWeek()
The day of the week from 0 (Sunday) to 6, for indexing an array of day names.
now.unixtime()
Seconds since January 1, 1970, for comparing two times or logging.
DateTime later = now + TimeSpan(0, 1, 30, 0);
Date arithmetic: days, hours, minutes and seconds added to a DateTime.
rtc.getTemperature()
The DS3231’s own temperature in degrees Celsius, in quarter-degree steps.
Example: Real-time clock
On the Arduino Uno R3. It runs in the browser exactly as printed.
// Real-time clock: a DS1307 keeps the date and time, and the Uno prints it.
//
// DS1307 SDA -> A4 DS1307 SCL -> A5 VCC -> 5V GND -> GND
//
// The Uno forgets the time whenever it resets. The DS1307 does not: it counts
// seconds on its own 32.768 kHz crystal, and on a real module a coin cell
// keeps it counting while everything else is unplugged. The Uno only asks it
// for the time over I2C, the two-wire bus on A4 and A5.
//
// RTClib does the asking. If the clock was ever stopped, the sketch starts it
// again from the date and time in setup(): put today's there before you
// upload it to a real module.
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
uint8_t lastSecond = 99;
void print2(uint8_t value) {
if (value < 10) Serial.print('0');
Serial.print(value);
}
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: DS1307 real-time clock");
if (!rtc.begin()) {
Serial.println("no clock found: check SDA and SCL");
while (true) {
}
}
if (!rtc.isrunning()) {
Serial.println("clock was stopped: setting it");
rtc.adjust(DateTime(2026, 9, 23, 12, 0, 0)); // year, month, day, hour, minute, second
}
}
void loop() {
DateTime now = rtc.now();
if (now.second() == lastSecond) return;
lastSecond = now.second();
Serial.print(now.year());
Serial.print('-');
print2(now.month());
Serial.print('-');
print2(now.day());
Serial.print(' ');
print2(now.hour());
Serial.print(':');
print2(now.minute());
Serial.print(':');
print2(now.second());
Serial.println();
}Open it in the editor, wired and ready to run · Real-time clock: the project page
Common errors and how to fix them
Couldn't find RTC
begin() got no answer on I2C. Check SDA and SCL go to the board’s I2C pins (A4 and A5 on an Uno), that the module has power and ground, and run an I2C scanner to see whether 0x68 shows up.
The time resets to the same moment every time the board restarts
rtc.adjust() runs on every start-up. Wrap it in if (rtc.lostPower()) or if (!rtc.isrunning()) so it only runs when the clock really needs setting.
The date reads 2165/165/165 or similar nonsense
The library read 0xFF from every register, meaning the chip is not answering properly. Recheck the wiring and pull-ups, and make sure the object type matches the chip (RTC_DS3231 or RTC_DS1307).
The clock loses its time whenever the power is unplugged
The coin cell is flat, missing or in backward. Fit a fresh CR2032 (or the LIR2032 some modules are designed for) and set the clock once more.
Projects that use RTClib
Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.
Keep going
Try RTClib without installing anything
Open the example, change the sketch, and keep your version in a free account.