Intro: An Elegant, Stacking, Real Time Clock for Raspberry Pi
If you want to build a good Real Time Clock for your Raspberry here's the tutorial you were looking for!
This is a revised and simplified version for Instructables made out of this article at my personal blog. Difference is mainly in the structure and my article is a bit more verbose.
Why another "annoying" how to on building a Real Time Clock for the RPi?
Because almost everywhere you find instructions telling you to build the circuit on a breadboard and simply wire it with a couple of loose wires.
This is a full Stacking Shield you can elegantly install on top the RPi leaving the GPIO entirely exposed for other devices. And you can build it for no more than 8$, instead of buying one for double the price.
NOTE: The RTC module is compatible with Raspberry A+ / B+ / 2.
This tutorial was originally written for a Raspberry B+ running Arch Linux ARM, however you should be able to adapt it to other configurations with few or even no changes.
I picked a simple DS1307, although not very precise, for many reasons:
- it was cheap
- my RPi is always connected to the internet, therefore can sync time periodically
- required a simple circuitry and few components
- it could be easily connected through a I2C interface
- Arch Linux ARM already has a kernel module compiled for it.
Anyway drift should be around 20ppm, a couple of seconds per day. Check with your requirements if it's accurate enough.
For this project you need:
- 1x DS1307 chip
- 1x IC 2x4 socket
- 1x 32.768 kHz crystal (Load-capacitance 12.5 pF)
- 1x CR2032 battery and a battery holder - 2x 1000 Ω resistors
- a 2x20 header (or better a stacking header)
- a piece of perfboard (at least 20x12 holes, better double side) and some small wires
Step 1: Solder the Components on the the Hardware
Above you can find the scheme of the RTC and some pictures showing the process.
I suggest you adopt a layout similar to mine, minimizing the board dimensions and allowing the RTC to fit in a RPi case if you have one.
You have to solder all the components on the perfboard following the circuit diagram above and leaving some space for the 2x20 header, which will be soldered last.
Since the I2C inteface supports multiple devices and I'll probably
need to stack something else on the Pi I decided to install a stacking header, like these found @ adafruit.com.
If you are planning to stack something else on top of the Pi pick a stacking header instead of a simple one. Please note that soldering the four wires to the stacking header will be a bit tricky. Using a double side perfboard would probably make things easier.
I used 4 1x10 stacking headers (usually sold for arduino projects) and arranged them into a 2x20, since I couldn't find a cheap 2x20. I also cut two 2x20 pieces of perfboard and used them to create a solid base for the header. I soldered the wires to their pins and eventually fixed everything with some hot glue, making the board rock solid.
Look at the pictures for a better description. You may also find by yourself a good way to solder the header.
Step 2: Plug the RTC Onto the Pi and Test It
Good, it's now time to connect the Real Time Clock to the raspberry and see what happens.
This is the procedure I've followed, using root privileges:
- With the Pi running edit /boot/config.txt adding or uncommenting
thus enabling I2C interface. This is required since Kernel v. 3.18 (~ Jan 2015) which introduced Device Tree support. Read this article @ raspberrypi.org for further infos.
- Create a file /etc/modules-load.d/i2c-rtc.conf containing
Although point 1. should be enough I also added the i2c-dev module explicitly.
- Now power off the Pi, plug the RTC board on the GPIO and power it on again.
- Typing a couple of lines in the shell will reveal if the hard work done up to this moment was worth it ;)
$ echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device $ hwclock -r
If everything goes fine the Raspberry should be able to access the RTC through the I2C interface and read the time stored on it. It should be set around year 2000 or even before.
- If you've made it so far, congratulations!
Now the hardware clock (RTC) should be synchronized with system clock. If the Pi was connected to the Internet since boot, it should be as easy as running hwclock -w.
Else read the man page to find out how to set it manually.
Step 3: Bonus Tip - Sync and Load Time at Boot With Systemd
If you want your system to behave like an usual personal computer, loading time from the hardware clock at boot and keeping it updated periodically with Internet time, I've also written a few steps to set up the proper systemd services.
Instructions will definitely look better at my website, where code is properly formatted and I could provide syntax highlighting.
- Create and enable rtc-init systemd service: write in /etc/systemd/system/ a rtc-init.service file containing the following lines
[Unit] Description=RTC Clock Setup and Time Sync Before=<a href="mailto:firstname.lastname@example.org" rel="nofollow">email@example.com</a> [Service] Type=oneshot ExecStart=/usr/lib/systemd/scripts/rtc-setup [Install] WantedBy=multi-user.target
Then add in /usr/lib/systemd/scripts/ an rtc-setup shell script like this
#!/bin/sh echo ds1307 0x68 >/sys/bus/i2c/devices/i2c-1/new_device echo "RTC DS1307 Installed" hwclock -s echo "System Time synced with RTC Time"
and give it right permissions with chmod 755 rtc-setup.
Run systemctl enable rtc-init to enable the service at startup.
- Move to /usr/lib/systemd/system/ and make a backup copy of systemd-timesyncd with cp systemd-timesyncd systemd-timesyncd.original, then edit systemd-timesyncd as shown below
[Unit] ... After=systemd-remount-fs.service systemd-tmpfiles-setup.service systemd-sysusers.service rtc-init.service #Before=time-sync.target sysinit.target shutdown.target Before=time-sync.target shutdown.target Conflicts=shutdown.target ... [Install] #WantedBy=sysinit.target WantedBy=multi-user.target
- Edit /etc/netctl/eth0 (file name may vary based on Ethernet interface name) commenting out ExecUpPost line:
Description='A basic dhcp ethernet connection' Interface=eth0 Connection=ethernet IP=dhcp #ExecUpPost='/usr/bin/ntpd -gq || true' ## for DHCPv6 #IP6=dhcp ## for IPv6 autoconfiguration #IP6=stateless
This disables automatic time sync with ntpd (at network startup during boot), since it's already performed by systemd-timesyncd service.
- Eventually run $ timedatectl set-ntp true to enable the edited version of systemd-timesyncd service
Reboot and enjoy, your Pi should now load the rtc time at startup (in a few seconds). Then, if you are connected to the internet the systemd-timesyncd service fetches current time with Simple NTP protocol and syncs it with system time and hardware time (your RTC).