Skip to content
Orlin edited this page Feb 17, 2018 · 2 revisions

The Correct way to add a RTC

sudo nano /boot/config.txt

device_tree_param=i2c_arm=on
#I2C
dtparam=i2c1=on
#lirc-rpi
dtoverlay=lirc-rpi,gpio_in_pin=16,gpio_in_pull=high
#w1-gpio
dtoverlay=w1-gpio-pullup,gpiopin=x,pullup=y

sudo nano /boot/cmdline.txt

bcm2708.vc_i2c_override=1

sudo nano /etc/modules-load.d/raspberrypi.conf

i2c-bcm2708
i2c-dev

Enable the Kernel modules:-

sudo nano /etc/modules

Add the following:

i2c-bcm2708 
i2c-dev

Remove them from the blacklist by adding #

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Make sure you see:

#blacklist spi-bcm2708
#blacklist i2c-bcm2708

Install the i2c tools for testing:

sudo apt-get install i2c-tools

reboot

Run a test to see if all is well...

sudo i2cdetect -y 1

You should see something like:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

load up the RTC module and test by running:

sudo modprobe rtc-ds1307

sudo bash

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

Check the clock is running and can be read:

sudo hwclock -r

Make sure pi is connected to internet so correct time is set....

Write the current system time to the hardware clock:

sudo hwclock -w

Add the clock to the Kernel modules:

sudo nano /etc/modules

Add:

rtc-ds1307

Reconfigure the hwclock.sh script:

sudo nano /etc/init.d/hwclock.sh

After "unset TZ" at top add..

init_rtc_device()
{
  [ -e /dev/rtc0 ] && return 0;

  # load i2c and RTC kernel modules
  modprobe i2c-dev
  modprobe rtc-ds1307

  # iterate over every i2c bus as we're supporting Raspberry Pi rev. 1 and 2
  # (different I2C busses on GPIO header!)
  for bus in $(ls -d /sys/bus/i2c/devices/i2c-*);
  do
    echo ds1307 0x68 >> $bus/new_device;
    if [ -e /dev/rtc0 ];
    then
      log_action_msg "RTC found on bus `cat $bus/name`";
      break; # RTC found, bail out of the loop
    else
      echo 0x68 >> $bus/delete_device
    fi
  done
}

Find "case "$1" in" and edit as per:

    case "$1" in
	start)
	    # If the admin deleted the hwclock config, create a blank
	    # template with the defaults.
	    if [ -w /etc ] && [ ! -f /etc/adjtime ] && [ ! -e /etc/adjtime ]; then
	        printf "0.0 0 0.0\n0\nUTC" > /etc/adjtime
	    fi
		init_rtc_device

            # Raspberry Pi doesn't have udev detectable RTC
	    #if [ -d /run/udev ] || [ -d /dev/.udev ]; then
		#return 0
	    #fi

Update the real HW Clock and remove the fake:

sudo update-rc.d hwclock.sh enable

sudo update-rc.d fake-hwclock remove

Now that real hardware clock is installed, remove the fake package and it’s crons:

sudo apt-get remove -y fake-hwclock

sudo rm /etc/cron.hourly/fake-hwclock

sudo rm /etc/init.d/fake-hwclock

Clone this wiki locally