Jul 28

Using a DS3231 I2C Real Time Clock (RTC) with a Raspberry Pi (Updated for Jessie)

Unlike most computers the Raspberry Pi doesn't contain a Real Time Clock (RTC), we assume it was left out of the design to save space and/or money. A RTC is a coin cell battery powered clock which keeps accurate time, even when the computer is powered off. Without one, the Raspberry Pi isn't capable of keeping time by itself.

If you're using a Pi 3 with an Internet connection then you can get the time from the Internet using Network Time Protocol (NTP), however if don't have a network connection, then you'll need an RTC to keep time.

We've found a simple to use RTC module for the Raspberry Pi with the Dallas DS3231 I2C RTC chip which is also a bunch more accurate than the commonly used DS1307 chip. It's also tiny, and fits right on the Pi's GPIO header (seen top left of the image). The image is of an older Raspberry Pi but the RTC modules works just fine on the Pi 3, Pi 2, B+ and original Model B

Raspberry Pi RTC Module installed

Here's the basic steps you need to get the Pi RTC Module going on the latest Raspbian Jessie distribution you can download from the Raspberry Pi Foundation's website.

In raspi-config, go to "9 Advanced Options" / "A7 I2C" and enable it including loading the module:

sudo raspi-config

Edit the file /lib/udev/hwclock-set

sudo nano /boot/config.txt

Comment out the following three lines:

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

to make them look like this:

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

Check the system date/time is correct

date

Finally, set the hardware clock to the correct time

sudo hwclock -w

That's it! Things have become easier than our earlier instructions due to changes in the Raspbian operating system.

1 response to "Using a DS3231 I2C Real Time Clock (RTC) with a Raspberry Pi (Updated for Jessie)"

  1. Hi all,
    Could quite get this working based upon the notes. Give away was the different file references for editing.
    After some "Google Foo", I found https://github.com/weewx/weewx/wiki/pi-RTC-with-raspbian-jessie

    Then I edited '/boot/config.txt' and added 'dtoverlay=i2c-rtc,ds3231', saved. Then a reboot and behold a working RTC.

    Hopes this helps someone.
    Cheers, Andrew

Leave a comment