Real-time clock on an Arduino Uno
A DS1307 clock module on A4 and A5 keeps the date and time, and RTClib reads it back once a second. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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();
}
Parts list
3 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; Real-time clock, DS1307 pin GND
- Arduino Uno R3 pin 5V; Real-time clock, DS1307 pin VCC
- Arduino Uno R3 pin A4; Real-time clock, DS1307 pin SDA
- Arduino Uno R3 pin A5; Real-time clock, DS1307 pin SCL
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.