Connecting your Raspberry Pi with a real-time clock

Test Code for the DS1307

Now it is time to test the DS1307 on the Raspberry Pi using a Python library. First you need to install the library

cd /home/pi
git clone https://github.com/switchdoclabs/RTC_SDL_DS1307.git

and run the test:

cd RTC_SDL_DS1307
sudo python testSDL_DS1307.py

You should see a result similar to Listing 2.

Listing 2

Clock Test

01 Test SDL_DS1307 Version 1.0 - SwitchDoc Labs
02
03
04 Program Started at:2014-07-20 19:36:32
05
06 Raspberry Pi=   2014-07-20 19:36:32
07 DS1307=         2014-07-20 19:36:32
08
09 Raspberry Pi=   2014-07-20 19:36:42
10 DS1307=         2014-07-20 19:36:42

If you let this test run overnight, you will notice that the DS1307 time will start to drift, either fast or slow.

The code to test the DS1307 is trivial. Listing 3 shows the bare necessities. Lines 1-3 import the libraries, including the SDL_DS1307 library. Lines 5 and 6 initialize the ds1307 object and write the current time out to the RTC. Line 15 reads the value from the DS1307 and outputs it to the screen.

Listing 3

DS1307 RTC Test Code

01 import time
02 import datetime
03 import SDL_DS1307
04
05 ds1307 = SDL_DS1307.SDL_DS1307(1, 0x68)
06 ds1307.write_now()
07
08 # Main Loop - sleeps 10 minutes, then reads and prints values of all clocks
09
10 while True:
11
12         print ""
13         print "Raspberry Pi=\t" + time.strftime("%Y-%m-%d %H:%M:%S")
14
15         print "DS1307=\t\t%s" % ds1307.read_datetime()
16
17         time.sleep(10.0)

Testing the accuracy of a set of four RTCs is the subject of the next SwitchDoc Labs column.

Results

I ran a test of the DS1307 for five days (429,179 seconds) comparing the DS1307 time with the Raspberry Pi time. The clock on the Raspberry Pi is not very accurate, but the ntp daemon will correct on a regular basis, making the Pi a pretty good clock as long as you are connected to the Internet.

The DS1307 was running seven seconds fast by the end of the test. That means my device gains about a second a day, which comes out to about 16ppm (parts per million):

[(error in secs)/(total secs)]x1,000,000

The data sheet says the accuracy is about 23ppm, so the test seems reasonable. As I will discuss next time, the two main failings of the DS1307 is that it is not temperature compensated, and you can't apply a correction in the device.

Run your own test and let me know the results.

Questions?

If you have a suggestion for a column, please send email to raspigeek@switchdoc.com. If you have specific questions, please post on the SwitchDoc Blog (www.switchdoc.com), so everybody can learn from your question. I'll be picking the best question and answering it in the next SwitchDoc Labs column.

Buy this article as PDF

Express-Checkout as PDF

Pages: 5

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content