User avatar
adafruit
Posts: 71
Joined: Sat Apr 28, 2012 3:32 pm
Location: NYC, USA
Contact: Website

Tutorial: Add a Real Time Clock to the Raspberry Pi

Sat Sep 01, 2012 3:19 pm

Image

Tutorial: Add a Real Time Clock to the Raspberry Pi
http://learn.adafruit.com/adding-a-real ... pberry-pi/

The Raspberry Pi is designed to be an ultra-low cost computer, so a lot of things we are used to on a computer have been left out. For example, your laptop and computer have a little coin-battery-powered 'Real Time Clock' (RTC) module, which keeps time even when the power is off, or the battery removed. To keep costs low and the size small, an RTC is not included with the Raspberry Pi. Instead, the Pi is intended to be connected to the Internet via Ethernet or WiFi, updating the time automatically from the global ntp (nework time protocol) servers

For stand-alone projects with no network connection, you will not be able to keep the time when the power goes out. So in this project we will show you how to add a low cost battery-backed RTC to your Pi to keep time!

Learn more!
http://learn.adafruit.com/adding-a-real ... pberry-pi/

User avatar
Jim JKla
Posts: 2218
Joined: Sun Jan 29, 2012 11:15 pm
Location: Newcastle upon Tyne UK

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Sat Sep 01, 2012 4:18 pm

Is there any chance of making a real time clock like this as a USB device?

Note battery backed in the same way so that it was not drawing power from the USB port.
Noob is not derogatory the noob is just the lower end of the noob--geek spectrum being a noob is just your first step towards being an uber-geek ;)

If you find a solution please post it in the wiki the forum dies too quick

User avatar
adafruit
Posts: 71
Joined: Sat Apr 28, 2012 3:32 pm
Location: NYC, USA
Contact: Website

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Sun Sep 02, 2012 9:10 pm

Jim JKla wrote:Is there any chance of making a real time clock like this as a USB device? Note battery backed in the same way so that it was not drawing power from the USB port.
huh, we don't recall ever seeing a USB based RTC, if you find one can you post a link?

User avatar
Jim JKla
Posts: 2218
Joined: Sun Jan 29, 2012 11:15 pm
Location: Newcastle upon Tyne UK

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Mon Sep 03, 2012 6:48 am

I think it would be a build I have never seen one.

I have a radio corrected clock (the type that linked to the atomic clock more info here http://www.npl.co.uk/science-technology ... ime-signal that has rejected its second hand (fell off) in the kitchen I was thinking of taking it to my local maker group to see if they could hack it into a USB. ;)

I know this is not a real time clock but I thought it was a related comment. ;)
Noob is not derogatory the noob is just the lower end of the noob--geek spectrum being a noob is just your first step towards being an uber-geek ;)

If you find a solution please post it in the wiki the forum dies too quick

jhd
Posts: 33
Joined: Thu Aug 02, 2012 1:11 pm
Location: france (44)
Contact: Website

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Mon Sep 03, 2012 9:09 am

great tutorial thanx
Site FR RaspBerry Pi: http://www.zem.fr

User avatar
Jim JKla
Posts: 2218
Joined: Sun Jan 29, 2012 11:15 pm
Location: Newcastle upon Tyne UK

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Mon Sep 03, 2012 9:26 am

We should realy see about getting a copy of this into the wiki. ;)
Noob is not derogatory the noob is just the lower end of the noob--geek spectrum being a noob is just your first step towards being an uber-geek ;)

If you find a solution please post it in the wiki the forum dies too quick

wmacm00
Posts: 5
Joined: Mon Sep 17, 2012 1:12 pm

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Wed Sep 19, 2012 12:19 pm

Question

Now about using the new P5 connections for the RTC ?

Andrew

robotworkshop
Posts: 24
Joined: Thu Aug 30, 2012 7:19 pm

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Mon Sep 24, 2012 1:54 pm

This is an excellent write up on how to add a hardware clock to the Raspberry Pi. I built a similar circuit on a protoboard using the DS3231SN chip and it works great.

In order to set the time on the RP from the hardware clock I had to use the command:

sudo hwclock -s

Maybe that command should be added to the tutorial.

Robert

User avatar
psergiu
Posts: 223
Joined: Mon Nov 07, 2011 8:36 am
Location: TX, U.S.A. (was: RO, E.U.)
Contact: Website

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Tue Sep 25, 2012 11:39 am

Small correction to the tutorial
With Raspbian, in order to correctly set the time using hwclock you have to (as root):

dpkg --purge fake-hwclock

rm /etc/rcS.d/K0?hwclock.sh

edit /etc/init.d/hwclock and modify the following lines in the header as:

# Required-Start: mountdevsubfs kmod
# X-Start-Before:

(this is a Debian bug, without this modifications, the I2C kernel modules are not yet loaded when the script is started thus reading the hardware clock fails)

run:

update-rc.d hwclock.sh enable

then add:

echo pcf8563 0x51 > /sys/class/i2c-adapter/i2c-0/new_device

(or wharever line you need to activate your device) at the end of /etc/default/hwclock or in /etc/init.d/hwclock after ' log_action_msg "Setting the system clock" '

This way the system will have the correct clock set early at the boot time.

robotworkshop
Posts: 24
Joined: Thu Aug 30, 2012 7:19 pm

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Tue Sep 25, 2012 3:28 pm

Thanks for the additional notes. I was doing all my testing on the command line after the Raspberry Pi was up. I'll try these steps to make it automatically use the time from the Real TIme Clock chip upon boot each time.

It was nice to try the Occidentalis v0.2 image since that had all the packages already there and made following the tutorial easy.

I would prefer to use the regular Raspbian "wheezy" distribution since that seems to be the main one that everyone is using. It would be helpful to note what packages should be added to that in order to support the Real Time Clock since the i2cdetect program doesn't seem to come as part of Raspbian. The list of packages would help for those using other distributions too.

Robert

User avatar
psergiu
Posts: 223
Joined: Mon Nov 07, 2011 8:36 am
Location: TX, U.S.A. (was: RO, E.U.)
Contact: Website

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Tue Sep 25, 2012 3:32 pm

i2cdetect is from the i2c-tools package

robotworkshop
Posts: 24
Joined: Thu Aug 30, 2012 7:19 pm

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Tue Sep 25, 2012 5:38 pm

Thank You! I'll try adding the package to Raspbian and see how it works out.

Since it should be possible to use the same interface for other devices I was looking around to see what else was available. One of the sites I found was:

http://www.i2cdevlib.com/

Seems like it has some useful information there.

8ctopus
Posts: 11
Joined: Sun Sep 23, 2012 9:50 am

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Sat Oct 13, 2012 4:16 am

run:
update-rc.d hwclock.sh enable
I'm getting:

Code: Select all

update-rc.d: using dependency based boot sequencing
update-rc.d: error: no runlevel symlinks to modify, aborting!
Any ideas?

User avatar
psergiu
Posts: 223
Joined: Mon Nov 07, 2011 8:36 am
Location: TX, U.S.A. (was: RO, E.U.)
Contact: Website

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Sat Oct 13, 2012 7:59 am

Try making the symlinks by hand and re-run ...

Code: Select all

cd /etc/rc0.d
ln -s ../init.d/hwclock.sh K06hwclock.sh
cd ../rc6.d
ln -s ../init.d/hwclock.sh K06hwclock.sh
cd ../rcS.d
ln -s ../init.d/hwclock.sh S10hwclock.sh

8ctopus
Posts: 11
Joined: Sun Sep 23, 2012 9:50 am

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Mon Oct 15, 2012 5:55 am

@psergiu Thanks, it turned out I was missing the S10hwclock.sh symlink. The update-rc.d hwclock.sh enable command ran successfully and I followed all of the other instructions, however upon reboot the system clock is still resetting to 1970 and I have to run hwclock -s to set the correct time. What could be wrong?

User avatar
psergiu
Posts: 223
Joined: Mon Nov 07, 2011 8:36 am
Location: TX, U.S.A. (was: RO, E.U.)
Contact: Website

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Mon Oct 15, 2012 9:04 am

Have you added "echo pcf8563 0x51 > /sys/class/i2c-adapter/i2c-0/new_device" as needed ?
After boot, can you please scroll up on the console (shift-pageup) and tell me if there are any RTC or module loading error messages ?

8ctopus
Posts: 11
Joined: Sun Sep 23, 2012 9:50 am

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Mon Oct 15, 2012 10:02 am

I've added "echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device" to /etc/default/hwclock since I'm using a ds1307. I also tried adding the same line to /etc/init.d/hwclock.

I only have ssh access at the moment but I had a search through the logs and found the following in dmesg, syslog and kern

Code: Select all

drivers/rtc/hctosys.c: unable to open rtc device (rtc0)
does that provide any clues? If not I will try to attach a monitor tomorrow to read the boot messages. Thanks for your help!

User avatar
psergiu
Posts: 223
Joined: Mon Nov 07, 2011 8:36 am
Location: TX, U.S.A. (was: RO, E.U.)
Contact: Website

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Mon Oct 15, 2012 10:19 am

Hmmm ... it means that hwclock is started before the kernel modules are loaded.

Please run:

ls -l /etc/rcS.d/*mod*

I have

/etc/rcS.d/S08kmod -> ../init.d/kmod

So on mine the kernel modules are loaded before hwclock is run (S08 vs S10)

You have added i2c-bcm2708, i2cdev & rtc-ds1307 to /etc/modules, right ?

8ctopus
Posts: 11
Joined: Sun Sep 23, 2012 9:50 am

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Mon Oct 15, 2012 7:46 pm

running ls -l /etc/rcS.d/*mod* I get:
/etc/rcS.d/S06kmod -> ../init.d/kmod
You have added i2c-bcm2708, i2cdev & rtc-ds1307 to /etc/modules, right ?
Not specifically, I am using the Occidentalis v0.2 image and just followed the steps in the tutorial plus your additional instructions.

User avatar
psergiu
Posts: 223
Joined: Mon Nov 07, 2011 8:36 am
Location: TX, U.S.A. (was: RO, E.U.)
Contact: Website

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Mon Oct 15, 2012 9:21 pm

Edit /etc/modules and add those 3 lines at the end:

Code: Select all

i2c-bcm2708
i2cdev
rtc-ds1307
Edit /etc/modprobe.d/raspi-blacklist.conf and comment out the line

Code: Select all

#blacklist i2c-bcm2708

8ctopus
Posts: 11
Joined: Sun Sep 23, 2012 9:50 am

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Tue Oct 16, 2012 6:39 pm

sorry, yes those lines are already in my modprobe file and the other line has already been commented out. Basically I started with a clean install of Occidentalis v0.2, and followed the Adafruit tutorial exactly. I thought everything was working fine until I noticed that the system clock was becoming more out of sync with the hwclock with every reboot, so obviously it was still using fake-hwclock. Following your instructions fake-hwclock is now disabled but for some reason it won't use hwclock on boot :?

User avatar
psergiu
Posts: 223
Joined: Mon Nov 07, 2011 8:36 am
Location: TX, U.S.A. (was: RO, E.U.)
Contact: Website

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Tue Oct 16, 2012 7:19 pm

Ugly hack time :)

Edit /etc/init.d/hwclock.sh

after log_action_msg "Setting the system clock"
add:

Code: Select all

modprobe i2c-bcm2708
modprobe i2cdev
modprobe rtc-ds1307
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
Now it SHOULD work okay.

8ctopus
Posts: 11
Joined: Sun Sep 23, 2012 9:50 am

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Thu Oct 18, 2012 6:49 am

still no joy unfortunately :cry: but I noticed a couple of things.

firstly I had been using the line modprobe i2c-dev which was already included in my etc/modules file rather than modprobe i2cdev (without the hyphen). I guess that makes a difference.

Secondly when I include the lines four lines in my /etc/init.d/hwclock.sh file this actually seems to corrupt the hwclock upon reboot, instead of being able to run hwclock -s, I get :

Code: Select all

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).
I set the hwclock again and tried another reboot and again the same thing happened.

I was also able to plug in a monitor and noticed the following boot messages:

Code: Select all

i2c i2c-0; new_device; Instantiated device ds1307 at 0X68
rtc-ds1307 0-0068; rtc core; registered ds1307 as rtc0
rtc-ds1307 0-0068; 56 bytes nvram
i2c i2c-0; Failed to register i2c client ds1307 at 0x68 (-16)
and later on

Code: Select all

[FAIL]startpar: service(s)  returned failure: rc.local ... failed!

User avatar
psergiu
Posts: 223
Joined: Mon Nov 07, 2011 8:36 am
Location: TX, U.S.A. (was: RO, E.U.)
Contact: Website

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Thu Oct 18, 2012 7:06 am

You have connected a battery to that RTC chip, right ? :-) I have only seen that hwclock register corruption errors only with run-down rtc batteries.
The chip is a pure DS1307 or a "compatible" variant ?

Those i2c errors are no problem.
You are doing the "echo ds1307 0x68 ..." two times so the 2nd time it fails - no impact.

Check rc.local - you may have a typo in there.

8ctopus
Posts: 11
Joined: Sun Sep 23, 2012 9:50 am

Re: Tutorial: Add a Real Time Clock to the Raspberry Pi

Fri Oct 19, 2012 3:41 am

I'm using this RTC www.sparkfun.com/products/99. It came with a battery installed so I assume it's good but perhaps not! I will have another play with it. Thanks again for your help.

Return to “Interfacing (DSI, CSI, I2C, etc.)”