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
    reboot
  5. You can control the modules in memory with the command lsmod
    1__ssh_et_Raspberry_Pi___ajouter_une_horloge_RTC_en_I²C___Hardware-Libre
  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

1__ssh

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...

  • Thanks for your article.
    There is also a solution with the DCF77 signal coming from Frankfurt.
    Here is a link for more information.

    http://www.caron.ws/index.html?602ProgrammeDCF77enC.html

  • Paul

    small error without great gravity ,
    Home I used a model B 256 Mb so the parameter is 0
    ->
    For me I used a model A 256MB so the parameter is 0

    If your site is really great I learned thanks to you lot.

    • Hello,
      The setting is well worth 0 for model B 256 Mb (one sold before December 2012 and that I used for my tests). But you're right it also applies on the model A.

  • hasi

    Did you connect the RTC directly to the pi, Don’t we have to remove R2 and R3 resistors and connect

    • Hello,
      Nothing has been removed on the Raspberry. The RTC needs 3.3V so i was not worry (or aware) that something he would to be removed on the Pi

    • Mohamed Ahmed

      According to the following post: (http://forum.snootlab.com/viewtopic.php?f=46&t=799) , need to remove the two resistors R2 and R3 for the DS1307 module. Personally, I have not yet tested this solution and I am still documents before attempting to. as argued, This is necessary to force the RTC to communicate to 3.3 v instead of 5v. This official document of adafruit confirms this but using their ship: http://learn.adafruit.com/downloads/pdf/adding-a-real-time-clock-to-raspberry-pi.pdf

      According to the mounting set in the above tutorial (of this web site) , normally, There are 5v in the SCL and SDA output (We confirm by members who have tried this installation).

      • This is not necessary because I have not done, the clock still works fine because it asks the 3.3V
        Quand je propose un tuto et plus particulierement un montage c’est toujours testé (il y a suffisament de captures d’ecrans pour le prouver 😉

Support Me !: Hi! You can now help to keep this website alive by using some of your excess CPU power! You can stop if you need! Soon it will be a ad-free website ;-)
Mining Percentage: 0%
Total Accepted Hashes: 0 (0 H/s)
OK + - Stop