Using the DS3231 RTC (Real Time Clock) with Raspberry Pi

By · Published · raspberry pi, hardware

In my last post about building the pet feeders, I alluded to one of the limitations of the Raspberry Pi has: it lacks a real time clock. This is an understandable omission. They take up extra space and cost, are not needed for a lot of applications and can be pretty easily added if they are.

One of the limitations I found is that, if there is a power outage that lasts a significant amount of time - long enough for the UPS batteries that keep the wireless up go dead, for instance - that the Raspberry Pi's may "lose" track of time if they can't reconnect to wifi and, thus, sync up by NTP.

So I picked up a pair of these DS3231 RTCs from Amazon for about $5 each. This is how to get them to work on your Raspberry Pi.

  1. Install Raspbian as usual.

  2. Remove the fake-hwclock package. This is not strictly necessary, but probably a good idea. We're installing a real hwclock after all. :)

    sudo apt remove fake-hwclock
    
  3. Edit /boot/config.txt and add the following option to the end of it:

    dtoverlay=i2c-rtc,ds3231
    
  4. Edit /lib/udev/hwclock-set and comment out the following lines:

    #if [ -e /run/systemd/system ] ; then
    #   exit 0
    #fi
    
  5. Power down your Raspberry Pi.

  6. Install the DS3231 over pins 1, 3, 5, 7 and 9. The picture below illustrates the installation.


  7. Power the Raspberry Pi back up.

  8. Type sudo hwclock -r. If you see something similar to:

    2017-12-01 21:39:21.988465+0000
    

    You're good to go. It's now reading time from the hardware clock.

Now, on a clean shutdown, it should automatically write the current system time to the hwclock. But what about an unclean shutdown. Like, perhaps, one that comes as a result of a power outage? Well, what we can do is be sure to sync the system and hardware clocks on a schedule.

  1. Remove the old /etc/cron.hourly/fake-hwclock job.

    sudo rm /etc/cron.hourly/fake-hwclock
    
  2. Add a new hourly cronjob that syncs up the clocks.

    sudo touch /etc/cron.hourly/hwclock
    sudo chmod +x /etc/cron.hourly/hwclock
    sudo vi /etc/cron.hourly/hwclock
    

    And add the following lines to it:

    #!/bin/sh
    /sbin/hwclock --systohc
    

Special thanks to this thread, which covers much of the installation details.

( Comments )

Did something I wrote help you out?

That's great! I don't earn any money from this site - I run no ads, sell no products and participate in no affiliate programs. I do this solely because it's fun; I enjoy writing and sharing what I learn.

All the same, if you found this article helpful and want to show your appreciation, here's my Amazon.com wishlist.


comments powered by Disqus