Overview

This tutorial requires a Raspberry Pi running a kernel with the RTC module and DS1307 module included. Current Raspbian distros have this, but others may not!
The Raspberry Pi is designed to be an ultra-low cost computer, so a lot of things we are used to on a computer have been left out. For example, your laptop and computer have a little coin-battery-powered 'Real Time Clock'  (RTC) module, which keeps time even when the power is off, or the battery removed. To keep costs low and the size small, an RTC is not included with the Raspberry Pi. Instead, the Pi is intended to be connected to the Internet via Ethernet or WiFi, updating the time automatically from the global ntp (nework time protocol) servers

For stand-alone projects with no network connection, you will not be able to keep the time when the power goes out. So in this project we will show you how to add a low cost battery-backed RTC to your Pi to keep time!

Wiring the RTC

To keep costs low, the Raspberry Pi does not include a Real Time Clock module. Instead, users are expected to have it always connected to WiFi or Ethernet and keep time by checking the network. Since we want to include an external module, we'll have to wire one up. We'll go with the easy-to-use and low-cost DS1307. To make the job really easy, we'll use the the Adafruit DS1307 RTC Breakout Board Kit - it comes with all the parts you need, just add a coin battery!

The Kit does require a little light soldering. In theory you could use all the parts and build them onto a breadboard, but the coin holder is a little difficult since its not breadboard-friendly, so please go ahead and build the kit.

When building the kit, leave out the 2.2KΩ resistors - by leaving them out, we force the RTC to communicate at 3.3V instead of 5V, which is better for the Pi!

The diagrams below show the 2.2KΩ resistors in place - but please remove them either by not soldering them in or clipping them out if you did solder them in! This way, we'll use the Pi's 1.8K pull-up resistors to 3.3V

Wiring is simple:

  1. Connect VCC on the breakout board to the 5.0V pin of the Pi
  2. Connect GND on the breakout board to the GND pin of the Pi
  3. Connect SDA on the breakout board to the SDA pin of the Pi
  4. Connect SCL on the breakout board to the SCL pin of the Pi


DONT FORGET TO POWER IT FROM 5V - THIS IS FINE AND SAFE FOR THE PI - JUST MAKE SURE TO REMOVE THE 2.2K PULLUPS AS MENTIONED IN THE LARGE RED BOX UP ABOVE TO KEEP THE I2C PINS AT 3.3V LOGIC LEVELS

Set up I2C on your Pi

You'll also need to set up i2c on your Pi, to do so, check out our tutorial on Raspberry Pi i2c setup and testing at http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c

Reboot once you've done that with sudo reboot

Verify Wiring (I2C scan)

Verify your wiring by running sudo i2cdetect -y 1 at the command line, you should see ID #68 show up - that's the address of the DS1307!

If you have an older Pi 1, you will have to run sudo i2cdetect -y 0 as the I2C bus address changed from 0 to 1

Once you have the Kernel driver running, i2cdetect will skip over 0x68 and display UU instead, this means its working!

Set RTC Time

Now that we have the module wired up and verified that you can see the module with i2cdetect, we can set up the module.

Raspbian Jessie (Systemd)

Thanks to kd8twg for the hints!

You can add support for the RTC by adding a device tree overlay. Run

sudo nano /boot/config.txt

to edit the pi configuration and add

dtoverlay=i2c-rtc,ds1307

to the end

Save it and run sudo reboot to start again. Log in and run sudo i2cdetect -y 1 to see the UU show up where 0x68 should be

Disable the "fake hwclock" which interferes with the 'real' hwclock

sudo apt-get remove fake-hwclock

sudo update-rc.d -f fake-hwclock remove

Start by editing a new service configuration

sudo nano /etc/conf.d/rtc-i2c

And paste in the following, then save:

# /etc/conf.d/rtc-i2c
CHIP="ds1307"
ADDRESS="0x68"
BUS="1"

Now create a new service file that will start/stop the real time clock manager

sudo nano /lib/systemd/system/rtc-i2c.service

# /lib/systemd/system/rtc-i2c.service

[Unit]
Description=Initialize i2c hardware RTC device driver
DefaultDependencies=no
Requires=systemd-modules-load.service
After=systemd-modules-load.service
Before=sysvinit.target
ConditionPathExists=/sys/class/i2c-adapter
Conflicts=shutdown.target

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/etc/conf.d/rtc-i2c
ExecStart=/bin/sh -c "echo ${CHIP} ${ADDRESS} > /sys/class/i2c-adapter/i2c-${BUS}/new_device"

[Install]
WantedBy=sysinit.target

Enable the new service, so that it will always start with the Pi when it boots!

sudo systemctl enable rtc-i2c.service

That's it! Next time you boot the time will automatically be synced from the RTC module

Raspbian Wheezy or other pre-systemd Linux

 

First, load up the RTC module by running

sudo modprobe i2c-bcm2708
sudo modprobe i2c-dev
sudo modprobe rtc-ds1307

Then, as root (type in sudo bash) run

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

If you happen to have an old Rev 1 Pi, type in

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device

You can then type in exit to drop out of the root shell.

Then check the time with sudo hwclock -r which will read the time from the DS1307 module. If this is the first time the module has been used, it will report back Jan 1 2000, and you'll need to set the time

First you'll need to get the right time set on the Pi, the easiest way is to connect it up to Ethernet or Wifi - it will automatically set the time from the network. Once the time is correct (check with the date command), run sudo hwclock -w to write the system time to the RTC

You can then verify it with sudo hwclock -r

Next, you'll want to add the RTC kernel module to the /etc/modules list, so its loaded when the machine boots. Run sudo nano /etc/modules and add rtc-ds1307 at the end of the file (the image below says rtc-1307 but its a typo)

Older pre-Jessie raspbian is a little different. First up, you'll want to create the DS1307 device creation at boot, edit /etc/rc.local by running

sudo nano /etc/rc.local

and add:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device (for v1 raspberry pi)
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device (for v2 raspberry pi)
sudo hwclock -s (both versions)

before exit 0 (we forgot the hwclock -s part in the screenshot below)

That's it! Next time you boot the time will automatically be synced from the RTC module