Using a Real-Time Clock over SPI with the Raspberry Pi 3 – DS3234


I would suggest when following these notes to have a ethernet connection at the beginning of this to get NTP time first.

To get NTP time run the following command:

sudo ntpd -gq

Enable the SPI interface:

sudo raspi-config

Create the following dts file: ds3234-rpi.dts


/ {
    compatible = "brcm,bcm2835";
    fragment@0 {
        target = <&spi0>;
        __overlay__ {
            spidev@0 {
                status = "disabled";
            ds3234@0 {
                compatible = "ds3234";
                reg = ;
                #address-cells = ;
                #size-cells = ;
                spi-max-frequency = ;

Create the Makefile:

all: ds3234-rpi.dtb
ds3234-rpi.dtb: ds3234-rpi.dts
        $(DTC) -@ -I dts -O dtb -o ds3234-Rpi.Dtbo ds3234-rpi.Dts
install-ds3234-rpi.dtbo: ds3234-rpi.dtbo
        cp ds3234-rpi.dtbo /boot/overlays/
install: install-ds3234-rpi.dtbo
        rm *.dtbo

Compile the overlay:

sudo make
sudo make install

Modify /boot/config.txt and include the following:


Modify /lib/udev/hwclock-set by commenting out the following code:

if [ -e /run/systemd/system ] ; then
    exit 0

So it looks like this:

#if [ -e /run/systemd/system ] ; then
#    exit 0

Remove the fake-hwclock:

sudo apt-get remove fake-hwclock

Create a two systemd services –
to set the system time on boot from the RTC and also write time to the RTC on shutdown.


Description=Set time from RTC on startup

ExecStart=/sbin/hwclock -s



Description=Synchronise Hardware Clock to System Clock

ExecStart=/sbin/hwclock --systohc


Enable the services to run:

systemctl enable hwclock-start hwclock-stop

Reboot your Pi and verify the hardware clock time:

sudo hwclock -r

Verify that the rtc_ds3234 module has loaded:

sudo lsmod

Verify systemtime


If all went well your should have a functional hardware clock you can disconnect ethernet and reboot and test if you have system time and hwclock time that match.

Leave a Reply

Your email address will not be published. Required fields are marked *