Close
0%
0%

An elegant stacking Real Time Clock for Raspberry

Looking for a simple Real Time Clock module to stack on your Raspberry Pi? Check out this project explaining how to build and install it!

Similar projects worth following
It's a full stacking shield you can elegantly install on top the RPi leaving the GPIO entirely exposed for other devices. This project covers both the hardware and the software part. You can build it for no more than 8$.

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 http://hackaday.io 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.

  • 1 × DS1307 Chip
  • 1 × IC 2x4 Socket
  • 1 × 32.768 kHz Crystal Load-capacitance 12.5 pF
  • 1 × CR2032 Battery and a Battery holder
  • 2 × 1000 Ω Resistors

View all 7 components

  • 1
    Step 1

    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+

  • 2
    Step 2

    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.

  • 3
    Step 3

    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
    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 sysinit.target shutdown.target
    Before=time-sync.target shutdown.target
    Conflicts=shutdown.target
    
    ...
    
    [Install]
    #WantedBy=sysinit.target
    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.

View all 3 instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates