HCL:Raspberry Pi3 RTC

Jump to: navigation, search

One of the main issues with the Raspberry Pi is the lack of a Real-Time-Clock (RTC) that gives the system the correct time directly after a reboot. Usually the system needs network access to get the correct time via NTP, but without access to an NTP server, during a network outage or stand-alone setup, the system will start with a pre-defined date and time after each reboot.

DS3231

Ds3231.jpg
The solution for this is really simple and also quite cheap. There is a small RTC module with the DS3231 I2C chip that can be directly connected to the GPIO socket.

Mounting the Module

The module gets mounted to the GPIO socket pins 1,3,5,7,9 as shown in the picture below.

DS3231-RaspberryPi3.JPG

Raspberry-pi-pinout-rtc.png

Device Tree (DT) overlay

The RTC module needs to be activated with a Device Tree (DT) overlay. The configuration for this is located at /boot/efi/extraconfig.txt.

dtoverlay=i2c-rtc,ds3231

After a reboot you will see the module with the dmesg output.

[    4.526184] rtc-ds1307 1-0068: rtc core: registered ds3231 as rtc0

hwclock

The time on the RTC module can be checked with the hwclock tool.

#> hwclock --show
2019-01-30 17:29:41.818087+0100

To write the system time that the system got over NTP to the RTC module run --systohc.

#> hwclock --systohc

systemd

To load the time from the RTC via a systemd service, create the file /usr/lib/systemd/system/hwclock-start.service with the following content.

[Unit]
Description=read rtc and write to system clock
After=sysinit.target

[Service]
Type=oneshot
ExecStart=/sbin/hwclock --hctosys --utc

[Install]
WantedBy=basic.target

Enable the service and restart the system.

#> systemctl status hwclock-start