Setting up the Real-Time Clock on Raspbian Jessie

Things are constantly moving forward in Raspberry Pi land and setting up a Real-Time Clock (RTC) on the RPi is no exception. For the old way of doing things see this page. For the new way under Jessie read on. Although this page describes setting up the RTC on  Sleepy Pi 2, this same method can actually apply to any supported RTC – you just need to change the overlay as explained below.

Setting up the i2c tools.

The first order of business is to setup / enable the i2c bus on the Rpi ( if you have already, skip this section – the Sleepy Pi setup script will have done this ). You can do this by uncommenting a line in config.txt. Here are the magic incantation in a command window:

scroll down until you find the line:

and uncomment by removing the # then exit & save (Ctrl-X then y). This will enable the i2c bus (well it will on your next reboot).

Next install the i2ctools by typing:

This will install some handy utilities for the i2c and then reboot:

Detect the RTC

To give you a warm cuddly feeling, it’s always a good idea to check the i2c bus with this command:

if you’ve got one of the original Raspberry Pi’s, then the bus was 0 so you would type:

You should see something like this:

i2c devices detected on Sleepy Pi 2 bus

i2c devices detected on Sleepy Pi 2 bus

If you are using a Sleepy Pi 1, then the listing is the same but without the 0x24 listing.

Setup the RTC

To tell the Raspberry Pi that we have a hardware clock, we need to add the following line to config.txt

scroll down to the bottom and add the line:

then exit and save (Ctrl-X then y).

BTW: if you have another RTC that you are trying to get working and want to know whether out is supported and what the overlay is called, have a look here and see if yours is listed.

Now comment out some lines in hwclock-set:

and comment out the following lines (using the #)

then exit and save (Ctrl-X then y). This will stop that script exiting early and will update the clock on boot.

Next reboot and the configuration is done.

Reading and Writing the RTC

To read the time from the hardware RTC you can issue the following commands. To read the time:

To set the time:

To copy the time from the Hardware clock to the system:

Gotchas

Whilst working with the RTC, you may come up against a few things that will bite you. Here’s a couple:

Remember to use sudo when accessing the clock.

The image below shows that if you don’t use sudo, you’ll get an (not too helpful) error shown below.

RTC Read error without sudo

RTC Read error without sudo

This can look really throw you off track when you are configuring the RTC because it makes you think that you haven’t configured it properly.

When you’ve configured the clock it appears as UU not as 68

If you run:

after you have configured the RTC in the system then it appear as UU in the map.

RTC shows up as UU when the system is configured.

RTC shows up as UU when the system is configured.

This can be pretty disconcerting, but is perfectly normal. It just means that the address 0x68 has been reserved for system use.