jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Using an I2C clock (DS1307, DS3231) on latest raspbian

Sun May 08, 2016 9:40 pm

There are many tutorials for how to turn on a hardware I2C RTC clock for the Raspberry Pi, so it remembers the time even if it's not connected to a network (and also so that log timestamps from booting are more correct than the late arrivals of network time.)

All of them are bad or wrong in some way, now that jessie is the standard distribution, because jessie uses systemd.

I created a simple service, and install script, that just Does The Right Thing and makes the RTC clock work on boot. The bonus is that it does this without editing the system hwclock.sh script (which is obsolete and not actually used in raspbian systemd!)

https://watte.net/slam-clock-1.0.1.tgz

README:
slam-clock cuts through all the misleading and outdated tutorials, and makes
a hardware real-time clock work on a Raspberry Pi, from early on in the boot
process.
It requires raspbian from March 2016 or newer, because it uses systemd, and
does not edit or replace the "hwclock" scripts (which are largely obsolete.)

To install:

1) Uncompress/tar the archive (you probably already did this, as you're reading
this file)

2) "cd" to the directory that contains these files in a Terminal window.

3) Run "slam-clock.install.sh as root: sudo ./slam-clock.install.sh

This should save the current clock, make necessary adjustments to boot files,
and both enable the hardware clock for future reboots as well as enable it right
now.

Comments can be sent to slam-clock@mindcontrol.org
Also available on GitHub: https://github.com/jwatte/slam-clock

observing
Posts: 52
Joined: Mon Feb 27, 2012 12:18 pm

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Fri May 13, 2016 1:35 pm

I tried your tutorial and ran into a problem. After the Pi boots up, I see these messages in syslog:
Dec 31 19:00:17 zwhome slam-clock.sh[60]: ls: cannot access /sys/bus/i2c/devices/i2c-*: No such file or directory
Dec 31 19:00:17 zwhome slam-clock.sh[60]: hwclock: Cannot access the Hardware Clock via any known method.
Dec 31 19:00:17 zwhome slam-clock.sh[60]: hwclock: Use the --debug option to see the details of our search for an access method.

In /sys/bus/i2c/devices, there is a device named i2c-1, but it appears that the device isn't there when the script gets to it. If I manually start the service after bootup, the script succeeds and the time is transferred from the rtc to the Pi. I'll try to do some more troubleshooting as I get the time (no pun intended).

DirkS
Posts: 10362
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Fri May 13, 2016 1:45 pm

The script probably doesn't work with the current Raspbian Jessie and kernel version (4.4.x).
The best way to set up a ds1307 is to use

Code: Select all

dtoverlay=i2c-rtc,ds1307
in /boot/config.txt and remove all but i2c-dev from /etc/modules

blaablaaguy
Posts: 623
Joined: Sun Sep 27, 2015 3:26 pm

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Fri May 13, 2016 3:29 pm

Thanks it worked. I think any way. I tried it, set the time rebooted a few times and the time stayed right. But then it changed one hour behind, might have been coz i plugged in my wifi dongle. Which does raspbian favour, the clock or the internet time?
This signature intentionally left blank.

observing
Posts: 52
Joined: Mon Feb 27, 2012 12:18 pm

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Fri May 13, 2016 6:55 pm

@DirkS: Thanks, but I still get the same error messages in syslog. I'm assuming you meant to replace the statement "dtparam=i2c_arm=on" with "dtoverlay=i2c-rtc,ds1307" in /boot/config.txt.

@blaablaaguy: Could you check your /var/log/syslog and see if there are messages about /sys/bus/i2c/devices/i2c-* similar to the ones in my previous post? Thanks.

DirkS
Posts: 10362
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Sat May 14, 2016 4:51 pm

observing wrote: I'm assuming you meant to replace the statement "dtparam=i2c_arm=on" with "dtoverlay=i2c-rtc,ds1307" in /boot/config.txt.
No, it replaces the entry in /etc/modules.
To set up the rtc this should work:
* remove any lines with 'rtc' in /etc/modules
* make sure the script from the OP is disabled

* enable i2c with 'sudo raspi-config). This may be optional, but I'm not sure
* add to /boot/config.txt:

Code: Select all

dtoverlay=i2c-rtc,ds1307
* Comment out these lines in /lib/udev/hwclock-set

Code: Select all

if [ -e /run/systemd/system ] ; then
 exit 0
fi
by putting a # in front of them:

Code: Select all

#if [ -e /run/systemd/system ] ; then
# exit 0
#fi
* reboot
* write current system time to rtc:

Code: Select all

sudo hwclock -w
* To check rtc time:

Code: Select all

sudo hwclock -r
* purging fake-hwclock is optional

blaablaaguy
Posts: 623
Joined: Sun Sep 27, 2015 3:26 pm

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Sat May 14, 2016 6:28 pm

blaablaaguy wrote:Thanks it worked. I think any way. I tried it, set the time rebooted a few times and the time stayed right. But then it changed one hour behind, might have been coz i plugged in my wifi dongle. Which does raspbian favour, the clock or the internet time?
Nevermind i had wrong timezone
This signature intentionally left blank.

observing
Posts: 52
Joined: Mon Feb 27, 2012 12:18 pm

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Tue May 17, 2016 4:30 am

Just to close this thread, jwatte updated slam-clock a couple of days ago and the tutorial now works (for me, at least) as advertised. This gives me the opportunity to thank him for a neat and concise method for installing rtc support.

DirkS
Posts: 10362
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Tue May 17, 2016 4:38 am

observing wrote:Just to close this thread, jwatte updated slam-clock a couple of days ago and the tutorial now works (for me, at least) as advertised. This gives me the opportunity to thank him for a neat and concise method for installing rtc support.
It adds redundant lines to /boot/config.txt and it still loads modules in /etc/modules that are not necessary (and can ultimately break things)
So I would not recommend using the script in its current state.

paulohaacke
Posts: 1
Joined: Fri Feb 13, 2015 7:20 pm

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Wed Jul 27, 2016 4:43 pm

DirkS wrote:
observing wrote:Just to close this thread, jwatte updated slam-clock a couple of days ago and the tutorial now works (for me, at least) as advertised. This gives me the opportunity to thank him for a neat and concise method for installing rtc support.
It adds redundant lines to /boot/config.txt and it still loads modules in /etc/modules that are not necessary (and can ultimately break things)
So I would not recommend using the script in its current state.
He is checcking for the existence of /dev/rtc0 before loading the modules, so its more of a fail check kind of thing, no redundancy from my point of view. And it has an actual use case, what would happen if you run the install script and start the service without rebooting the system.

DirkS
Posts: 10362
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Wed Jul 27, 2016 10:15 pm

paulohaacke wrote:no redundancy from my point of view. And it has an actual use case,
The redundancy is in using both i2c1 and i2c_arm in config.txt. On all but the very early models Pi these have the same function.
Then also loading i2c_bcm2708 manually in /etc/modules while it's already taken care of by enabling i2c in config.txt

The recommended way is to use device tree. Easier to set up; no need for a script to initialise the rtc, etc.

rubensx
Posts: 3
Joined: Sun Jul 20, 2014 2:26 am

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Thu Jul 28, 2016 11:42 pm

please I need Help...

I'm at a Tyny i2C RTC and still get the code

Code: Select all


~$ sudo hwclock --debug
hwclock from util-linux 2.25.2
hwclock: cannot open /dev/rtc: No such file or directory
No usable clock interface found.
hwclock: Cannot access the Hardware Clock via any konw method.

my RPI is a rpi 2 with jessie and still can't put to run the hardware clock... maybe a good soul can help? =(

User avatar
davidcoton
Posts: 5026
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Fri Jul 29, 2016 8:49 am

rubensx wrote:please I need Help...

I'm at a Tyny i2C RTC and still get the code

Code: Select all


~$ sudo hwclock --debug
hwclock from util-linux 2.25.2
hwclock: cannot open /dev/rtc: No such file or directory
No usable clock interface found.
hwclock: Cannot access the Hardware Clock via any konw method.

my RPI is a rpi 2 with jessie and still can't put to run the hardware clock... maybe a good soul can help? =(
Which guide are you following?
What did you do to install hardware and software?
Signature retired

rubensx
Posts: 3
Joined: Sun Jul 20, 2014 2:26 am

Re: Using an I2C clock (DS1307, DS3231) on latest raspbian

Fri Jul 29, 2016 4:38 pm

I'm follow first a old guide path the i2ctools and later I see the new way but the 2 ways not work, my entire hardware:

Pi 2 + jessie
Tiny I2C Real time clock DS1307

My problem is not create a rtc in dev...

Return to “General discussion”