Galaxynik
Posts: 43
Joined: Mon Jan 02, 2012 2:55 pm

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

Sat Aug 24, 2013 4:26 pm

yes it does -

I only found this thread yesterday whilst trying to figure out how to install my RTC :D

Now to play with the 1wire stuff :o

Hi-Z
Posts: 29
Joined: Wed Jan 02, 2013 8:19 pm
Location: Canuckistan

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

Sat Aug 24, 2013 6:10 pm

Thanks for confirming this ... Myself and those reading this thread in the future appreciate your input

Have fun with the 1-wire devices, I found that they present their own set of challenges with timing and reliability of data transfer because they use "bit-banging" instead of the i2c protocol.

Galaxynik
Posts: 43
Joined: Mon Jan 02, 2012 2:55 pm

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

Sat Aug 24, 2013 8:03 pm

well I'm using an I2C bus master - so far I have managed to get a couple of tempo sensors to read using the

Code: Select all

cat 'owfsdirectory/sensor code/temprature' 
method - now to get owserver and owhttop up and running then on to some perls - Its a very steep learning curve :D

itsisme
Posts: 20
Joined: Sat Mar 09, 2013 2:16 am

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

Tue Sep 24, 2013 9:05 pm

Hello,

Recently I added a ds1307 from ebay and all is looking good its reported with i2cdetect -y 1
see http://pi.gids.nl:81/temp/i2c.php

However I can not get it to run hwclock -r that reports
hwclock: Warning: unrecognized third line in adjtime file
(Expected: `UTC' or `LOCAL' or nothing.)
hwclock: ioctl(RTC_RD_TIME) to /dev/rtc0 to read the time failed: Invalid argument

At start i2cget -y 1 0x68 0 reported seconds counting but now I guess I changed to much to get it to run properly.
I followed quite some hints on this forum but now its a mix of settings that I guess are confusing.

root@pi:~# i2cget -y 1 0x68 0
Error: Could not set address to 0x68: Device or resource busy

Can someone assist me in getting it back

ls -al /dev/rtc0
crw------- 1 root root 254, 0 Jan 1 1970 /dev/rtc0

root@pi:~# cat /sys/class/rtc/rtc0/date
cat: /sys/class/rtc/rtc0/date: Invalid argument

root@pi:~# cat /etc/modules
snd-bcm2835
i2c-bcm2708
i2c-dev
bcm2708_wdog
rtc-ds1307

root@pi:~# ls -al /sys/class/i2c-adapter/i2c-1/new_device
--w------- 1 root root 4096 Jan 1 1970 /sys/class/i2c-adapter/i2c-1/new_device


Chrs....

User avatar
Penthux
Posts: 79
Joined: Thu Oct 11, 2012 7:33 am
Location: United Kingdom

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

Thu Sep 26, 2013 4:44 pm

I got hold of a Chronodot from those brilliant buys at Phenoptix, which is a 'drop-in' for the DS1307 RTC but vastly more accurate. If you're thinking of adding an RTC to your Raspberry Pi then go for the Chronodot because it's not much more expensive and the timekeeping is so much more accurate. Reports say the accuracy is within +/- 2 seconds per year.

This is what I did to get a Chronodot v2.1 working under Raspbian Wheezy on the Raspberry Pi. It literally took 15 mins and no more.

1.Connect VCC connector to the 3.3V pin(1) of the RPi GPIO
2.Connect GND connector to the GND pin(6) of the RPi GPIO
3.Connect SDA connector to the SDA0 pin(3) of the RPi GPIO
4.Connect SCL connector to the SCL0 pin(5) of the RPi GPIO

Verify wiring by running the following (as 'root') at the command line, you should see ID #68 show up - that's the address of the DS3231:

Code: Select all

sudo i2cdetect -y 0 
** If you have a rev 2 Pi, you will have to run:

Code: Select all

sudo i2cdetect -y 1

Now, as 'root' user type the following:

Code: Select all

apt-get update && apt-get dist-upgrade 
rpi-update
If you need to reboot do it. Then, as 'root' user:

Code: Select all

apt-get install i2c-tools libi2c-dev
By default the Raspberry Pi Raspbian config doesn’t
load the I2C modules at boot. To change this you need to
comment out or delete the I2C entry from the following
file: /etc/modprobe.d/raspi-blacklist.conf

Code: Select all

blacklist spi-bcm2708
#blacklist i2c-bcm2708
(We hashed ours out instead of deleting the line)

Add the following lines to /etc/modules and save it (we're
using a wireless device and 8192cu is our module for it, or
otherwise this can be left out):

Code: Select all

snd-bcm2835
8192cu
i2c-bcm2708
i2c-dev
rtc-ds1307
Now type the following command:

Code: Select all

modprobe rtc-ds1307
Edit the /etc/rc.local file and add the following lines:

(for rev.1 RPi)

Code: Select all

echo ds3231 0x68 > /sys/class/i2c-adapter/i2c-0/new_device 
/sbin/hwclock -s
(for rev.2 RPi)

Code: Select all

echo ds3231 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
/sbin/hwclock -s
Remove the fake-hwclock with the following commands:

Code: Select all

update-rc.d -f  fake-hwclock remove
update-rc.d -f  ntp remove
Now reboot the Raspberry Pi. If all goes according to plan you will see the modules being loaded and DS3231 being detected.

Login and sudo su - to 'root' and type the following to see the hwclock's set time and date:

Code: Select all

hwclock -r
To set the timezone and date correctly type the following:
(NB: We're in England which is why we use Europe/London)

Code: Select all

rm -f /etc/localtime
ln -sf /usr/share/zoneinfo/Europe/London /etc/localtime
date DDMMhhmmYYYY
(DD = day | MM = month | hh = hour | mm = minutes | YYYY = year) * always use leading zero where needed!

Code: Select all

/sbin/hwclock --systohc
You can also install ntpdate to synchronise time initially:

Code: Select all

apt-get install ntpdate
Now use the following command to synchronise time and date:

Code: Select all

ntpdate 0.pool.ntp.org
That's what got my Chronodot working and now I'm setup for accurate time on my Raspberry Pi for at least the next 8 years. ;-)
Penthux
------------
Slackware ARM on a Raspberry Pi - SARPi
http://sarpi.co.uk
"Slackware ARM - it's not for NOOBS!"

lostgdi
Posts: 5
Joined: Sun Jul 28, 2013 1:48 pm

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

Sat Oct 05, 2013 4:41 am

Galaxynik wrote:playing with the hardware seems a bit drastic lol
I get the UU in device 0x68 when hwclock is working but

Code: Select all

echo 0x68 > /sys/class/i2c-adapter/i2c-1/delete_device
reverts it to 68 and allows i2c-tools to access it ......
thanks for your post , I am just looking for how to remove i2c device by bash.

User avatar
Penthux
Posts: 79
Joined: Thu Oct 11, 2012 7:33 am
Location: United Kingdom

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

Fri Oct 25, 2013 4:56 pm

I favour the ChronoDot v2.1 (DS3231SN) over other controllers. It's very easy to connect and install on your Raspberry Pi under Raspbian Wheezy. There's a nice HOW TO guide for Slackware ARM users here, for those who might be interested. ;)
Penthux
------------
Slackware ARM on a Raspberry Pi - SARPi
http://sarpi.co.uk
"Slackware ARM - it's not for NOOBS!"

piasawhole
Posts: 3
Joined: Sat Dec 07, 2013 10:11 am

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

Sat Dec 07, 2013 10:25 am

Hello all,

Recently ?I have been trying the RTC Code on a DS1307 based on the http://learn.adafruit.com/adding-a-real ... i?view=all

But right now, when I run sudo i2cdetect -y 0, i get the following output

Code: Select all

       0   1   2   3    4   5   6   7   8   9   a   b   c   d   e  f
00:                03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 
10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 
20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 
30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 
40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 
50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 
60: 60 61 62 63 64 65 66 67 UU 69 6a 6b 6c 6d 6e 6f 
70: 70 71 72 73 74 75 76 77 
I have been following this thread and tried out various methods as prescribed in this thread, but of no avail. Any help would be deeply appreciated

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

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

Sat Dec 07, 2013 10:29 am

piasawhole wrote:Hello all,

Recently ?I have been trying the RTC Code on a DS1307 based on the http://learn.adafruit.com/adding-a-real ... i?view=all

But right now, when I run sudo i2cdetect -y 0, i get the following output

Code: Select all

       0   1   2   3    4   5   6   7   8   9   a   b   c   d   e  f
00:                03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 
10: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 
20: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 
30: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 
40: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 
50: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 
60: 60 61 62 63 64 65 66 67 UU 69 6a 6b 6c 6d 6e 6f 
70: 70 71 72 73 74 75 76 77 
I have been following this thread and tried out various methods as prescribed in this thread, but of no avail. Any help would be deeply appreciated
So, just to confirm, by typing

Code: Select all

sudo i2cdetect -y 0
this implies you have a revision 1 pi, ie an early 256mb model B ?
Because otherwise you should be using

Code: Select all

sudo i2cdetect -y 1


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

piasawhole
Posts: 3
Joined: Sat Dec 07, 2013 10:11 am

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

Sat Dec 07, 2013 11:37 am

this implies you have a revision 1 pi, ie an early 256mb model B ?
That is correct Texy. I am using a 256 MB model B

User avatar
Penthux
Posts: 79
Joined: Thu Oct 11, 2012 7:33 am
Location: United Kingdom

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

Thu Dec 19, 2013 7:25 pm

piasawhole wrote:That is correct Texy. I am using a 256 MB model B
Have you made sure the connections to the GPIO are correct? I'm also seeing there's a driver using the 0x65 address on the I2C bus. It should be 0x68? The "UU" indicates a driver is using that 0x65 address and it cannot be probed.

As for the version of RPi you have... http://raspi.tv/2013/rpi-gpio-basics-2- ... n-you-have
Penthux
------------
Slackware ARM on a Raspberry Pi - SARPi
http://sarpi.co.uk
"Slackware ARM - it's not for NOOBS!"

piasawhole
Posts: 3
Joined: Sat Dec 07, 2013 10:11 am

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

Fri Dec 20, 2013 6:06 am

Have you made sure the connections to the GPIO are correct? I'm also seeing there's a driver using the 0x65 address on the I2C bus. It should be 0x68? The "UU" indicates a driver is using that 0x65 address and it cannot be probed.

As for the version of RPi you have... http://raspi.tv/2013/rpi-gpio-basics-2- ... n-you-have
Thanks for the reply Penthux. As for the 0x65 address, if you notice the alignment of the driver addresses in my previous post has been messed up. My apologies. The "UU" was present at the address 0x68.

I tried all the steps described in this thread and was still unable to get any positive response from the RTC.

I tried a new SD card (4 GB) with the Raspbian wheezy distro and it worked like a charm!! Still trying to figure out what could have gone wrong the first time.

Later found that the old SD card (16 GB) was physically damaged and the card stopped being detected. I had to press the SD card slot for the card to be detected. Don't know if this could have been a reason for the RTC to not work the first time around.

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

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

Fri Dec 27, 2013 7:18 pm

I am using a PCF8563 clock.. It seems to install but when I do
sudo echo 0x51 > /sys/class/i2c-adapter/i2c-1/delete_device-bash: /sys/class/i2c-adapter/i2c-1/delete_device: Permission denied
to remove the UU status I get permission denied.. I also can't do a new_device functon ... permission denied again

does anyone know why this is happening? or how to get around it .. gordon's c code pcf8563.c works fine but of course
it will not work when the status is UU from icdetect -y 1

sudo hwclock -r returns :
Wed 31 Dec 1969 19:25:17 EST -0.451877 seconds
which is wrong ..
one thing I was able to address is that hwclock.sh requires an \n at the end of the printf
to prevent this message:
hwclock: Warning: unrecognized third line in adjtime file
(Expected: `UTC' or `LOCAL' or nothing.)
my software level is
Linux raspberrypi4 3.10.24+ #614 PREEMPT Thu Dec 19 20:38:42 GMT 2013 armv6l GNU/Linux
thanks in advance for any assistance here ..

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

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

Sun Dec 29, 2013 12:51 pm

this is interesting in that I left the unit up and came back to it after an hour and the sudo hwclock -r or -s works fine...
I imagine there is some other process running that stopped perhaps. I still can't clear the UU status ..If I reboot the machine then the hwclock goes back to showing WED DEC 31 1969... for about an hour .. so it is repeatable .. will retrace install steps again..

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

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

Mon Dec 30, 2013 10:28 pm

after much trial and error I solved it for myself .. it might not work for someone else. I got into trouble
looking at another post which suggested putting the echo command into the /etc/default/hwclock
member .. instead I went over to the adafruit tutorial for the ds1307 and followed those directions.
it works fine for the pcf8563.

http://learn.adafruit.com/adding-a-real ... t-rtc-time

I also learned that some commands need to run with the user being root (sudo -s) rather than just
sudo and the command...(my permission denied problem)

to free up the port 0x51 and remove the UU status
sudo -s
echo 0x51 > /sys/class/i2c-adapter/i2c-1/delete_device

nifty
Posts: 1
Joined: Tue Feb 25, 2014 4:13 am

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

Tue Feb 25, 2014 4:45 am

I have successfully installed an Adafruit D1307 RTC on my RPi by following the instructions in the tutorial.
Thank you, the tutorial was very helpful, and it all went without a hitch.
Real time & date are now always correct, even if not connected to the Internet.

My problem, however, is:
I am developing an application in Python3 which requires real time & date.
I can obtain real time & date in my application easily enough using function 'localtime' in the the built-in module 'time'.

But I do not know how to set date & time from within my Python application.

The RPi running my Python application may or may not have access to the Internet, and in those cases where it doesn't, the user must be able to set the date & time from within the Python application (using a PiFace HMI) . The user will not have access to the os command line.

Any suggestions?

Bucky2090
Posts: 2
Joined: Wed Feb 26, 2014 10:27 pm

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

Wed Feb 26, 2014 10:33 pm

Thanks so much for this tutorial. This saved me a lot of time. I am building a security camera system and my Pi needs to keep very accurate time. The Chronodot is perfect for this project.

Bucky2090

User avatar
dentex
Posts: 180
Joined: Sun May 20, 2012 7:58 am
Location: Italy
Contact: Website

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

Wed Oct 01, 2014 1:51 pm

Hello,
I'll recall the initial issues from the beginning of the thread.
I also have a Waiman's Tiny RTC v1.1 (the white coloured one) and it's not clear reading the initial part of the thread which ones are the correct resistors to remove in order to have the module work OK with our RPi (Tiny RTC is for Arduino).
Does anyone have a definitive answer?
In fact, the R2 and R3 resistors seems to be the correct answer in regards to the blue version of the module, not the 1.1-white one.

Thanks.
One of my projects with the Raspberry Pi:
--------------------------------------------------------
(Raspberry) Pi Aquarium Controller: https://github.com/dentex/piac

Hi-Z
Posts: 29
Joined: Wed Jan 02, 2013 8:19 pm
Location: Canuckistan

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

Wed Oct 01, 2014 3:21 pm

Having multiple pull up resistors on the SDA & SCL lines isn't really an issue unless you have lots and lots of them (yielding lower overall parallel resistance resulting in greater current draw) or the power supplies are of different voltages (ie RPi at 3.3V and RTC at 5V)

In this case, it appears that the two different voltages may be present and thus you should be removing one of the pull up resistors.
link to wiki
http://www.dfrobot.com/wiki/index.php/R ... U:DFR0151)
link to schematic
http://www.dfrobot.com/image/data/DFR01 ... %20sch.pdf

According to the schematic, RA1 is used to provide the pull up resistors. Ideally you'd like to remove the connections from SDA & SCL by lifting the pins ... to make things difficult, the exact pins aren't noted so a meter to buzz out which ones they are will help.

IF YOU DO'T USE THE TEMPERATURE SENSOR OR SQUARE WAVE OUTPUT.... then removing RA1 completely will be the simplest and easiest thing to do. RA1 provides pull up resistors to those two outputs as well as the SDA & SCL

--------------------------------------------------

It should be noted that the RPi is 3.3V and the RTC module asks for 5V
- If you use 3.3V on the RTC then no resistors need to be removed
- If you use 5V on the RTC then resistors should be removed and additional components may be required to level shift the outputs from 5V to 3.3V

Personally, I run all external devices (RTC, GPS, gyroscope, accelerometer, magnetometer, relays, temperature, humidity sensors, etc) at 5V and level shift to 3.3V where needed .... the reason I do this is because I tap directly off the 5V supply as it has a greater current capacity instead of the 3.3V which is limited in maximum current available. If you only have one device attached then using the 3.3V supply is sufficient ... since I have many attachments, I can't.
Last edited by Hi-Z on Wed Oct 01, 2014 5:55 pm, edited 1 time in total.

User avatar
dentex
Posts: 180
Joined: Sun May 20, 2012 7:58 am
Location: Italy
Contact: Website

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

Wed Oct 01, 2014 3:51 pm

Hey @Hi-z,
Thank you very much for the insights.

So it seems I have two ways to go:

1) Since I'm going to use just this I2C device, I'll use the 3v3 GPIO pin to feed the module's VCC.
Considering also that I have a spare DS18B20 sensor to grab and that you say <<IF YOU DO'T USE THE TEMPERATURE SENSOR OR SQUARE WAVE OUTPUT then removing RA1 completely will be the simplest and easiest thing to do....>> I'll leave the RA1 in place.

2) On a project I'm making, I can easily access a 4v5 power rail, leaving the RPi completely out from powering the RTC module.
If I'm not missing something, in this scenario I would link the SDA pin to the Pi's pin 3 (GPIO 0), the SCL to n.5 (GPIO 1) and the DS to the n.7 (GPIO 4, 1-wire). VCC and GND would go to the 4v5 adapter's (+) and (-) rails. Is that right?

Could you please confirm if solution 1 and/or 2 would work?

Thanks again for the help.
One of my projects with the Raspberry Pi:
--------------------------------------------------------
(Raspberry) Pi Aquarium Controller: https://github.com/dentex/piac

Hi-Z
Posts: 29
Joined: Wed Jan 02, 2013 8:19 pm
Location: Canuckistan

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

Wed Oct 01, 2014 6:51 pm

1) The quickest & easiest solution

2) Ideally, you would use a i2c voltage level shifter between the RPi (3v3) and the RTC (4v5) when they are powered from different sources as you suggest .... hence the SDA & SCL of the RTC would not connect directly to the RPi as they would in scenario 1.

User avatar
dentex
Posts: 180
Joined: Sun May 20, 2012 7:58 am
Location: Italy
Contact: Website

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

Wed Oct 01, 2014 6:56 pm

Ok, as the lazy b****rd I am, I'll use the solution 1 ! :D
Nothing to solder, works out of the box... perfect.
And I'm still not familiar exactly with the "voltage lever shifter" concept. Still learning all this stuff.

Thanks man!
One of my projects with the Raspberry Pi:
--------------------------------------------------------
(Raspberry) Pi Aquarium Controller: https://github.com/dentex/piac

Hi-Z
Posts: 29
Joined: Wed Jan 02, 2013 8:19 pm
Location: Canuckistan

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

Wed Oct 01, 2014 8:12 pm

Pretend you're in a large room with many other people. You can talk to them and them to you. During the conversations you find out that you are actually on the 33rd floor of a building and rumour has it that there may be others on the hiding out on the 50th floor but there is no way to talk to them. Suddenly (or should I say magically) an elevator appears with only two buttons ... 33 & 50. Using your super genius skills, you get in, press 50 and you are now on the 50th floor with a whole new group of people ... After talking for a bit, you find out that they want to know what time it is on the 33rd floor (This is a RTC tutorial, so I will stay relevant). Since you are the only one who can figure out how the two buttons in the elevator work, you press 33 and arrive at that floor. You check the time, return to the elevator and press 50. On the 50th, you report your results and somebody says "I wonder what time it is now on the 33rd?" ... So back to the 33rd you go, check the time and return to the 50th .... and then somebody asks "How about now?" and the cycle continues.

In this instance the elevator (level shifter) allows the conversations on one level (50th floor) to communicate with the other level (33rd floor) ... without it, they can't talk with each other.

-----------------------------------------------------

The story gets a little weird after that ... the hero (that would be you) finds out that the people of the Ground level have no potential.

User avatar
dentex
Posts: 180
Joined: Sun May 20, 2012 7:58 am
Location: Italy
Contact: Website

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

Wed Oct 01, 2014 8:19 pm

Lol! My friend, now, this is truly brilliant... I swear, NOW I understand the concept, really.
Thanks.
One of my projects with the Raspberry Pi:
--------------------------------------------------------
(Raspberry) Pi Aquarium Controller: https://github.com/dentex/piac

User avatar
dentex
Posts: 180
Joined: Sun May 20, 2012 7:58 am
Location: Italy
Contact: Website

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

Wed Oct 01, 2014 8:26 pm

Hi-Z, now I can't stop myself from this bad habit of crosslinking you to the help request of mine at http://www.raspberrypi.org/forums/viewt ... 37&t=88271
:shock:
(Sorry Mods, feel free to delete if this brakes some rules)
One of my projects with the Raspberry Pi:
--------------------------------------------------------
(Raspberry) Pi Aquarium Controller: https://github.com/dentex/piac

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