Raspberry Pi: Adding a Real-Time Clock on Raspbian Jessie

Updated version of this for Raspbian Stretch here.

The Raspberry Pi is a great computing device at a great price – but to keep costs down, sacrifices had to be made.  One of those sacrifices was a so-called “real-time clock” or RTC.  Without one, the device forgets the time as soon as it’s powered down or rebooted.  Fortunately, Internet connectivity is available in most cases and NTP can be used to synchronize the clock.  In cases where Internet connectivity will be spotty or nonexistent, it may be a good idea to install a real-time clock.

There are many guides to configuring this in Raspbian Wheezy, but I was not able to find much for Jessie.  Jessie is different from Wheezy in that it runs systemd, which is a completely new way to manage the system and services. I’d also like to note that this was all done on a Raspberry Pi model 2.

The RTC

I purchased a bunch of RTCs from eBay.  Search for Raspberry Pi DS3231 – they can be had for about $2 apiece.  Here’s what it looks like:

Raspberry Pi Realtime Clock

 

Install the RTC Device

You’ll want to install the RTC on the inside row of pins at the “bottom” of the connector, like so:

raspberry-pi-real-time-clock-rtc-ds3231-31-300x210

 

 

Install the Software

You’ll want two packages: i2c-tools and python-smbus. To install:

Configure the Device

First, go into the raspi-config utility by typing

You’ll see a screen that looks like this:

Screen Shot 2015-10-29 at 9.03.49 PM

Select Advanced Options, then I2C.  Select Yes to enable it, and Yes to automatically load the kernel module.

Then, to be safe, add these lines to /etc/modules-load.d/rtc-i2c.conf:

Configure udev to set the system time from the RTC when the device appears:

Load the kernel modules immediately:

Finally, check to see if the device is available:

You should see something like this:

The “68” is the i2c address of the clock module.

Systemd

Next, we’ll need to set up a systemd service called rtc-i2c.service. This will cause systemd to create the RTC device on boot.  And remember, when udev sees the device appear, it will call the hwclock utility to set the system clock from the hardware clock.

First, the config file:

Then, the actual systemd service unit:

Finally, enable the service we just created:

Cleanup

Raspbian by default includes a fake hardware clock, which we don’t need anymore because we have a real one.  Remove the package and service:

Notes

If you run ntpd to sync the time to the internet, it will automatically update the hardware clock every 15 minutes.

Credits

Many thanks to github user h0tw1r3 for the systemd instructions which came from this gist.

5/5 - (4 votes)
Scroll to Top
Scroll to Top