How to add RTC DS3231 to Raspberry Pi

After you have connected DS3231 to your Raspberry Pi, edit the /boot/config.txt file and add the following lines:


Save the file and exit. Next, install the i2c tools:

# aptitude install -y i2c-tools

Reboot your Rapsberry Pi. Run the following command and note the address of the RTC. In my case that is 0x68:

# i2cdetect -y 1


The position of UU determines the RTC address, 0x68.

Run the following commands:

# modprobe i2c-dev
# modprobe i2c-bmc2808
# modprobe i2c-ds1307

Run the following:

# echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new-device

Query the hardware clock:

# hwclock -r

You will probably get the wrong time, but then add the -s switch

# hwclock -s
# hwclock -r

You will notice that the time is accurate.

Add the RTC kernel module to /etc/modules:


Add the following to /etc/rc.local just above the exit 0 line.

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

If the /etc/rc.local file does not exist, first create the service:

# vim /etc/systemd/system/rc-local.service

Now add your code:

Description=/etc/rc.local Compatibility

ExecStart=/etc/rc.local start


Create the /etc/rc.local file, including the required line:

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device exit 0

Enable the rc-local service:

# systemctl enable rc-local

Remember to set the executable bit on the rc.local

# chmod +x /etc/rc.local

Reboot your server and confirm that the hwclock -r shows the correct time.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.