Adding a DS3231 Real Time Clock to the Raspberry Pi 3

ds3231-rtcSince the Raspberry Pi 3 doesn’t come with a battery-powered real time clock, it will only show the correct time once it has Internet connectivity (thanks to the NTP daemon). If the Raspberry Pi 3 is not connected to the Internet, you might want to add a hardware clock to set the current date. Here’s how to add a DS3231 real time clock GPIO module to the Raspberry Pi 3 in Raspbian Jessy Lite:

  1. Get a DS3231 real time clock module and install it on the GPIO header of the Raspberry Pi 3 on pin 1
  2. Add the following line at the end of /boot/config.txt in Raspbian Jessy:
  3. We don’t need fake-hwclock anymore:
    apt-get purge fake-hwclock
  4. Check/set the current system time and write the system time to the RTC module using:
    hwclock -w
  5. Set the correct time zone using:
    dpkg-reconfigure tzdata
  6. Edit /etc/rc.local and add the hwclock command above the line that says “exit 0”:
    /sbin/hwclock -s
  7. The /etc/init.d/ shell scripts tends to corrupt this RTC clock module. In my case, the RTC clock was set to 2066/01/01 after every reboot. To prevent this from happening, edit /etc/default/hwclock and set HWCLOCKACCESS to no:
  8. Reboot
  9. Done! Raspbian will now set the time from the RTC clock during boot even if there is no Internet connectivity available.
  10. If RTC corruption is still happening, you may have to get rid of the NTP daemon as well using:
    apt-get purge ntp
    apt-get install ntpdate
  11. After the NTP daemon has been removed, you can still sync the system clock using ntpdate-debian which you might add to /etc/rc.local as well (after the hwclock command though) – just in case there is an Internet connection available during boot. And/or add it to /etc/cron.daily for example.

Raspbian Jessy Lite will detect the DS3231 real time clock module automatically (as a DS1307 module but nevermind), there’s no need to whitelist or blacklist any I2C modules. There’s no need to run the i2cdetect command from the i2c-tools package. Once the clock module is detected, this line should be visible using dmesg:

# dmesg | grep rtc
[    6.640799] rtc-ds1307 1-0068: rtc core: registered ds3231 as rtc0

Check /proc/driver/rtc for more data on the RTC:

# cat /proc/driver/rtc
rtc_time : 19:26:18
rtc_date : 2016-03-25
alrm_time : 00:00:00
alrm_date : 1970-01-01
alarm_IRQ : no
alrm_pending : no
update IRQ enabled : no
periodic IRQ enabled : no
periodic IRQ frequency : 1
max user IRQ frequency : 64
24hr : yes

19 replies on “Adding a DS3231 Real Time Clock to the Raspberry Pi 3”

  1. /sbin/hwclock -s sets the system time to UTC !.

    Despite my local timezone set correctly.

    How do we set the time to local time, i.e how do we apply our timezone to UTC ?

  2. Finally, a set of commands that gets RPi Zero W to set system clock at boot time, even if Wifi is not available. Thank you!

  3. Hello.
    My RTC seems to work fine but one thing is not clear to me. Why do I get someting like this in logs during the boot:

    Mar 28 14:05:37 raspberrypi syslog-ng[577]: syslog-ng shutting down; version=’3.5.6′
    Mar 28 14:06:27 raspberrypi syslog-ng[576]: syslog-ng starting up; version=’3.5.6′
    Jan 1 01:00:05 raspberrypi mtp-probe: checking bus 1, device 5: “/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.5”
    Jan 1 01:00:05 raspberrypi mtp-probe: bus: 1, device: 5 was not an MTP device
    Jan 1 01:00:05 raspberrypi mtp-probe: checking bus 1, device 3: “/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.1”
    Jan 1 01:00:05 raspberrypi mtp-probe: checking bus 1, device 4: “/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3”
    Jan 1 01:00:05 raspberrypi mtp-probe: bus: 1, device: 4 was not an MTP device
    Jan 1 01:00:05 raspberrypi mtp-probe: bus: 1, device: 3 was not an MTP device
    Mar 28 14:06:23 raspberrypi wpa_action: WPA_IFACE=wlan0 WPA_ACTION=CONNECTED
    Mar 28 14:06:23 raspberrypi wpa_action: WPA_ID=1 WPA_ID_STR=WIFI WPA_CTRL_DIR=/var/run/wpa_supplicant

    “Jan 1” is present only when mtp-probe. Every other event in logs has correct time. Is it normal or should I worry about that?

  4. I have followed the above and have some extra tweaks to add. I wanted to create a system that would:
    1) Work normally if the Internet was available and access to an NTP server was permitted.
    2) Use the DS3231 (RTC) if no NTP server was accessible.
    3) If the system booted without Internet access the RTC would be used initially. If the Internet connection became available later, the system clock would be updated to NTP time.
    4) At poweroff/reboot/halt update the RTC from the system clock.

    This is what I did…

    1) As per step 2 add dtoverlay=i2c-rtc,ds3231 to /boot/config.txt
    2) As per step 3 remove fake-hwclock
    3) I had already set the timezone but step 5 is executed here.
    4) As per step 6 add /sbin/hwclock -s to /etc/rc.local
    5) For step 7 – the line HWCLOCKACCESS=no in /etc/default/hwclock was commented out and I did not change that.
    6) I did not remove ntp or ntpupdate as per step 10.
    7) I did remove fake-hwclock using apt-get remove fake-hwclock
    8) My clock device was /dev/rtc0 (rtc zero)
    9) running “cat /proc/driver/rtc” is useful as it shows the current hardware (RTC) time .
    10) The posting from “genekh” regarding 2066-01-01 was followed BUT with one important change; The text -rtc-$dev was removed. When debugging I found the $dev variable was not set which meant the rtc was not found. As the default clock is /dev/rtc0 the command runs as it should.
    11) At this point the system was almost perfect but, when rebooting with no internet connection , it was clear that the RTC was not being updated from system time.
    12) The final fix! There is already a systemd task for updating the RTC from the system clock during power-off or rebooting; it is called hwclock-save.service and is located in /lib/systemd/system . In my case it did not run because there was a condition that was not met namely: “ConditionFileIsExecutable=!/usr/sbin/ntpd” . Basically this says that ntpd should not be present for the service to run. As I wanted ntpd as a “parallel” service then I just prep-ended a # character to the line to comment it out.
    13) You should make sure the is enabled by running:
    “sudo systemctl enable hwclock-save.service”
    13) Now, when the system is shutdown, halted or rebooted, the hardware RTC is updated from the system clock.

    The end result is a system clock that works reliably whether I have Internet access or not. If your system is never going to have Internet or ntp access then you can uninstall ntp.

    Hopefully this sorts out this dragon once and for all…

  5. Hi ,
    I tried so many tutorials but at last, i found this to be working.Iam using Pi3 , DS3231 and Raspian Pixel OS.

    I have one query what i need to change if i am using DS1307(0x68) and PCF8563 (0x51) rtc modules ??

  6. Thanks for this guide!
    The rtc I was using kept resetting to 1-1-1970. It works ok for now after having followed the the guide above. Also , I used date -s command to set the local time and date accurately and then wrote it to the rtc.

    One query I had was in /proc/driver/rtc should the rtc_time show universal time (UTC) or my local time?

  7. Does this board as pin INT/SQW wired ?

    Any setup instructions on how to set up PPS on this pin ?

  8. Thank you so much! I tried so many tutorials, getting confused between the versions with or without Device Tree, but couldn’t get it to work.
    But your guide is so simple and by following it I finally got my Pi to remember the date and time.

  9. Hello,
    I have 2 questions:

    1) Why do you install “ntpdate” ?
    I was thinking that we should purge it instead of installing it !
    (sudo apt-get purge ntpdate)

    2) Can you give me more details about “HWCLOCKACCESS=no”
    What is this option currently doing ?

    Quickly, it seems like we are telling the RPI to NOT use the Hardware clock!


    1. Question 1: We REMOVE ntp (the daemon that automatically updates the time), but INSTALL ntpdate (which can be executed manually, so we can still update our time via NTP). If you don’t want to ever use NTP, you don’t need to install it.

      Question 2: I don’t quite know, but I think this is just some script that we don’t need, but was there before. We just tell this script not to access the RTC module, not the entire OS.

  10. coul you send me step by step instr how to install a rtc on my raspberry pi 2.
    Im also new too sudo config etc.
    I would be very gratefull

  11. Hello – I spent ages trying to get the DS3231 to work and just about gave up – being totally ignorant I ignored this set of instructions because it failed at step 4, after many more failures I came back to this post and realised that step 4 should read hwclock -r is wrong and it should be hwclock -w – after that eureka all worked great

  12. I too went through the same guide and had the same issue with 2066-01-01.

    The solution is to modify the file /lib/udev/hwclock-set like this
    if [ yes = “$BADYEAR” ] ; then
    #/sbin/hwclock –rtc=$dev –systz –badyear
    /sbin/hwclock –rtc=$dev –hctosys –badyear
    #/sbin/hwclock –rtc=$dev –systz
    /sbin/hwclock –rtc=$dev –hctosys

    Comment out the lines with –systz, put –hctosys instead

    No need to modify /etc/default/hwclock
    No need to uninstall ntp

    I hope the guide can be updated to include that, as it a known debian issue and is the recommended solution fo hwclock corruption

  13. Hello. I am new to raspberry pi3. New to Linux. I have tried the above but when use sudo hwckock -r get …..registers contain invalid or…range we can handle.
    Sudo nano/proc/driver/rtc shows rtc-date of 2066-01-01
    Any assistance would be appteciated

Comments are closed.