The DS1307 IC is used to accurately count time and date. Once set, and as long as it has power, it will continually count the time by itself. It uses the I2C bus to transfer data to the microcontroller. Below are the key features of this IC.
- Real-Time Clock (RTC) Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the week, and Year with Leap-Year Compensation Valid Up to 2100
- 56-Byte, Battery-Backed, General-Purpose RAM with Unlimited Writes
- I²C Serial Interface
- Programmable Square-Wave Output Signal
- Automatic Power-Fail Detect and Switch Circuitry
- Consumes Less than 500nA in Battery-Backup Mode with Oscillator Running
- Optional Industrial Temperature Range: -40°C to +85°C
- Available in 8-Pin Plastic DIP or SO
The DS1307 IC has only 8 pins and the pinout are as follows :
Because the DS1307 communicates through I2C interface, we need two pull up resistors for both the SDA and SCL channel. I used 10k in my circuit.
For the crystal, the datasheet states that it needs a 32.768kHz Quartz Crystal. You will usually find these on digital watches.
The DS1307 needs a coin cell battery to provide backup power in case the power on the main circuit goes off, for instance when the user is changing the battery of the device. The coin cell battery is connected to Vbat and GND.
I got this coin cell battery holder, it’s for the CR2032 type.
And a cheap battery to go with it.
A coin cell battery is usually rated at 3V but I got 3.3V out of this guy. A quick read on the datasheet confirms that it’s alright.
Here’s the DS1307 IC set up on the Arduino Prototype Shield.
I found a well written library called the RTClib and in it there’s an example sketch which sets the time on the DS1307 IC. Upon the first power up, the DS1307 will start counting from 1/1/2000 0:00 which is not right, so we need to set it up to the correct date and time. We’ll only need to do this once as long as the coin cell battery is not removed.
What this code does is basically sets the time and date according to when the sketch is compiled. Of course there will be a slight delay from the time the code compiles and it is fully uploaded to the Arduino. I think it’s less of a problem on the newer Arduinos that are using the Atmel 8U2 and 16U2 as the upload times are significantly faster.
The code will read the time and date from the DS1307 and display it on the Serial Monitor.
Now that I know the time and date is correct, I went on to build a fully independent circuit. Which means the circuit is not in any way connected to a computer.
Used an LCD to display the time and date and the circuit is powered by a 9V battery.
With the RTClib, the programming is made so much easier and I can go as far to say that it’s cheating. LOL. To be frank, I’m still very new and don’t have much knowledge on the I2C communication. With such good libraries on the Arduino platform, it has made me kind of lazy
I have thought of building a permanent digital clock circuit on a PCB and I’m still deciding on what power source to use so that the batteries doesn’t need to be changed often. To do that I need to know the power consumption so I hooked up the circuit in series with the multimeter and measured its’ current usage.
Around 67mA. It’s not much but I think I can get it lower by using a 7 segment display. I think I’ll try this on a PIC microcontroller too to compare their power consumption.