Accessing the Real-Time Clock from the Raspberry Pi

You can can talk to the Real-time clock (RTC) from both the Raspberry Pi and the Arduino (though try not to do it at the same time!) over the 12c bus.

[box type=”info”]These instructions describe the setup of the RTC under older Non-Jessie Raspbian. If you are using Sleepy Pi 2 head over to this page instead.[/box]

Install and Enable the I2C Tools

First of all you need to add the following two lines to the /etc/modules file using nano. You need root privileges to do this so use:

sudo nano /etc/modules

and add:

i2c-bcm2708 
i2c-dev

It should then look a bit like this:

modules-file-i2c-450

 

You will need to reboot for the changes to take effect.

You now need to install the i2c tools by typing the following into a Terminal window such as LXTerminal (make sure you are connected to the internet as these are downloaded and installed from the Repository).

sudo apt-get install i2c-tools

Detect your RTC

Once the i2c tools have been installed you can see if you can see the RTC by typing the following command in LXTerminal:

sudo i2cdetect -y 1

This works for the latest models of Raspberry Pi’s. If you have one of the earlier 256Mb ones, then the i2c bus was slightly different and you need the following command instead:

sudo i2cdetect -y 0

In either case you should see the following in LXTerminal:
i2cdetect of the RTC from LXTerminal

 

The RTC will be detected with the address 0x68. If you are running Sleepy Pi 2, there will be another device shown on address 0x24 (which is a GPIO expander).

Note: just about all RTC’s have the same i2c address of 0x68!

Access your RTC

First of all to check that the RTC is working we can manually type the necessary magic incantations from LXTerminal and see if we can read / write the clock.

For Sleepy Pi 1 type the following lines:

sudo modprobe rtc-ds1374
sudo /bin/bash -c "echo ds1374 0x68 > /sys/class/i2c-adapter/i2c-1/new_device"
sudo hwclock -r

For Sleepy Pi 2 use these lines instead (there is a different RTC on Sleepy Pi 2)

sudo modprobe rtc-pcf8523
sudo /bin/bash -c "echo pcf8523 0x68 > /sys/class/i2c-adapter/i2c-1/new_device"
sudo hwclock -r

(in both cases, if you have an original 256Mb Pi then replace “i2c-1” with “i2c-0”)

Which should give you something like this:

RTC Setup in LXTerminal

 

Set the Right Time on the RTC

The easiest way to do this is to first setup the right time on the Raspberry Pi and then transfer that to the RTC.

To set the time on the Raspberry Pi just connect it to the internet via either the Ethernet connection or by Wi-Fi. It will automatically sync itself to an internet time service. To check that the time is correct type “date” on the command line and the current time will be displayed.

Once you have set the time on the Raspberry Pi you can now transfer it to the RTC with the following command:

sudo hwclock -w

Set the Time at Startup

If we want to set the time at startup then we first need to add the RTC kernel module to the /etc/modules using either nano, so we get:

etc/modules files with ds-1374 module added

To set the clock automatically when the Raspberry Pi boots you can edit the /etc/rc.local file using nano as follows:

/bin/bash -c "echo ds1374 0x68 > /sys/class/i2c-adapter/i2c-1/new_device"

# Check for an IP address

_IP=$(hostname -I) || true
_HOSTNAME=$(hostname) || true

if [ "$_IP" ]; then
    # We have a network, set the hwclock from the system time.
    printf "\nSetting hardware clock from system time\n"
    /sbin/hwclock -wu

    printf "\n%s IP address is %s\n" "$_HOSTNAME" "$_IP"
else
    # No network, set the system time from the hw clock
    printf "\nSetting system time from hardware clock\n"
    /sbin/hwclock -s
fi

adding the lines above the “exit 0”. A hat tip goes to AndyD for this from this forum thread.

Back to Top

1 thought on “Accessing the Real-Time Clock from the Raspberry Pi”

  1. Looking at this and your — apparently more recent — https://spellfoundry.com/sleepy-pi/setting-up-the-real-time-clock-on-raspbian-jessie/ and looking for a date… if only a year? Seems there is a mixed history of Raspberry Pi OS, and today we have (on my brand new machine) Raspbian GNU/Linux 11 (Bullseye).
    I do not read of changes in the clock, but am wondering if the instructions for the raspbien-jessie are still appropriate for the new systems?
    (My interest is most directed towards using clock signal to drive a HAT to do data logging.)

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping Cart
Scroll to Top