Adding a DS1307 Real Time Clock to Raspberry Pi

Grove RTC
The DS1307 RTC (Real Time Clock) is widely available, but adding one to a Raspberry Pi turned out to be a bit of a Yak shave.

I'm gradually adding code and wiring instructions to my babelboard hardware and software projects, and I thought it was time to cover an RTC.

I had a Grove RTC board from years ago and decided I'd document connecting that to a Raspberry Pi.

The need for Battery Power


First Yak Shave moment: RTCs normally use a coin battery to maintain power so they can keep time when their host is off, and the battery in my Grove board was way past its use-by date.

This happened on Sunday evening, and the local supermarkets were shut, but the local convenience store was open. No CR1220, though, so I had to wait until the next day to get started.

While I was waiting I read the data sheet for the DS1307 chip and realised I had another problem.

Don't connect this chip directly to your Pi!


Seeed Studios describe the DS1307 breakout as Pi-compatible, and so it is, if you connect it via a GrovePi board. The DS1307 is an I2C device and it requires a 5 volt supply.

If you do a direct connection to the Pi's I2C pins and apply 5v to the Grove RTC board, the pullup resistors will apply 5v to the Pi and bad things will eventually happen to the Pi.

Babelboard to the rescue


babelboard
One of my babelboards is designed to fix this very problem. It connects to the Pi header, but it has two Grove I2C connectors.

One is directly connected, so it runs at 3.3V.

The other is connected via a level shifter. It provides a 5v supply and handles 5v on the SDA and SCL I2C lines, but the level shifter means that the Pi only ever sees 3.3 v signals.

That's just what's needed for the DS1307, so I thought I was away to the races.

Not so fast!

Numpy? Why?


why numpy?
I installed the Grove script for the DS1307 and tried to run it. I got an error message telling me that Python couldn't find numpy.


What? Why does an RTC program need numpy's fast array handling?

It turns out that the Grove software assumes you want to install the whole Grove library, and everything depends on a Grove package, which in turn needs numpy for one particular board!

working output
Fortunately Adafruit has code to drive the DS1307 on the Pi. Once I'd installed that I could interface with the RTC and everything went swimmingly.

I'll document what I did and add it to the babelboard projects over the weekend.

After that, I'm an I2C keypad reader which uses an Arduino to do the heavy lifting, followed by a similar I2C 'old skool' LCD display. I'll post about those when they are ready.

If you're based in London, I'll be talking about micropyhon, CircuitPython and Blinka at the Raspberry Pint meeting at the Microsoft Reactor next Tuesday (25 Feb). There are other interestin talks; you can sign up here.




Comments

Popular posts from this blog

Controlling a Raspberry Pi Pico remotely using PySerial

Five steps to connect Jetson Nano and Arduino

Raspberry Pi Pico project 2 - MCP3008