First step is building the hardware.
Here's the scheme of the RTC and some pictures showing the process:
First of all solder the components on the perfboard following the circuit diagram above leaving some space for the 2x20 header, which will be soldered later.
[Click images to see hi-res version]
Now solder the header and connect the +5V, GND, SDA and SDL wires to the header. Optionally you could just connect the four wires to the GPIO without any header.
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.
This what I've got in the end and how it looks stacked on the Raspberry Pi B+
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:
1. 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.
2. 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.
3. Now power off the Pi, plug the RTC board on the GPIO and power it on again.4. 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.
5. 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.
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.
The cleanest solution would be recompiling the kernel with static RTC support. A good article explaining this can be found here at lemoneerlabs.com.
Another possibility is reading the clock and setting time from user space, and that's what I did, inspired by laziness :D
Well, laziness apart, I picked it because I didn't want to interfere with kernel updates and have to recompile it manually in the future.
Here we go:
1. 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 Beforefirstname.lastname@example.org [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-initto enable the service at startup.
2. 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.target3. 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.
4. Eventually run
$ timedatectl set-ntp trueto 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).
I measured the clock drift disabling systemd-timesyncd service for a couple of days. It turned out that the clock has an accuracy of 20 ppm, it lose a couple of seconds each day. It was more than acceptable for me, since my Raspberry is always online and can sync time periodically.