Raspberry Pi Python Library for DS1307

DS1307 Python Raspberry Pi Test Jig
The Four Realtime Clock Test Jig

Raspberry Pi Python Library for DS1307 Real Time Clock

DS1307 Python Raspberry Pi Test Jig
The Four Realtime Clock Test Jig

We have now released the first of a series of Python libraries for the Raspberry Pi for various Real Time Clocks (RTC).  We are also including how to modify the DS1307 to run with the Raspberry Pi.  The original code is the DS1307 from @XiErCh. The main changes involve removing the 12 hour mode, which didn’t work correctly in all cases. This library now only runs in 24 hour mode. We will be releasing libraries for the MCP79400, DS3231 and the PCF8563 over the next couple of weeks. To the left is a picture of the test jig.  The  library is on https://github.com/switchdoclabs/RTC_SDL_DS1307.

Python DS1307 Librrary
Test Jig for Modified DS1307

Remember that the DS1307 (we used the Adafruit DS1307 Breakout Board) is a 5V device. Before you hook it up to a Raspberry Pi, you need to do a couple of things.

Modifying the DS1307 for the Raspberry Pi

One of the things that is very important to remember about the Raspberry Pi, is that all the GPIO inputs and outputs are 3.3V and are not tolerant of 5.0V.  You will burn out pins on the Raspberry Pi if you connect 5.0V outputs to them.   The Adafruit DS1307 is a 5V board.  What do we do about this?

The Raspberry Pi has a 5V power supply pin (pin 2 on the P1 header on the Raspberry Pi) so we can connect that up to the DS1307 (see the green wire in the picture to the right). The GND pin is Ground, so we can connect that. The issue is the SDA and the SCL pins for the I2C interface. Remember that these two I2C lines are open-drain lines. The problem for our Raspberry Pi is that SDA and SCL are connected to two 2.2K pullup resistors on the DS1307 and these are connected to the 5.0V line. We can’t connect these lines to the Raspberry Pi without modifications.  You will destroy the Raspberry Pi I2C GPIO lines if you don’t remove the resistors.   You can see the clipped resistors in the picture above.

Other DS1307 boards will have to be looked at individually to determine if these pull-up resistors are present.

DS1307_bb
DS1307 Breadboard for Raspberry Pi

Here is the hookup diagram from Fritzing:

We will be releasing libraries for the MCP79400, DS3231 and PCF8563 in the next few weeks.

Example Code

There is a testSDL_DS1307.py  included with the library.  Here is the bare code needed:

import time
import datetime
import SDL_DS1307


ds1307 = SDL_DS1307.SDL_DS1307(1, 0x68)
ds1307.write_now()

# Main Loop - sleeps 10 seconds, then reads and prints values of all clocks


while True:


 print ""
 print "Raspberry Pi=\t" + time.strftime("%Y-%m-%d %H:%M:%S")

 print "DS1307=\t\t%s" % ds1307.read_datetime()

 time.sleep(10.0)

You should see a result like this:

Raspberry Pi= 2014-07-20 19:36:32
DS1307= 2014-07-20 19:36:32

Raspberry Pi= 2014-07-20 19:36:42
DS1307= 2014-07-20 19:36:42

I ran a test for 5 days and the DS1307 drifted 7 seconds for an accuracy of 16ppm (parts per million). More accuracy data later when I have all the units running.

Project Curacao, Arduino and the DS1307

We used a Adafruit DS1307 on Project Curacao connected to the Arduino Battery Watchdog. This has been the source of reliability issues on the Arduino. It requires a reboot (power on / off) about once a month. And it is the real time clock that is getting lost. Noise? Heat? We are not sure. But we are replacing it in September.

DS1307 Arduino
DS1307 in Project Curacao
DS1307 Arduino
Project Curacao DS1307 Connected to the Arduino