Real Time Clock¶
Introduction¶
Raspberry Pis do not include a Real Time Clock (RTC) unlike most computers. This means that Raspberry Pis have to rely on an external source to get the current time. Most of the time this is done using the Network Time Protocol (NTP) via the Internet. Without access to an external time source, the last known time is used instead - this is saved when the Raspberry Pi is shut down and then loaded at the next boot time.
By adding a RTC module to the Raspberry Pi it can keep time when powered off and when it does not have an Internet connection. When the Raspberry Pi boots it can use the time from the RTC rather than the last saved time.
With an accurate time in place it means we can use DNSSEC with Dnsmasq.
Note
If you enable DNSSEC in dnsmasq
without an accurate time source in place, your DNS lookups for the Arch Linux NTP servers will fail to validate as they use DNSSEC. Your Raspberry Pi will therefore be unable to set its time.
RTC module¶
The DS3231 RTC module from Hobby Components can be used with the Raspberry Pi. It is very cheap and easy to install. Simply place the module over pins 1, 3, 5, 7, 9 (the vertical row of pins at the top left).

Configuration¶
After the module has been installed add to /boot/config.txt
:
device_tree_param=i2c_arm=on
Edit /etc/modules-load.d/raspberrypi.conf
and add:
rtc-ds1307
Save and reboot.
Once rebooted, install i2c-tools
and use it to check that the module is working:
pacman -S i2c-tools
modprobe i2c-dev
i2cdetect -y 1
After running i2cdetect
above, you should see the following output:
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 can remove the i2c-dev
module:
rmmod i2c-dev
Create RTC device so we can set it to the current date/time:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
Now hwclock
should work:
hwclock
Output:
2000-01-01 00:05:23.942725+00:00
Date is not correct, so write the current time to the RTC:
hwclock -w
You can then re-run the hwclock
command to verify that the time has been written.
Systemd service¶
To tell the Raspberry Pi to read the current time from the RTC module at boot time we can create a systemd service to do this for us.
Create the following script: /usr/lib/systemd/scripts/create-rtc-device
#!/bin/bash
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s
Set permissions:
chmod 700 /usr/lib/systemd/scripts/create-rtc-device
Create: /etc/systemd/system/rtc.service
with the following contents:
[Unit]
Description=Real Time Clock (RTC)
Before=network.target
[Service]
ExecStart=/usr/lib/systemd/scripts/create-rtc-device
Type=oneshot
[Install]
WantedBy=multi-user.target
This rtc.service
will execute /usr/lib/systemd/scripts/create-rtc-device
at boot time.
Now start and enable service:
systemctl start rtc
systemctl enable rtc