Arduino DS1307 Real-Time Clock: Wiring, Code and Simulator
To build this Arduino DS1307 Real-Time Clock project you need an Arduino Uno and a DS1307 RTC module. Set the time on a DS1307 over I2C, then read it back every second: BCD registers by hand. Make the 4 connections in the wiring table, upload the Arduino C++ code, and the clock follows simulated time, so it keeps counting at whatever speed the simulator runs.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | DS1307 RTC module | Real-time clock on I²C at 0x68: seven BCD registers from 0x00 (seconds, minutes, hours, day, date, month, year). SDA/SCL to the I²C pins, 5V, GND. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno GND | DS1307 RTC module GND |
| 2 | Arduino Uno 5V | DS1307 RTC module 5V |
| 3 | Arduino Uno A4 | DS1307 RTC module SDA |
| 4 | Arduino Uno A5 | DS1307 RTC module SCL |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a DS1307 RTC module from the parts palette.
- Wire 1. Connect Arduino Uno GND to DS1307 RTC module GND.
- Wire 2. Connect Arduino Uno 5V to DS1307 RTC module 5V.
- Wire 3. Connect Arduino Uno A4 to DS1307 RTC module SDA.
- Wire 4. Connect Arduino Uno A5 to DS1307 RTC module SCL.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. The clock follows simulated time, so it keeps counting at whatever speed the simulator runs.
Arduino C++ code
// Real-time clock: set a DS1307 over I2C, then read the time back every second.
// Its registers are BCD: the tens digit in the high nibble, units in the low.
#include <Wire.h>
const byte DS1307 = 0x68;
byte bcd(byte n) { return ((n / 10) << 4) | (n % 10); }
byte dec(byte b) { return (b >> 4) * 10 + (b & 0x0F); }
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(DS1307);
Wire.write(0x00); // start at the seconds register
Wire.write(bcd(56)); // seconds (bit 7 = 0 keeps the clock running)
Wire.write(bcd(34)); // minutes
Wire.write(bcd(12)); // hours, 24-hour mode
Wire.endTransmission();
}
void loop() {
Wire.beginTransmission(DS1307);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307, (byte)3);
byte s = dec(Wire.read() & 0x7F), m = dec(Wire.read()), h = dec(Wire.read() & 0x3F);
char text[12];
sprintf(text, "%02d:%02d:%02d", h, m, s);
Serial.println(text);
delay(1000);
}
What to try
The clock follows simulated time, so it keeps counting at whatever speed the simulator runs.
Frequently asked questions
How do I wire the DS1307 Real-Time Clock project on an Arduino Uno?
Make 4 connections: Arduino Uno GND to DS1307 RTC module GND; Arduino Uno 5V to DS1307 RTC module 5V; Arduino Uno A4 to DS1307 RTC module SDA; Arduino Uno A5 to DS1307 RTC module SCL.
What parts do I need for this Arduino project?
An Arduino Uno and a DS1307 RTC module.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
Set the time on a DS1307 over I2C, then read it back every second: BCD registers by hand.
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator