Real Time Clock Python Libraries For Raspberry Pi – DS1307, DS3231, PCF8563, MCP79400 – Reviews/Results
For devices like the Raspberry Pi, a Real Time Clock (RTC) is needed anytime the device is
not connected to the Internet (if you care what time it is!). NTP will keep your clock close if you are connected. An Arduino often requires a RTC especially if it needs to sleep (shutting down the internal timer based clock). A realtime clock is necessary for any project requiring accurate time keeping especially when you don’t always have an Internet connection. For example, a Raspberry Pi being sent up in a balloon or a Pi working in the remote caribbean, like Project Curacao. I have now released four libraries for I2C Real Time Clocks (RTC) for the Raspberry Pi. This project was designed to select a new I2C RTC for Project Curacao and to test the accuracy of a set of these clocks. I have now completed over 2,600,000 seconds (30 days) of testing. My goal is to reach 3,000,000 by the weekend and then write up the results. My Raspberry Pi based RTC tester just keeps humming along storing all the results into a MySQL database ready to be analyzed and graphed for my next column in Raspberry Pi Geek magazine. There are python libraries for the following devices on the SwitchDoc Labs Github: I’ve written up a brief article on each device.
These releases are part of a project to measure accuracy of a number of Real Time Clocks (RTC) and select one to upgrade the Project Curacao DS1307 as there is a problem with that clock in the project. Based on our testing, we have now selected the DS3231 to replace the flakey DS1307. We are measuring the results in PPM (Parts Per Million). For example, losing 1 second per day is a drift of 11.5 PPM while 1 PPM is a drift of 0.0864 seconds per day.
Preliminary Results
Our first glance results yield the following values:
- DS1307 – ~16 PPM
- DS3231 – < 0.3 PPM (I can only detect a slight drift at this point)
- PCF8563 – ~24 PPM
- MCP79400 – ~4 PPM (Not trimmed. on chip trim for +/-127 PPM – it is rock solid at 4 PPM at this point)
The DS3231 is the only temperature compensated device and the MCP79400 is the only
device that has a programmable trim. Project Curacao has not had good luck with the DS1307.
The full results of these tests will be published in November, 2014 in Raspberry Pi Geek magazine.
Hi,
I am struggling to get the .py program to read the DS1307. Both .py programs are installed in the /pi directory but when I run the test program I get the following results:
Results in Terminal Window
Test SDL_DS1307 Version 1.0 – SwitchDoc Labs
Program Started at:2014-11-16 17:16:51
Traceback (most recent call last):
File “testSDL_DS1307.py”, line 35, in
ds1307.write_now()
File “/home/pi/SDL_DS1307.py”, line 200, in write_now
self.write_datetime(datetime.now())
File “/home/pi/SDL_DS1307.py”, line 194, in write_datetime
dt.isoweekday(), dt.day, dt.month, dt.year % 100)
File “/home/pi/SDL_DS1307.py”, line 157, in write_all
self._write(self._REG_SECONDS, _int_to_bcd(seconds))
File “/home/pi/SDL_DS1307.py”, line 83, in _write
self._bus.write_byte_data(self._addr, register, data)
IOError: [Errno 16] Device or resource busy
**************************************************************
Results if run from Python Shell
Traceback (most recent call last):
File “/home/pi/testSDL_DS1307.py”, line 21, in
import SDL_DS1307
File “/home/pi/SDL_DS1307.py”, line 32, in
import smbus
ImportError: No module named smbus
>>>
All the i2c modules are set up and a search of the cache confirms python-smbus is installed. It also installs at start up.
I can read the DS1307 using hwclock -r(in a terminal window) and ic2detect confirms it is OK.
What am I doing wrong? I need to try to get this to read the DS1307 as I want to disconnect the RPi from its network input.
Thanks in advance
Ray Drury
Ray,
Somebody else (maybe the hwclock?) is holding the bus. That is the resource busy error. The python error is more fundamental. You don’t have the smbus module installed where python can find it.
To test the hardware / software, start with a new installation of Raspian and carefully follow the directions to install the i2c bus and make sure it works then.
I know its a pain, but there is a conflict somewhere in your installation. I have never seen this kind of a problem in any of my setups.
The other thing I wonder about are permissions. Are you running these tests under “sudo” so they have root permission?
John
John
John
Thanks for such a rapid response. Much appreciated.
To answer your last question first. Yes I am running as root.
I’ll try a reinstall and see if I can get the smbus running correctly.
Ray