Discuss this article in the Nuts & Volts forums at http://forum.nutsvolts.com.
MAKING THE DS1307 RTC
Last month, we began looking at the
accuracy of the DS1307 RTC and ways to
calibrate it so that it will be more accurate. We
learned that it can be off by up to + or - 1.7
seconds per day. That's almost 12 seconds per
week, or 10-1/2 minutes per year. Does this
matter? Well, if you are plugging your AAC
(Arduino Alarm Clock) into a PC every few days
and recalibrating it, then probably not. After all,
what’s a few seconds among friends? However,
if you are going to set this thing as some sort of
data logger that goes unattended for months,
then yes it probably will matter. It really
depends on the application.
Last month, I claimed that I thought I could
make this thing as accurate as the more
expensive and accurate DS3234 that has a built-in
temperature compensated crystal. The modest folks at
SparkFun call their breakout board for this chip "DeadOn
RTC - DS3234." So, we can take it that the rated ± two
minutes per year is DeadOn. Will ours do this? I think so,
but I'll have to run one for a few months to get an idea.
So maybe, maybe not. As they say, time will tell.
Also last month, we approached calibration by
watching the RTC and the PC for a week or so to see
how far off the clock drifted. We then derived the
number of seconds that passed for each second that the
RTC drifted, and whether that drift was + or -. We called
these seconds calSeconds and manually entered them
into the 'calibration seconds' box in the PCAAC
My first inclination was to calibrate by having the RTC
adjusted each time it got off by a second. Unfortunately, I
kept running into walls doing that, mostly due to me over-thinking the whole thing. Finally, after wasting too much
time debugging, I decided to leave the RTC alone and
simply calculate the seconds it is off each time the time is
requested. This turns out to be fairly simple (ahem!). All
we need to know is the last time the RTC was calibrated
by the PC, the number of seconds in which the RTC gains
or loses a second, whether it is, in fact, a gain or loss, and
if the calibration has been set:
■ FIGURE 2. Arduino alarm clock.
along with the RTC time to calculate the calibrated time.
Calculating the 'real' real time is simply a matter of
subtracting the lastSet time from now to get the number
of seconds since the last time the PC calibrated the RTC.
Then, we divide the seconds elapsed by the calSeconds
(the number of seconds that it takes the RTC to gain or
lose a second) to get the number of seconds the RTC is
off. [Okay, stop and think ... read it again, think some
more ... got it? Thought so.]
More mathish: 'now' minus 'last time set by the PC'
divided by 'seconds gained or lose a second' equals
'seconds gained or lost':
Seconds gained or lost = (Now - lastSet) / calSeconds
We can then look at addSubtract to see if we need to
add or subtract these seconds to get our adjusted real
time. Of course, we only want to do this if the calibration
has, in fact, been done. So, we should check calIsSet and
make sure it is true before doing the calibration. Then,
depending on whether you are gaining or losing, you get
the adjusted time by adding or subtracting adjustSeconds
from what the lying RTC is saying now.
calSeconds — The number of seconds it takes the
RTC to gain or lose a second.
lastSet — The 32-bit unixtime that the RTC was last
set to the PC time.
addSubtract — Whether the RTC is gaining or losing
calIsSet — Has the calibration been set?
These values are saved to the EEPROM and each
time the Arduino starts up, it accesses these values to use
68 May 2013
The Arduino proto shield alarm clock kit lets you
build an alarm clock circuit on a breadboard and port
that circuit to a PCB. This kit is the basis for my
presentation of how to do a complete Arduino design
cycle using Fritzing to go from a breadboard
prototype, through schematic creation and breadboard
layout, and finally producing your own printed circuit
board. You can get the kit or materials that support this
learning activity from the Nuts & Volts webstore.