As an Amazon Associate we earn from qualifying purchases.
The DS1307 is a low-cost real time clock (RTC) chip that keeps track of seconds, minutes, hours, day of the week, date, month and year. It is a common choice when an Arduino project needs to know the time, even after the board has lost power. This article shows how to connect the RTC DS1307 to an Arduino Uno, read the time over I2C, and send it to a PC through the serial port.
The video below shows the project working, and the full sketch follows further down the page.
What the DS1307 does
An Arduino has a timer, but it resets whenever the board is powered off or restarted. An RTC chip solves this. It counts time on its own using an external 32.768 kHz crystal, and it can keep running from a small backup battery while the main supply is off.
Key points about the DS1307:
- It talks to the microcontroller over the two-wire I2C bus (SDA and SCL).
- Its I2C address is 0x68, which is the value used in the code below.
- It stores seconds, minutes, hours, weekday, date, month and year in registers, in BCD format.
- It has a small block of battery-backed RAM you can use for your own data.
- It can switch to a backup battery when the main power drops.
What you need
- An Arduino Uno
- A DS1307 RTC module (or the bare chip with a 32.768 kHz crystal and a battery holder)
- A backup battery for the module, usually a coin cell
- Jumper wires and a USB cable
Most ready-made DS1307 modules already include the crystal, the battery holder and the pull-up resistors that the I2C lines need. If you build the circuit on a breadboard from the bare chip, check the data sheet for the recommended pull-ups and crystal.
Wiring the DS1307 to the Arduino Uno
On the Uno, the I2C pins are A4 (SDA) and A5 (SCL). The connections are:
- SDA on the module to A4 on the Arduino
- SCL on the module to A5 on the Arduino
- VCC to the 5 V pin
- GND to GND
Make sure the backup battery is installed. Without it, the clock will not keep time when the Arduino is unplugged.
The Arduino code
The sketch uses the standard Wire library. It reads seven registers starting at address 0, converts each value from BCD to decimal, and prints the result to the serial port at 9600 baud once per second.
//connect SDA---> A4, SCL--->A5
#include <Wire.h>
const int DS1307 = 0x68; // Address of DS1307 see data sheets
byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
byte year = 0;
void setup()
{
Wire.begin();
Serial.begin(9600);
//setTime();
}
void loop()
{
readTime();
delay(1000);
}
byte bcdToDec(byte val)
{
int msbdec=val/16*10; //gets decimel msb of BCD value(4 bits)
int lsbdec = val%16;//gets decimel lsb of BCD value(4 bits)
int total=msbdec+lsbdec;
return (total);
//return ((val/16*10) + (val%16));
}
void readTime()
{
Wire.beginTransmission(DS1307);
Wire.write(byte(0));
Wire.endTransmission();
Wire.requestFrom(DS1307, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read());
weekday = bcdToDec(Wire.read());
monthday = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
Serial.print(monthday);
Serial.write('-');
Serial.print(month );
Serial.write('-');
Serial.print(year);
Serial.write('-');
Serial.write('-');
Serial.print(hour);
Serial.write(':');
Serial.print(minute);
Serial.write(':');
Serial.print(second);
Serial.write(0xd);
}
void setTime() {
// The following codes transmits the data to the RTC
Wire.beginTransmission(DS1307);
Wire.write(byte(0)); //starts sending data writing
Wire.write(0x12);//second
Wire.write(0x20); //minute
Wire.write(0x15); //hour
Wire.write(0x7); //weekday
Wire.write(0x30);//monthday
Wire.write(0x8);//month
Wire.write(0x14);//year
Wire.write(byte(0));
Wire.endTransmission();
// Ends transmission of data
}
How the code works
BCD values
The DS1307 stores each number in binary-coded decimal. The upper four bits hold the tens digit and the lower four bits hold the ones digit. For example, 0x30 means 30, not 48. The bcdToDec() function splits the byte into those two parts and combines them into a normal decimal number.
Reading the time
In readTime(), the Arduino first writes register address 0 to the chip. This points the chip at the seconds register. It then requests seven bytes, which arrive in this order: seconds, minutes, hours, weekday, date, month and year. Each one is converted and printed in the format date-month-year–hour:minute:second.
Setting the time
The setTime() function writes the values to the same registers. Because the chip expects BCD, you write the values as hexadecimal numbers that look like the decimal ones. For example, 0x20 for minutes means 20 minutes. The final Wire.write(byte(0)) writes the control register, which turns off the square-wave output.
The call to setTime() in setup() is commented out. To set the clock:
- Edit the values in
setTime()to the current time and date. - Remove the
//in front ofsetTime();insetup(). - Upload the sketch once.
- Add the
//back and upload again, so the time is not reset every time the board restarts.
Skipping step 3 and 4 is a common mistake. If your clock always shows the same starting time after a reset, the setting call is probably still active.
Viewing the output on your PC
Open the Serial Monitor in the Arduino IDE and set the speed to 9600 baud. You should see a new line of text every second. Any serial terminal program set to the same speed will work too, which is useful if you want to log the time to a file or read it in your own PC application.
The sketch prints plain numbers, so values below 10 appear without a leading zero (for example, 5 instead of 05). If you want a neater display, add a small check that prints a zero first when the value is less than 10.
Troubleshooting
- Output is all zeros or strange values: check the SDA and SCL wires, and confirm the module is powered with 5 V.
- The clock does not advance: the chip has a clock halt bit in the seconds register. Writing the time with
setTime()starts the clock. - Time resets when unplugged: the backup battery is missing, flat or not seated correctly.
- No response from the chip: make sure the address is 0x68 and that the I2C lines have pull-up resistors.
- The hour looks wrong: the DS1307 can run in 12-hour or 24-hour mode. This code assumes 24-hour mode, which is what the set routine writes.
Accuracy and alternatives
The DS1307 depends on an external crystal, so its accuracy is affected by the crystal quality and by temperature. For a hobby clock or a data logger, it is usually fine, though you may notice it drift over weeks. If you need better accuracy, a temperature-compensated RTC such as the DS3231 is often used instead, and it also works over I2C.
Related RTC projects
Once the basic reading works, you can build on it. Here are a few related projects on this site:
- If you prefer another compiler, there is a MikroC version of this project.
- You can build a clock that shows the date and time on a seven segment display.
- For a PIC microcontroller, see the PIC18F4550 with an LCD simulation.
- To compare with a different chip, read about the BQ32000 real time clock with Arduino.
FAQ
What is the I2C address of the DS1307?
The DS1307 uses the 7-bit I2C address 0x68. The address is fixed, so you cannot connect two DS1307 chips to the same I2C bus.
Which Arduino Uno pins are used for the DS1307?
Connect SDA to A4 and SCL to A5. Power the module from the 5 V and GND pins.
Why does the DS1307 need a battery?
The battery keeps the clock running when the main power is off. Without it, the time is lost whenever you unplug the Arduino.
Why are the values stored as BCD?
The DS1307 keeps each digit of a number in its own four bits. This is why the code converts the values to decimal when reading, and writes them in BCD form when setting the time.