Adding RTC Clock to Raspberry Pi 4
Setting up I2C
Enable I2C
Add or uncomment dtparam=i2c_arm=on
line in /boot/config.txt
.
Add i2c-dev
line to /etc/modules
.
Reboot Raspberry Pi.
Check if RTC Module is Connected to I2C
$ i2cdetect -y 1
If, at this stage instead of 68
, the I2C address is UU
(Unbound/Unknown)
then the I2C device has been detected but not working correctly.
Setup RTC Clock
Enable kernel module:
# modprobe rtc-ds1307
Run the following command to add I2C RTC device:
# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
After above command, i2cdetect -y 1
will no longer have access to I2C address
and display UU
instead of RTC Clock:
Check the time on the RTC device:
$ hwclock --show
Check the system clock time:
# date
# # or
# timedatectl
Write the system time to the RTC board:
# hwclock --systohc
Auto-setup RTC on every boot
Add the following line to the end of /etc/modules
:
rtc-ds1307
Add the following lines to /etc/rc.local
:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
hwclock -s
date
Setting system and RTC time
To test RTC, disable automatic system clock synchronisation:
# systemctl disable systemd-timesync
To manually set the system clock and time zone via timedatectl
:
$ timedatectl set-time "<YYYY>-<MM>-<DD> <HH>:<MM>:<SS>"
$ set-timezone <TIMEZONE>
To list possible <TIMEZONE>
values:
$ timedatectl list-timezones
To manually set the system clock time via date
:
# date MMDDhhmm[[CC]YY][.ss]
# # Where:
# # MM: Month (01 to 12)
# # DD: Day (01 to 31)
# # hh: Hour (00 to 23)
# # mm: Minute (00 to 59)
# # CC: Century (optional)
# # YY: Year (00 to 99)
# # .ss: Seconds (optional)
To manually run auto-set the system clock time from NTP
:
# apt install ntpdate
# ntpdate pool.ntp.org
RTC debugging hints
Run the following command as root to remove I2C RTC device:
# echo 0x68 > /sys/class/i2c-adapter/i2c-1/delete_device
Check RTC module kernel messages:
$ dmesg | grep ds1307