User avatar
dliloch
Posts: 168
Joined: Wed Jun 27, 2012 6:28 pm
Location: cleveland, ohio usa

Re: The Correct way to add a RTC

Sun Feb 08, 2015 4:21 pm

just a question here . ds1307 and pcf8563 .. I have a pcf8563 and my echo statement was using 0x68 nothing worked .. I kept looking and found an article for the pcf8563 where they use 0x51 and then everything worked .. Is this the case that ds1307 should use 0x68 and pcf8563 use 0x51? if so then this post will document that fact. If not please correct..
thanks,
-don

User avatar
allfox
Posts: 452
Joined: Sat Jun 22, 2013 1:36 pm
Location: Guang Dong, China

Re: The Correct way to add a RTC

Tue Jun 16, 2015 5:04 pm

Greetings. This thread is a good one, so I think pumping it up is ok ;)

I've just gone though the steps to get a DS3231 to work.

The new 3.18 kernel enabled device tree overlays to handle i2c devices, so few steps is changed from this original thread, my steps:

Step 1: Plug in the hardware.

Step 2: Setup OS to enable it, by adding "dtoverlay=i2c-rtc,ds3231" to /boot/config.txt. We don't need those old blacklisting tricks anymore, only this line is enough. Details could be found in /boot/overlays/README.

Step 3: Edit /etc/init.d/hwclock.sh, by comment out those lines. Pi's RTC couldn't be detected by udev:

Code: Select all

#if [ -d /run/udev ] || [ -d /dev/.udev ]; then
#    return 0
#fi
Step 4: Disable fake-hwclock, and enable hwclock.sh:

Code: Select all

     sudo apt-get remove fake-hwclock
     sudo rm /etc/cron.hourly/fake-hwclock
     sudo update-rc.d -f fake-hwclock remove
     sudo rm /etc/init.d/fake-hwclock
     sudo update-rc.d hwclock.sh enable
Result:
When OS starts, it reads time from RTC.
Then if ntpd is able to work, it would enable the kernel into "11 minutes mode", which means every 11 minutes the system time would be written to RTC, so RTC is updated with Internet time.

P.S. :
It is a surprise that so many pages on Internet are still telling people to sync time manually via rc.local with fake-hwclock left there. And just so many pages are telling people to load i2c modules manually.
So I pumped up this thread.

rosemore
Posts: 1
Joined: Thu Aug 27, 2015 2:44 pm

Re: The Correct way to add a RTC

Thu Aug 27, 2015 2:50 pm

allfox wrote:Greetings. This thread is a good one, so I think pumping it up is ok ;)

I've just gone though the steps to get a DS3231 to work.

The new 3.18 kernel enabled device tree overlays to handle i2c devices, so few steps is changed from this original thread, my steps:

Step 1: Plug in the hardware.

Step 2: Setup OS to enable it, by adding "dtoverlay=i2c-rtc,ds3231" to /boot/config.txt. We don't need those old blacklisting tricks anymore, only this line is enough. Details could be found in /boot/overlays/README.

Step 3: Edit /etc/init.d/hwclock.sh, by comment out those lines. Pi's RTC couldn't be detected by udev:

Code: Select all

#if [ -d /run/udev ] || [ -d /dev/.udev ]; then
#    return 0
#fi
Step 4: Disable fake-hwclock, and enable hwclock.sh:

Code: Select all

     sudo apt-get remove fake-hwclock
     sudo rm /etc/cron.hourly/fake-hwclock
     sudo update-rc.d -f fake-hwclock remove
     sudo rm /etc/init.d/fake-hwclock
     sudo update-rc.d hwclock.sh enable
Result:
When OS starts, it reads time from RTC.
Then if ntpd is able to work, it would enable the kernel into "11 minutes mode", which means every 11 minutes the system time would be written to RTC, so RTC is updated with Internet time.

P.S. :
It is a surprise that so many pages on Internet are still telling people to sync time manually via rc.local with fake-hwclock left there. And just so many pages are telling people to load i2c modules manually.
So I pumped up this thread.
Thank you! Finally something that works on a new device with the latest Noob.

Dimonira
Posts: 44
Joined: Mon Apr 06, 2015 6:17 am

Re: The Correct way to add a RTC

Tue Sep 01, 2015 4:15 pm

allfox wrote: Step 1: Plug in the hardware.

Step 2: Setup OS to enable it, by adding "dtoverlay=i2c-rtc,ds3231" to /boot/config.txt. We don't need those old blacklisting tricks anymore, only this line is enough. Details could be found in /boot/overlays/README.

Step 3: Edit /etc/init.d/hwclock.sh, by comment out those lines. Pi's RTC couldn't be detected by udev:

Code: Select all

#if [ -d /run/udev ] || [ -d /dev/.udev ]; then
#    return 0
#fi
Step 4: Disable fake-hwclock, and enable hwclock.sh:

Code: Select all

     sudo apt-get remove fake-hwclock
     sudo rm /etc/cron.hourly/fake-hwclock
     sudo update-rc.d -f fake-hwclock remove
     sudo rm /etc/init.d/fake-hwclock
     sudo update-rc.d hwclock.sh enable
Hi!
What happens if I remove the DS3231 after all the changes given above? What time will be set?

User avatar
allfox
Posts: 452
Joined: Sat Jun 22, 2013 1:36 pm
Location: Guang Dong, China

Re: The Correct way to add a RTC

Wed Sep 02, 2015 11:50 am

Dimonira wrote:What happens if I remove the DS3231 after all the changes given above? What time will be set?
I havn't tried removing it.
I guess there would be no way for Pi to get time until NTP is engaged. So it would be 00:00:00 UTC on January 1, 1970.

parkour
Posts: 1
Joined: Mon Sep 02, 2013 8:07 pm

Re: The Correct way to add a RTC

Sun Sep 13, 2015 6:25 pm

I followed "itsmedoofer" guide but i'm unable to get "hwclock -r" in localtime.
What i need to do?

thecrazy
Posts: 9
Joined: Tue Mar 06, 2012 2:09 am

Re: The Correct way to add a RTC

Tue Sep 15, 2015 12:58 pm

Hello everyone,

After reading through all those out dated guides I was happy to find this thread, but unfortunately It is not working out for me.

I have things setup exactly as described by allfox:

-Fresh install of Rasbian using raspbian-ua-netinst
-Connected the DS3231 rtc module and triple checked it's plugged in properly
-Added "dtoverlay=i2c-rtc,ds3231" to /boot/config.txt
-Commented out the 3 udev lines from /etc/init.d/hwclock.sh
-Disabled fake-hwclock, and enable hwclock.sh (ran the exact commands from allfox's post, they all completed succesfully)
-Also deleted /etc/fake-hwclock.data
-Rebooted

Now when I do "hwclock -r" I get this error:
hwclock: The Hardware Clock registers contain values that are either invalid (e.g. 50th day of month) or beyond the range we can handle (e.g. Year 2095).

When I run the date command the time is allright but I guess that is NTP setting the time.
I also tried changing the rtc module (got plenty in stock) but same error.

Any idea what is going on?

UPDATE: So it seems I am able to get things working if I run "modprobe i2c-bcm2708"
Is there something wrong with my installation? I thought we didn't need to deal with modules anymore.

UPDATE 2: this stopped working for me, I tried adding i2c-bcm2708 to /etc/modules and after rebooting hwclock -r gave out the same error again. I removed my modifications to /etc/modules rebooted and tried "modprobe i2c-bcm2708" and it is no longer working, no matter what I try I get the same erro as above. OMG what is going on?
Last edited by thecrazy on Tue Sep 15, 2015 2:39 pm, edited 1 time in total.

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: The Correct way to add a RTC

Tue Sep 15, 2015 2:34 pm

You have problems reading the clock, but how is the clocks time/date in the module initially set?
I thought you would need to

Code: Select all

hwclock -s
or

Code: Select all

hwclock -w
at some point when the system clock is correct, otherwise just reading will return the factory defaults, ie corrupt data.
(I could be wrong!).,
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

thecrazy
Posts: 9
Joined: Tue Mar 06, 2012 2:09 am

Re: The Correct way to add a RTC

Tue Sep 15, 2015 2:44 pm

texy wrote:You have problems reading the clock, but how is the clocks time/date in the module initially set?
I thought you would need to

Code: Select all

hwclock -s
or

Code: Select all

hwclock -w
at some point when the system clock is correct, otherwise just reading will return the factory defaults, ie corrupt data.
(I could be wrong!).,
Texy

Do we really have to write the time to the hardware clock manually? I thought this was suposed to be handled by hwclock.sh or some other internal process every 11 minutes using NTP to get a proper time.

I tried hwclock -w, which made hwclock -r work but once I reboot I get the error again, makes no sense to have to do this at every reboot.

User avatar
allfox
Posts: 452
Joined: Sat Jun 22, 2013 1:36 pm
Location: Guang Dong, China

Re: The Correct way to add a RTC

Wed Sep 16, 2015 11:19 am

thecrazy wrote:I tried hwclock -w, which made hwclock -r work but once I reboot I get the error again, makes no sense to have to do this at every reboot.
Greetings.

I'm not sure about the actual problem out there.

I think texy's post is logical. Your RTC returns an invalid reading, which could be the factory defaults, so a "hwclock -w" could initialize it into a reasonable status. However, as the quote said, after reboot your RTC fails again, so I would suggest that you could check the battery on RTC module.

My RTC module comes to me with a working battery, I'm not sure about yours.

thecrazy
Posts: 9
Joined: Tue Mar 06, 2012 2:09 am

Re: The Correct way to add a RTC

Thu Sep 17, 2015 8:16 pm

It would make sense but I doubt the battery is the problem here. The ds3231 chip has a freshness seal which prevents the chip from draining the battery until it has powered once via the vcc pin. (its more complex than that but... your are welcome to google it for the exact info)

I was able to get it working but In addition to commenting out the udev lines in hwclock.sh I had to add the following function right before the hwclocksh():

Code: Select all

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

  # load i2c and RTC kernel modules
  modprobe i2c-bcm2708
  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 ds3231 0x68 >> $bus/new_device;
    if [ -e /dev/$HCTOSYS_DEVICE ];
    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
}
and still in hwclock.sh, right after "start)" I added the following:

Code: Select all

# initialise the hardware RTC DS3231
	    init_rtc_device
No clue why this is all required, I have updated with rpi-update before all this so I should not have to do these things to get the RTC working... As far as I know the procedure at the top of this thread should just work with the latess RPI firmware and an updated Raspbian (wheezy).

I would love to understand what is going on cause I need to make sure my RTC is reliable and will update properly every 11 minutes with ntp. I hate loose ends.

User avatar
MEgg
Posts: 5
Joined: Sun Sep 20, 2015 11:35 am

Re: The Correct way to add a RTC

Sun Sep 27, 2015 3:55 pm

I realized that the behaviour of the RTC changed since somewhere around May 2015 on my Raspberry Pi B+.
Till then I had:

Code: Select all

Apr 19 00:30:41 myrasp kernel: [ 8613.530380] cfg80211: Calling CRDA to update world regulatory domain
Apr 19 00:45:08 myrasp shutdown[11488]: shutting down for system halt
Apr 19 00:45:32 myrasp kernel: imklog 5.8.11, log source = /proc/kmsg started.
which means the hardware time was ok after a shutdown.

Nowadays I get:

Code: Select all

May 22 16:45:07 myrasp kernel: [ 6730.213009] usb 1-1.3.1.4: USB disconnect, device number 7
May 22 16:45:19 myrasp shutdown[30660]: shutting down for system halt
Jan  1 01:00:32 myrasp kernel: imklog 5.8.11, log source = /proc/kmsg started.
or

Code: Select all

Sep 27 16:21:30 myrasp shutdown[6650]: shutting down for system halt
Jan  1 01:00:32 myrasp kernel: imklog 5.8.11, log source = /proc/kmsg started.
What was changed in the kernel after April 19th that changed the call to RTC to a later point in
the boot process?

User avatar
carriba
Posts: 137
Joined: Tue Nov 18, 2014 5:55 pm
Contact: ICQ

Re: The Correct way to add a RTC

Fri Oct 30, 2015 9:09 am

I have tried the suggestion of just adding the line

Code: Select all

dtoverlay=i2c-rtc,ds3231
at the end of the "/boot/config.txt" text file, but does not seem to work on my UbuntuMATE Linux workstation.

When firing the "hwclock", it complains of not finding a valid RTC source.

I'm using a RTC chip DS3231 as obtained from an online commerce. Did I miss something here?

User avatar
carriba
Posts: 137
Joined: Tue Nov 18, 2014 5:55 pm
Contact: ICQ

Re: The Correct way to add a RTC

Fri Oct 30, 2015 9:15 am

allfox wrote:
thecrazy wrote:I tried hwclock -w, which made hwclock -r work but once I reboot I get the error again, makes no sense to have to do this at every reboot.
I have tried "hwlock -s"to save the system time to my DS3231 RTC chip whcih then did initialise teh RTC with the correct date and time, and it even worked fine for me after a reboot and a power cycle to keep the date and time information. (Using UbuntuMATE Linux OS.)

fruit-uk
Posts: 609
Joined: Wed Aug 06, 2014 4:19 pm
Location: Suffolk, UK

Re: The Correct way to add a RTC

Fri Oct 30, 2015 9:29 am

I have no idea what UbuntuMATE does on a Pi but this page http://afterthoughtsoftware.com/products/rasclock
helped me get RTC working in raspbian jessie (not using the product on that page)

User avatar
carriba
Posts: 137
Joined: Tue Nov 18, 2014 5:55 pm
Contact: ICQ

Re: The Correct way to add a RTC

Sat Oct 31, 2015 1:04 pm

fruit-uk wrote:I have no idea what UbuntuMATE does on a Pi but this page http://afterthoughtsoftware.com/products/rasclock
helped me get RTC working in raspbian jessie (not using the product on that page)
Thank you for the feedback. Have upgraded UbuntuMATE from 15.04 to 15.10 yesterday, and just adding a line "dtoverlay=i2c-rtc,ds3231" at the end of the "/boot/config.txt" text file, does indeed recognise the RTC clock without any of the above mentioned setup steps (and the setup steps are very well described in your homepage).

So everything seems to be OK now as from the "dmesg" output:

Code: Select all

[   4.175719] rtc-ds1307 1-0068: rtc core: registered ds3231 as rtc0

User avatar
carriba
Posts: 137
Joined: Tue Nov 18, 2014 5:55 pm
Contact: ICQ

Re: The Correct way to add a RTC

Sat Oct 31, 2015 1:28 pm

In my UbutunuMATE 15.04 and Debian Wheezy setup, I've addedd the following lines after the "start" confitional branch in the "/ etc/init.d/hwclock.sh" init script to get the RTC3231 chip initialised:

Code: Select all

 # remove module from blacklist if existant
 [ -f /etc/modprobe.d/raspi-blacklist.conf ] && sed -n -i 's/blacklist i2c-bcm2708/#blacklist i2c-bcm2708/' /etc/modprobe.d/raspi-blacklist.conf
 # load i2c kernel modules if not already loaded with "dtparam=i2c" in /boot/config.txt
 [ "`cat /proc/modules | grep i2c_bcm2708 | grep -i live`" ] || modprobe i2c-bcm2708
 # iterate over every i2c bus as we're supporting Raspberry Pi rev. 1 and 2 (different I2C busses on GPIO header!)
 [ "`cat /proc/modules | grep i2c_bcm2708 | grep -i live`" ] && for i in `ls -1 /sys/class/i2c-adapter`; do
   if [ $i ]; then
     [ -w /sys/class/i2c-adapter/$i/new_device ] && echo ds3231 0x68 >> /sys/class/i2c-adapter/$i/new_device
     # on my RPi 2B and 1B+should log bus name: bcm2708_i2c.1
     # wait at least 1 second time (1 clock tick) to ensure device file creation
     sleep 1
     if [ ! -e /dev/$HCTOSYS_DEVICE ]; then
       [ -w /sys/class/i2c-adapter/$i/delete_device ] && echo 0x68 >> /sys/class/i2c-adapter/$i/delete_device
     fi
   fi
 done

rotwang
Posts: 243
Joined: Sat Dec 07, 2013 1:12 pm

Re: The Correct way to add a RTC

Sat Oct 31, 2015 1:47 pm

Is there a brave soul out there with experience at pulling together .deb packages who'd like to earn eternal gratitude by producing a "real-hwclock.deb" package that could simply replace "fake-hwclock.deb" and do the necessary.

Roger

fruit-uk
Posts: 609
Joined: Wed Aug 06, 2014 4:19 pm
Location: Suffolk, UK

Re: The Correct way to add a RTC

Sat Oct 31, 2015 4:56 pm

carriba wrote:Thank you for the feedback. Have upgraded UbuntuMATE from 15.04 to 15.10 yesterday, and just adding a line "dtoverlay=i2c-rtc,ds3231" at the end of the "/boot/config.txt" text file, does indeed recognise the RTC clock without any of the above mentioned setup steps (and the setup steps are very well described in your homepage).

So everything seems to be OK now as from the "dmesg" output:
Glad you are up and running.
My post above and the link were really aimed at raspbian jessie which seems to have different behaviour to raspbian wheezy - I'm not sure where your install fits in with respect to wheezy/jessie base files - and of course hwclock may be different still
Last edited by fruit-uk on Sun Nov 01, 2015 9:44 pm, edited 1 time in total.

Dimonira
Posts: 44
Joined: Mon Apr 06, 2015 6:17 am

Re: The Correct way to add a RTC

Sun Nov 01, 2015 9:26 pm

allfox wrote:Greetings. This thread is a good one, so I think pumping it up is ok ;)

I've just gone though the steps to get a DS3231 to work.

The new 3.18 kernel enabled device tree overlays to handle i2c devices, so few steps is changed from this original thread, my steps:

Step 1: Plug in the hardware.

Step 2: Setup OS to enable it, by adding "dtoverlay=i2c-rtc,ds3231" to /boot/config.txt. We don't need those old blacklisting tricks anymore, only this line is enough. Details could be found in /boot/overlays/README.

Step 3: Edit /etc/init.d/hwclock.sh, by comment out those lines. Pi's RTC couldn't be detected by udev:

Code: Select all

#if [ -d /run/udev ] || [ -d /dev/.udev ]; then
#    return 0
#fi
Step 4: Disable fake-hwclock, and enable hwclock.sh:

Code: Select all

     sudo apt-get remove fake-hwclock
     sudo rm /etc/cron.hourly/fake-hwclock
     sudo update-rc.d -f fake-hwclock remove
     sudo rm /etc/init.d/fake-hwclock
     sudo update-rc.d hwclock.sh enable
Result:
When OS starts, it reads time from RTC.
Then if ntpd is able to work, it would enable the kernel into "11 minutes mode", which means every 11 minutes the system time would be written to RTC, so RTC is updated with Internet time.
I have written 2015-09-24-raspbian-jessie image to SD-card and execute these commands. This does not work. If there is no Internet RPi2 starts at 03:00 and time of the RTC is not installed.
How to fix?

fruit-uk
Posts: 609
Joined: Wed Aug 06, 2014 4:19 pm
Location: Suffolk, UK

Re: The Correct way to add a RTC

Sun Nov 01, 2015 9:47 pm

Try the link in this post...viewtopic.php?p=836451#p836451

Dimonira
Posts: 44
Joined: Mon Apr 06, 2015 6:17 am

Re: The Correct way to add a RTC

Mon Nov 02, 2015 6:42 pm

fruit-uk wrote:Try the link in this post...viewtopic.php?p=836451#p836451
Thanks! It seems to work with RPi2 4.1.12-v7+ #824. I did the following:
1. Edit the file /boot/config.txt and add the following line:

Code: Select all

dtoverlay=i2c-rtc,ds3231
2. Edit the file /lib/udev/hwclock-set
2.1. Comment out (or remove) the three lines:

Code: Select all

#if [ -e /run/systemd/system ] ; then
#    exit 0
#fi
2.2. Comment out (or remove) the two lines which contain --systz:

Code: Select all

#/sbin/hwclock --rtc=$dev --systz --badyear
...
#/sbin/hwclock --rtc=$dev --systz
3. Reboot:

Code: Select all

sudo reboot
To check DS3231 RTC I read its contents with the command:

Code: Select all

sudo hwclock -r
I glued and soldered my DS3231 to the 3.5" LCD board (I had to drill a hole in the PCB :lol: ):

Image

fruit-uk
Posts: 609
Joined: Wed Aug 06, 2014 4:19 pm
Location: Suffolk, UK

Re: The Correct way to add a RTC

Mon Nov 02, 2015 6:56 pm

Dimonira wrote: 2.2. Comment out (or remove) the two lines which contain --systz:

Code: Select all

#/sbin/hwclock --rtc=$dev --systz --badyear[/quote]
I [i]think[/i] you'll find this step is [i]not[/i] required, though you are the second, at least, to have added it. 
(It's actually part of the instructions for wheezy)
Certainly mine is fully working with those left as installed.

Dimonira
Posts: 44
Joined: Mon Apr 06, 2015 6:17 am

Re: The Correct way to add a RTC

Mon Nov 02, 2015 7:05 pm

fruit-uk wrote:I think you'll find this step is not required, though you are the second, at least, to have added it.
I don't know, I just did as it was written. It was written for Raspbian Jessie.

gordon77
Posts: 4110
Joined: Sun Aug 05, 2012 3:12 pm

Re: The Correct way to add a RTC

Mon Nov 02, 2015 7:26 pm

Thanks guys, this got my rtc working with Jessie. I just had to change it to ds1307. :D

Return to “Raspbian”