A simple, stacking, Real Time Clock for Raspberry

 

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.

Lately, in my few spare time I tried to play around with the Pi.

Looking for something simple that I could build by myself I decided to add a simple hardware clock. I picked a simple DS1307, although not very precise, for many reasons:

  • it was cheap
  • 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.

I looked around on the Web for a prebuilt DS1307 RTC, like those sold by SparkFun, but the one I wanted was so expensive I would have built one by myself for half the price. And so I did! It was even fun and I finally learned how to properly sold on a perfboard without spilling solder everywhere ;)

Here’s a short but exhaustive guide to build one.

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 $\Omega$ resistors
  • a 2x20 header (or better a stacking header)
  • a piece of perfboard (at least 20x12 holes) and some small wires

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
     device_tree_param=i2c_arm=on
    

    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

     i2c-dev
     rtc-ds1307
    

    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.

I wanted to achieve a behavior similar to a personal computer, where RTC time is loaded immediately at boot by the kernel. Unfortunately Arch Linux ARM for raspberry doesn’t provide Real Time Clock support already built into the Kernel, since a stock RPi does not come with one preinstalled.

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.

Now you have to mess a bit with systemd services:

  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
     Before=netctl-auto@eth0.service
        
     [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.

  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 shutdown.target
     Conflicts=shutdown.target
        
     ...
        
     [Install]
     WantedBy=multi-user.target
    
  3. 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 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).

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.