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 About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $32. On Mokxi they're free.
What you'll learn
- What a real-time clock does
- I2C wiring with SDA and SCL
- Using the RTClib library
- Printing numbers with leading zeros
- Acting only when a value changes
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
The Uno forgets the time whenever it resets. A DS1307 clock module does not: it counts seconds on its own crystal. The Uno asks it for the time over I2C, with SDA on A4 and SCL on A5.
The RTClib library does the asking. At start-up the sketch checks the clock is there, and if the clock was stopped it sets it to a fixed date and time from the code.
Then loop() reads the time and prints a line only when the second changes, so you get one line per second, like 2026-09-23 12:00:01.
Parts list
3 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Real-time clock, DS1307
Kits that can build it
This starter kit has every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Real-time clock, DS1307 pin GND | Arduino Uno R3 pin GND |
| Real-time clock, DS1307 pin VCC | Arduino Uno R3 pin 5V |
| Real-time clock, DS1307 pin SDA | Arduino Uno R3 pin A4 |
| Real-time clock, DS1307 pin SCL | Arduino Uno R3 pin A5 |
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();
}
Code walkthrough
The key functions of the sketch, and what each one does.
print2 sketch.ino
It prints a number with at least two digits. If the value is under 10, it prints a 0 first. That is how the month, day, hour, minute and second print as 09 instead of 9.
void print2(uint8_t value) {
if (value < 10) Serial.print('0');
Serial.print(value);
}setup sketch.ino
It starts serial and calls rtc.begin(). If the clock does not answer, it prints a message and stops in an endless loop. If rtc.isrunning() says the clock was stopped, it calls rtc.adjust with a date and time from the code. On a real module, put today's date there before uploading.
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
}
}loop sketch.ino
It reads rtc.now() and returns early if the second is the same as lastSecond. When the second changes, it saves it and prints the year, month, day, hour, minute and second in a tidy line.
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();
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The monitor says no clock found This project
Check SDA goes to A4 and SCL to A5, with 5V and GND connected. Swapped data lines mean the clock never answers. - The time is wrong on a real module This project
Change the date and time inrtc.adjustto now before you upload. It only sets the clock when the clock was stopped. - The time is wrong, or stays at the same value. Real-time clock
A new chip starts halted. Clear the clock halt bit (bit 7 of the seconds register) to start it, and set the time. Most libraries do this when you call the set time function. - The clock reads garbage, or the I2C scan finds nothing. Real-time clock
The DS1307 runs from 5 V, not 3.3 V. It answers at I2C address 0x68. SDA and SCL need pull-up resistors, about 4.7 k. Many modules already have them on board. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.