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

This is an old post!

This post is over 2 years old. Solutions referenced in this article may no longer be valid. Please consider this when utilizing any information referenced here.

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.

Update August 2020

So we had another long power outage this morning and, surprisingly, the feeders did not come back with the correct time. And in doing a postmortem on what happened, I discovered that the system was not reading the hardware clock on boot.

The clue was in this not-too-helpful systemd error message:

System clock time unset or jumped backwards, restoring from recorded timestamp: Thu 2020-08-06 01:13:39 CDT

Turns out there is nothing that actually reads the hwclock back to the system on boot. So I added a systemd unit to do this:

[Unit]
Description=Sync hwclock to system
Before=systemd-timesyncd.service

[Service]
User=root
Type=oneshot
ExecStart=/sbin/hwclock --hctosys

[Install]
WantedBy=multi-user.target

Comments (0)

Interested in why you can't leave comments on my blog? Read the article about why comments are uniquely terrible and need to die. If you are still interested in commenting on this article, feel free to reach out to me directly and/or share it on social media.

Contact Me
Share It
Linux
Here’s a quick article about how to make a multiboot USB stick under macOS. These are useful in a lot of situations - such as for doing system installs or system rescues - because you can boot a wide variety of live OSs from a single stick. There are a lot of guides out there for doing this on Linux, and a lot of software for automating it on Windows, but not a lot of guides for doing it on macOS. Fortunately, it is pretty straightforward as the instructions will be broadly similar to doing it on Linux.
Read More
Home Assistant
My mailbox - yes, my physical mailbox where I receive actual mail - is one of the things that has stubbornly resisted my attempts to automate it. I’ve tried a few different solutions. Third party proprietary chimes. A Z-Wave tilt sensor on the door. But nothing has worked long-term.
Read More
Hardware
So here we are in October. COVID-19 is still with us and I am still working from home. Meanwhile, summer has quickly changed to autumn. The leaves are falling as are the temperatures. My house was the model home for our neighborhood, and what would have been the garage was finished in and used as a sales office. So when we bought the house, I was like, perfect, a perfect spot for an office! But the problem is that, because it was a garage, it’s not connected to the house’s HVAC system. In the summer there is a mini-split that keeps the whole area cool. But it’s kind of loud. However, I do have some of these DeLonghi Oil Radiators to use in the winter which provide abundant, silent heat without using very much power. But the downside is that they take awhile to warm up. Wouldn’t it be cool if I could have them turn on an hour early and “pre-warm” the office? Well, to get the obvious part out of the way, yes, there is timer functionality, but that is not nearly as cool as tying it into the rest of my smart home. But it has a remote. What if I could find a way to use Home Assistant to send IR commands to the heater? Turns out you can!
Read More