Wednesday, 20 January 2016

Raspberry PI - Adding a real time clock module (RTC)

The Raspberry PI does not have an internal clock so on power up it gets the time from the internet. But if there is no internet the clock reverts to a default date and time (1st January 1970 10:00AM in my time zone). An RTC module usually has a DS1307 chip that can keep time for about 2 years with a small cell battery the module plugs on to the GPIO pins and updates the date and time on power up. Here is a guide on how to install and set-up RTC.

Buy or build RTC module

Real Time Clock (RTC) modules can be purchased from EBay for a few dollars. Search on EBay for "Raspberry PI RTC".

Connect RTC module

Attach the module to the Raspberry PI. Most have a plug that plugs straight on to the GPIO pins. The I2C uses pins 3 and 5 and the module usually uses pin 1 and 9 for power.

Check and update time and date settings

  1. In terminal type sudo raspi-config to set country and time zone,
  2. Restart system if changes are made
Enable i2c the communication to the RTC module

Install I2C interface

  1. First make sure the system is up to date
    sudo apt-get update
    sudo apt-get upgrade
    sudo reboot
  2. Enable the I2C interface 
  3. sudo raspi-config select Advanced the enable I2C and set to load by default.
  4. Install I2C softwaresudo apt-get install i2c-tools
  5. Reboot the Raspberry PI
    sudo reboot

Configure the I2C interface

  1. Add the following lines for the DS1307 chip support
  2. Type sudo nano /etc/modules add the below linesi2c-bcm2708i2c-devrtc-ds1307Press ctrl y then enter to save
  3. Restart system
    sudo reboot
  4. Then check the interface is working
    sudo bash
    i2cdetect -y 1 (Note:i2cdetect -y 0 on Model A)
  5. Module detected at address 68

Start the RTC module

  1. sudo su
    echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
  2. Check the hardware clock is working
    sudo hwclock
  3. set the local time if required
    sudo date --set="Jan 14 10:00:00 AEDT 2016"
    Note:Use date command to check the time zone
  4. Set the hardware clock from the system date time
    sudo hwclock -w

Update the Raspberry PI time on boot

  1. Add the configuration to the start up file
  2. sudo nano /etc/rc.local
  1. Add lines just before the exit 0
    echo ds1307 0x68 > /sys/class/itc-1/new_device
      Installs the device
    sudo hwclock -s  
    Sets the system time from the hardware clock

Test the clock works

  1. Power down the Raspberry PI
  2. Unplug internet cable and/or WIFI dongle
  3. Start the Raspberry PI
  4. Check the time and date are correct after boot

No comments:

Post a Comment

Thank you fro reading my page.
Let me know if there is anything that I could add or change.
Please let me know if this information is helpful.