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

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.

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.

COVID-19 has taken the world by storm and left a lot of brokenness in its wake. A lot of people are suffering. If you feel so inclined, please make a donation to your local food bank or medical charity. Order take-out from your local Chinese restaurant. Help buy groceries for an unemployed friend. Help people make it through to the other side.

But if you found this article helpful and you really feel like donating to me specifically, you can do so below.

Read More

petfeedd 0.2 released, with Docker support!

petfeedd, the daemon I wrote for my Raspberry Pi-powered cat feeders has been updated to fix a number of bugs people were seeing attempting to install it since I originally wrote it in 2017. Perhaps the biggest change is Docker support! That’s right, if you just want to run petfeedd, now you can do it in just three commands! No more installing various libraries and things (but that approach still works as well.)