Skip to content
Advertisements

Should You Add a Real-Time Clock to a Raspberry Pi

The Raspberry Pi enables us, tinkerers, to complete (or attempt to complete) so many different projects and one question I see come up a lot, is “Should I add a Real-Time Clock to my Raspberry Pi?” Well, the answer to that question is complex and depends a lot on what exactly your project is. That being said Real-Time Clock addon boards for the Raspberry Pi is relatively inexpensive and is not going to hurt anything so if you think you need one or just want one you should add one.

The way the Raspberry Pi works out of the box is that every time it boots it connects to the Network Time Protocol Server and essentially asks it what time it is? Personal computers work the same way but they also have a Real-Time Clock built in with a small battery allowing it to keep time even when powered off. Depending on what exactly you are doing with your Raspberry Pi, having the reach out to the Network Time Protocol Server on boot to get the time may not be an issue but, what if your Raspberry Pi is not connected to the internet? or what if you are using your Raspberry Pi in a security project where you can not afford either the latency of going to the Network Time Protocol Server or the possibility of a network connection issue causing time interruptions and/or inaccuracies?

If you have an application for your Raspberry Pi where time is sensitive then a Real-Time Clock is probably right for you. Real-Time Clocks are small and can plug directly into your Raspberry Pi GPIO and use I2C to communicate making setup and use easy. If you want to add a Real-Time Clock to your Raspberry Pi simply follow the instructions below:

  • With your Raspberry Pi Turned off, plug in the Real-Time Clock
  • Turn on your Raspberry Pi
  • Launch Terminal and run:
    • sudo apt-get install python-smbus i2c-tools
  • From your Terminal command line run the following command and verify the ID of your Real-Time Clock is showing up:rtc-1
    • sudo i2cdetect -y 1
  • Next, we need to set up the Real-Time Clock Time by adding a line to our config.txt file. To do this:
    • from Terminal runsudo nano /boot/config.txt
    • Then we need to add the appropriate code (the one that matches your Teal-Time Clock chip) to the end of the config.txt file. Use one of the following:rtc-2
      • dtoverlay=i2c-rtc,ds1307
      • dtoverlay=i2c-rtc,ds1307
      • dtoverlay=i2c-rtc,ds3231
    • Save your config.txt by pressing
      •  ctrl+x1
  • Next reboot your Raspberry Pi by running
    • sudo reboot
  • With the Rasberry Pi rebooted we need to disable the “fake hardware clock” by running the following commands
    • sudo apt-get -y remove fake-hwclock
    • sudo update-rc.d -f fake-hwclock remove
    • sudo systemctl disable fake-hwclock
  • With the “fake hardware clock” disabled we need to start the real “hardware clock” which simply requires us to comment out a few lines of code. From Terminal run
    • sudo nano /lib/udev/hwclock-set
      • Comment out the following lines
        • #if [ -e /run/systemd/system ] ; then
        • # exit 0
        • #fi
  • Now all that is left to do is to set the correct time. The easiest way to do this is to connect your Raspberry Pi to the internet and let it get the time from the Network Time Protocol Server the write that time to the Real-Time Clock. to do this once yourrtc-3 Raspberry Pi has the correct time simple run the following command from Terminal
    • sudo hwclock -w

That’s it, your Real-Time Clock is completely set up on your Raspberry Pi and now your Raspberry Pi can keep accurate time on its own without having to be connected to the internet.

This brings us back to the question “Should you add a Real-Time Clock” to your Raspberry Pi and the answer is still the same, that depends on your application but again for the price of a Real-Time Clock there really is really no reason to not add one.

Are you using a Real-Time Clock on your Raspberry Pi? What is your application? Let me know in the comments below.

Advertisements

Mike View All

I am passionate about the IoT and connected devices. Using connectivity to automate our lives will empower civilization to achieve greatness.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: