Add to Raspberry Pi a DS1307 RTC clock on the I2C bus

Warning : This article has been automatically translated by Bing Translate

For those wishing to use their IP Raspberry in a mobile project or even that cannot connect to Internet, This article will allow you to keep the date and time for the system to update after each reboot.

Raspberry Pi Home Server - Ajouter une horloge RTC DS1307 sur le bus I2C

For reasons that appear before any economic, It was decided at the time of the design of Raspberry Pi, not him integrate real time clock (RTC so). It is this component that allows when you shut down your computer to give the exact time on the next restart. You know the battery which is located on your motherboard ? This is it !

As I'm working on a mobile project, so, I got this problem. No process NTPd to get back the system via Internet. So how do ?

We must start by buying the small component that will plug into the Raspberry. I bought it here for the sum of 2.30$ including shipping costs (a bit more expensive here but faster delivery). Here is what I got :

Raspberry Pi Home Server - Ajouter une horloge RTC DS1307 sur le bus I2C

There may be better and/or components suited to Raspberry Pi connector (see this article that contains an excellent comparison) but it seemed to me a very good compromise. First the low price, then the use of the I2C bus therefore need to 50 GPIO (Yes I am exaggerating a little) and finally the really tiny size.

Once Assembly, It is necessary to connect our CTS to the Raspberry. For this nothing more simple ! We begin by connecting pin DC out 5V of the Raspberry, the GND PIN to pin 6, the SDA PIN to pin 3 PI and finally SCL PIN to pin 5.

This is the result :

  1. Update the system
     apt-get update && apt-get -y upgrade
  2. Enable the I2C interface by editing the file /etc/modules. Need to add the following lines at the end of the file :
    i2c-bcm2708 i2c-dev rtc_ds1307
  3. Remove the blacklist I2C module. In the file /etc/modprobe.d/raspi-blacklist.conf highlight comment line
    blacklist i2c-bcm2708

    Need to add a # beginning of line

    #blacklist i2c-bcm2708
  4. Restart the Raspberry
  5. You can control the modules in memory with the command lsmod
  6. Install the I2C tools
     apt-get install i2c-tools
  7. You can now check that your clock is detected with the command
    i2cdetect y X

    where X is worth :

    • 0 for models A and B 256 Mb
    • 1 for models B 512 Mb

    Home I used a model B 256 Mb so the parameter is 0
    1__sshThis is the position 68 !

  8. The next step is to define the type of device installed in 0 x 68.
    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device

    Consider replacing i2c-0 by i2c-1 If you use a model B

  9. It is time to test our new clock with the hwclock command return
    1__sshThe-r option allows to display the time stored in the RTC clock
  10. For now set the time clock, then set the time your Raspberry send time to the clock. For this, either you use the command
    date mmjjhhmmaaaa

    either a first time you connect the computer to the internet and you synchronize the time with the NTP service (This is done automatically)

  11. Once the time system set, you send the time system to the RTC clock
    hwclock -w

    The command returns no information. You can check the result with the read command

    hwclock -r
  12. Now if you want to set the system from the RTC clock time
    hwclock -s

To sync automatically with the Raspberry boot, You must edit the file /etc/rc.local by adding the following lines before the exit 0 the end

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

Or the following lines for the revision 2

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

Here's what it gives in edition for the revision 1 the Pi Raspberry


You now have a cheap clock to keep your system time if you don't have Internet access !

Personally I need for a mobile project. It was my last item to finalize the project, que je vais bientôt partager avec vous 🙂


You may also like...