IAN LANG ELECTRONICS

A real time clock (RTC) is a circuit consisting of a counter and it's associated hardware which, when powered by a small battery, can keep track of the passage of time and pass this on to a larger device when that larger device is powered on. By far and away the most common application for an RTC is in computers; it's what keeps the time and date constant when you turn the computer off. You find them in games consoles too, and in the more expensive digital clocks as when the battery dies the clock doesn't need to be reset again.

Now, we could get an integrated circuit, suc as the DS1307, and wire it in with a crystal and the concomitant resistors and capacitors needed to get it going, then spend a couple of days working out how to program it to the right date and time. We could do that. Indeed.

We aren't though. We're going to let Limor Fried do the hard work for us, and buy the Adafruit DS1307 breakout board from Oomlout. The link to it is on the left.

Using a Real-Time Clock on your Arduino

Go Back DS1307 Breakout

It comes in a kit form and so a bit of soldering is necessary, and you'll need to import a library into your IDE.

 

Full instructions on doing both are here:

 

Library & Assembly

The restrictions of the running library mean that you have to use pins A4 and A5 to get input to the Arduino from your RTC (as well of course as having 5V and GND). Here's the wiring diagram with the breakout board made much larger than life for clarity's sake:

If you've succesfully installed the RTClib files in your Arduino IDE you should now find in files/examples some new ones under RTClib. Try them out. Here's some more code that tells you the time and date. Firstly we need to update the RTC with the time and date on your computer. It might be a good idea to make sure that's right first; mine is so off we go uploading the following code:

 

  #include <Wire.h>

#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {

 

    Serial.begin(300);

    Wire.begin();

    RTC.begin();

    RTC.adjust(DateTime(__DATE__, __TIME__));

 

}

 

void loop(){}

 

 

 

DO NOT COMPILE AND THEN UPLOAD THIS CODE, just hit the upload button first and let it compile and upload. If you compile it first, the clock will be off by a couple of minutes. Now upload this code:

 

 

#include <Wire.h>

#include "RTClib.h"

 

RTC_DS1307 RTC;

 

void setup () {

    Serial.begin(300);

    Wire.begin();

    RTC.begin();

}

 

void loop () {

    DateTime now = RTC.now();

   

   

Serial.print(now.hour(), DEC);

Serial.print(':');

Serial.print(now.minute(), DEC);

Serial.print(':');

Serial.print(now.second(), DEC);

Serial.print("   ");

Serial.print(now.day(), DEC);

    Serial.print("   ");

    Serial.print(now.month(), DEC);

        Serial.print("   ");

    Serial.print(now.year(), DEC);

Serial.println();

   

    delay(1000);

}

 

Having uploaded the second sketch above, open your serial monitor and you should see something like this:

 

 

It  tells you the current time, in 24 hour format, and the current date 24 12 2011 (Christmas Eve! Huzzah!)

You'll notice that sometimes it skips a second, 18:52:34 goes to 18:54:36

 

This is not a fault in the RTC, this is due to cusping on the very end  of the preceding second and the very beginning of the next. The delay in the sketch is set for 1 second and there are a few milliseconds delay in the processing time. If you find it annoying, try lowering the delay time in the sketch until it counts perfectly.

 

You'll see that two libraries are called, wire and RTClib. Both are necessary as RTClib will not work without wire. (I tried it). Then we have this:

 

RTC_DS1307 RTC;

 

which defines the type of RTC IC being used.

 

We have to begin the two libraries and the serial communications protocol in the setup, the latter is not of course necessary to the running of the RTC but is if we want to see the display.  In the loop, this line appears first:

                                                                                                      DateTime now = RTC.now();

 

This is the system variable for reading the RTC. It reads the time in hours,minutes and seconds, and the date in number and month.  We now just need to send this data to be printed to the serial monitor. All of them are done exactly the same way, and so we just need to consider how one is done and that's them all. Let's look at the hour:

 

Serial.print(now.hour(), DEC);

 

now.hour is the system variable that we must use; it's part of the library and reads just the hour part of  DateTime now.  We have to tell it that we want it printed out as a decimal value DEC  otherwise the results we get are somewhat weird. Try removing the DEC parts and see what you get. It should be lots of blanks. Having done the time and date we need to move down a line and so:

 

Serial.println( );

 

Easy as falling off a slippy log. Now, what practical purposes can we put this to? The most obvious thing is of course a clock. Are we really going to go for the most obvious thing in such a predictable manner though?

 

Yes we are. You know I'm lazy. Let's start it over the page

 

 

 

More>