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.

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

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.)

Rob's Raspberry Pi Powered Pet Feeders

Or, how to massively over-engineer dumping cat food into a bowl. As with many of my projects, it started with something that made me angry. In this case, it was this: The Petmate Le Bistro Pet Feeder. Okay, let’s back up a little bit. Back to about 8 or so years ago. We had a cat at the time, Pumpkin, who as objectively not a good cat. She was foul tempered on the best of days and very difficult to love. But she was my wife and I’s first pet, so we did love her all the same. She had a habit of wanting food precisely on time. And if it was late, she would raise all manner of noise until she was fed. Often this came at some ungodly early time in the morning. So I bought a Petmate Le Bistro Pet Feeder.