Skip to main content

Real time clock (RTC) module

Enable i2c on the Raspberry Pi.

Attach the RTC Module to the Pi board:

XC9044-rtc-clock-module-for-raspberry-pi

Ds3231_module

RP2_Pinout_tc-2

Install i2c-tools and reboot:

sudo apt install -y i2c-tools
sudo reboot

Run the following (for Model B)

#sudo i2cdetect -y 0 # for Model A
sudo i2cdetect -y 1 # for Model B

and 0x68 should be listed:

     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: -- -- -- -- -- -- -- --

Instantiate the DS1307

#echo "ds1307 0x68" | sudo tee -a /sys/class/i2c-adapter/i2c-0/new_device # for Model A
echo "ds1307 0x68" | sudo tee -a /sys/class/i2c-adapter/i2c-1/new_device # for Model B

Ensure the system time is set correctly.

Initialise the hardware clock:

sudo hwclock --systohc -D --noadjfile --utc --verbose

which should output something similar to this:

hwclock from util-linux 2.36.1
System Time: 1681005153.491771
Trying to open: /dev/rtc0
Using the rtc interface to the clock.
Assuming hardware clock is kept in UTC time.
RTC type: 'rtc-ds1307'
Using delay: 0.000000 seconds
missed it - 1681005153.494080 is too far past 1681005153.000000 (0.494080 > 0.001000)
1681005154.000000 is close enough to 1681005154.000000 (0.000000 < 0.002000)
Set RTC to 1681005154 (1681005153 + 1; refsystime = 1681005153.000000)
Setting Hardware Clock to 01:52:34 = 1681005154 seconds since 1969
ioctl(RTC_SET_TIME) was successful.

To verify the hardware clock, run the following

sudo hwclock -r

which should display the time and date, e.g.

2023-04-09 11:53:33.595627+10:00

Configure the Raspberry Pi to synchronise the hardware clock on start up,

by inserting one of the following lines before exit 0 in the file /etc/rc.local

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device # for Model A
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device # for Model B

i.e.

sudo sed -i '/exit 0/i \\necho ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device' /etc/rc.local

followed by sudo hwclock -s

sudo sed -i '/exit 0/i \\nsudo hwclock -s\n' /etc/rc.local 

Setup complete.