As an Amazon Associate we earn from qualifying purchases.
A digital clock is one of the best first projects for learning how an Arduino talks to other chips. In this project, a DS1307 real time clock (RTC) keeps the time, and the Arduino reads it and shows hours and minutes on a four-digit seven segment display. The video below shows the finished clock running.
Why use an RTC instead of millis()
You can build a clock with the Arduino’s own timer using millis(), but the time is lost whenever power is removed, and the board’s clock source drifts. A DS1307 solves both problems. It has its own 32.768 kHz crystal and a backup battery input, so it keeps counting while the Arduino is switched off.
If you are new to the chip, our notes on DS1307 RTC basics cover the register layout in more detail.
What you need
- Arduino Uno or a similar board
- DS1307 RTC IC with a 32.768 kHz crystal, or a ready-made DS1307 module
- 3 V coin cell (usually CR2032) for backup power
- 4-digit seven segment display (common cathode in the example below)
- 7 current-limiting resistors, about 220 Ω each, one per segment line
- 4 NPN transistors with base resistors, one per digit, to switch the digit commons
- Two pull-up resistors (about 4.7 kΩ) for the I2C lines, unless your module already has them
- Breadboard and jumper wires
How the DS1307 works
The DS1307 communicates over I2C, which needs only two wires: SDA (data) and SCL (clock). On an Arduino Uno these are pins A4 and A5. The chip responds at address 0x68.
Time is stored in registers, starting with seconds at register 0, then minutes and hours. The values are stored in BCD (binary coded decimal), so 0x45 means 45, not 69. You must convert them before using them as normal numbers:
- BCD to decimal: (value >> 4) * 10 + (value & 0x0F)
- Decimal to BCD: ((value / 10) << 4) | (value % 10)
Bit 7 of the seconds register is the clock halt bit. If it is set, the oscillator is stopped and the time will not advance. Writing the seconds register with that bit cleared starts the clock.
Wiring
DS1307 to Arduino
- VCC to 5 V and GND to GND
- SDA to A4 and SCL to A5
- Coin cell between VBAT (positive) and GND
- Crystal between X1 and X2 if you use the bare IC
Display to Arduino
Connect segments a to g through 220 Ω resistors to Arduino pins 2 to 8. Connect the four digit commons to pins 9 to 12 through NPN transistors, so each digit can be switched on in turn. The transistors matter: with all segments lit, the current through one digit is more than an Arduino pin should carry.
If you are still learning how segments are driven, read our guide to seven segment with Arduino first.
How multiplexing works
A four-digit display shares the same segment lines across all digits. The Arduino turns on one digit at a time, sets the segments for that digit, waits a few milliseconds, and moves to the next. Done fast enough, your eye sees all four digits lit at once. This uses 11 pins instead of 28.
Arduino sketch
This sketch reads hours and minutes from the DS1307 twice a second and refreshes the display continuously. It assumes a common cathode display, with segment pins HIGH to light and digit transistors driven HIGH to select.
#include <Wire.h>
const byte RTC_ADDR = 0x68;
const byte segPins[7] = {2,3,4,5,6,7,8}; // a to g
const byte digitPins[4] = {9,10,11,12};
const byte font[10] = {0b0111111,0b0000110,0b1011011,0b1001111,0b1100110,0b1101101,0b1111101,0b0000111,0b1111111,0b1101111};
byte hours = 0, minutes = 0;
unsigned long lastRead = 0;
byte bcdToDec(byte v) { return (v >> 4) * 10 + (v & 0x0F); }
byte decToBcd(byte v) { return ((v / 10) << 4) | (v % 10); }
void setTime(byte h, byte m, byte s) {
Wire.beginTransmission(RTC_ADDR);
Wire.write(0);
Wire.write(decToBcd(s)); // bit 7 clear starts the clock
Wire.write(decToBcd(m));
Wire.write(decToBcd(h)); // 24-hour mode
Wire.endTransmission();
}
void readTime() {
Wire.beginTransmission(RTC_ADDR);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(RTC_ADDR, (byte)3);
Wire.read(); // seconds, not shown
minutes = bcdToDec(Wire.read());
hours = bcdToDec(Wire.read() & 0x3F);
}
void showDigit(byte pos, byte value) {
for (byte i = 0; i < 4; i++) digitalWrite(digitPins[i], LOW);
for (byte s = 0; s < 7; s++) digitalWrite(segPins[s], (font[value] >> s) & 1);
digitalWrite(digitPins[pos], HIGH);
delay(3);
}
void setup() {
Wire.begin();
for (byte i = 0; i < 7; i++) pinMode(segPins[i], OUTPUT);
for (byte i = 0; i < 4; i++) pinMode(digitPins[i], OUTPUT);
// setTime(12, 30, 0); // uncomment once to set the time, then comment out and upload again
}
void loop() {
if (millis() – lastRead > 500) { readTime(); lastRead = millis(); }
showDigit(0, hours / 10);
showDigit(1, hours % 10);
showDigit(2, minutes / 10);
showDigit(3, minutes % 10);
}
Setting the time
- Uncomment the setTime() line in setup() and enter the current time.
- Upload the sketch once.
- Comment the line out again and upload a second time.
The second upload is important. If you leave setTime() active, the clock resets to the same time every time the Arduino restarts, and the battery backup becomes pointless.
Common problems
- Display shows 00:00 or random values: check the SDA and SCL wires and make sure the I2C lines have pull-up resistors.
- Time does not advance: the clock halt bit may be set, or the crystal is not connected properly. Write the time again with setTime().
- Time resets after power loss: the backup battery is missing, flat or connected the wrong way round.
- Display flickers: shorten the delay in showDigit() and avoid long delays elsewhere in loop().
- Segments look dim or uneven: check the resistor values and that the transistors are switching each digit fully.
Ways to improve it
- Blink a colon or decimal point every second using the seconds value.
- Add buttons to set the hour and minute without re-uploading code.
- Use a driver chip or shift register to free up Arduino pins.
- Try another RTC, such as the BQ32000 real time clock, which also uses I2C.
- Move to a different microcontroller. We have an example of the DS1307 with a PIC18F4550.
- If you would rather show more than numbers, see our guide to LCD and OLED displays.
FAQ
How accurate is a DS1307 clock?
It depends mostly on the crystal and the temperature. A DS1307 can drift by a few seconds per day or more. For better accuracy, a temperature-compensated RTC such as the DS3231 is a common upgrade.
Can I use a common anode display?
Yes. You need to invert the logic: segments light when their pin is LOW, and the digit commons are switched to the positive supply. The multiplexing idea is the same.
Do I need a battery for the DS1307?
The clock works without one while the Arduino is powered, but it will lose the time when power is removed. A 3 V coin cell on VBAT keeps it running.
Why does the DS1307 give strange hour values?
The hours register can be set to 12-hour or 24-hour mode, and the upper bits are flags. Masking the value with 0x3F in 24-hour mode, as in the sketch above, removes them before converting from BCD.