06 | I²C Real-Time Clock: DS3231

Verify that the hardware clock keeps running through a power cycle without network time synchronization.

  1. Connect a 3.3 V-compatible DS3231 module to SDA, SCL, 3.3 V, and ground. It can share SDA and SCL with an SSD1306. The DS3231 uses 0x68, while the OLED typically uses 0x3c or 0x3d.
  2. Confirm that i2c-rtc.dtbo exists on the boot partition. Add the following to the active config.txt and reboot.
dtparam=i2c_arm=on
dtoverlay=i2c-rtc,ds3231
  1. Check that Linux registered an RTC and identify the DS3231 by name.
ls -l /dev/rtc*
cat /sys/class/rtc/rtc*/name
  1. Verify the system time and save it to the RTC. The example assumes the DS3231 is /dev/rtc0.
sudo hwclock --rtc=/dev/rtc0 --systohc --utc
sudo hwclock --rtc=/dev/rtc0 --show --utc
  1. Shut down normally, disconnect main power, wait a few minutes, and boot offline. Check hwclock --show to verify that the RTC kept time. Use date separately to check whether the boot process restored the system clock from the RTC.

Driver reading exercise

Follow the DS3231 Device Tree node to the rtc-ds1307 driver and see how the RTC subsystem exposes /dev/rtcN. For Yocto, include the kernel configuration, overlay, and a version of hwclock that supports the options used here.