Real Time Clock on the Raspberry Pi

November 7th 2012

The Raspberry Pi is an amazing device with an amazing price tag. But that also means some compromises have to be made. And one of the most notorious is the lack of a real time clock module.
Should you turn off the device, the next time you boot, the time will be all wrong. The regular way to maintain system time on the pi is via ntp, connecting to a server on the internet and syncing from there. Of course this assumes the device has access to an internet connection everytime it reboots.

There are some simple software alternatives (fake-hwclock for instance) that attempt to maintain some sync by saving the current time to file and then resuming it on boot, thus reducing the error or preventing it from going back in time to the ‘70s!
This may not be a problem for some, but for my projects I really needed an accurate clock without the assurance of an internet connection.

Fortunately there are a number of hardware solutions out there, ranging from USB GPS devices to actual RTC modules that you can plug right on the Pi’s connectors.

Part of what makes the Raspberry Pi a fun experience, is assembling your own components, be it by breadboard or soldering iron. You can find a great tutorial at adafruit on the more technical side of assembling and connecting your own, while keeping costs down.

For this tutorial I’m using an RTC module from CJE Micro’s / 4D. It sells for £10 (plus postage) and you can find it here.

RTC

Assembly was easy and it came with some simple instructions. All you have to do is plug it in the first 6 GPIO pins, as seen in the picture below:

pi with RTC

As you can see, it fits quite nicely and doesn’t take up much space. Plus, it sits a bit lower than the tallest component (the usb module).

The pi is currently running Raspbian Wheezy. It seems kernels 3.2 and above already include the required drivers.
First we must install i2c-tools:

sudo apt-get install i2c-tools
sudo modprobe i2c-dev
sudo modprobe i2c-bcm2708

This specific RTC is a ds1338, which is supported by the ds1307 driver. We load it with:

sudo modprobe rtc-ds1307

We can run a check on the devices currently connected:

sudo i2cdetect -y 0 #(change 0 to 1 if you're using rev2)

A “68” should indicate that your RTC is connected correctly

pi@raspberrypi ~ $ sudo i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:           -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 

Now we must create the new device with the correct address. To do that we must open a root shell (change /i2c-0/new_device to i2c-1/new_device if you’re using rev2):

sudo bash
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
exit

and then, fingers crossed (not actually a requirement!):

sudo hwclock -r

If all goes well this should output a fancy line with system time as read from your newly mounted RTC

pi@raspberrypi ~ $ sudo hwclock -r
Thu Nov  8 14:15:36 2012  -0.335125 seconds

It may be off but you can sync it with ntp by running:

sudo ntpd -qg

Finally, once we get everything working, we must ensure that all the modules are loaded at boot. We edit /etc/modules:

sudo vim /etc/modules

we add the following lines:

i2c-bcm2708
rtc-ds1307

And to ensure that the device is created at boot and the time is set from the RTC, we edit /etc/rc.local :

sudo vim /etc/rc.local

and add the following lines before ‘exit 0’ (change /i2c-0/new_device to i2c-1/new_device if you’re using rev2):

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
hwclock -s

The next time you boot, it should have the correct time.

« return to blog