dfries
Posts: 9
Joined: Mon Mar 27, 2017 1:32 am

RTC DS3231 setup on stretch

Sat Mar 31, 2018 7:29 pm

Raspberry Pi Real Time Clock setup instructions for Raspbian Stretch. This version adds a new udev rule instead of editing /lib/udev/hwclock-set that is replaced on system upgrade.

Hardware:
Connect GND, 3.3V VCC, SDA, SCL from the RTC to corresonding RPI pin header.

RPI configuration:
Add configuration to pick up the RTC, this is using the ds3231 RTC, use the correct value if you have a different RTC.

edit /boot/config.txt

Code: Select all

dtparam=i2c_arm=on
dtoverlay=i2c-rtc,ds3231
reboot to get access to the RTC and update with the system time

Code: Select all

hwclock --systohc
remove fake hwclock

Code: Select all

apt-get purge fake-hwclock
Create a new udev rule to set the clock (new file).

edit /etc/udev/rules.d/85-hwclock.rules

Code: Select all

# On the Raspberry Pi the RTC isn't available when systemd tries,
# set the time from RTC now when it is available.
KERNEL=="rtc0", RUN+="/sbin/hwclock --rtc=$root/$name --hctosys"
Notes:

Many instructions say to edit /lib/udev/hwclock-set and commit out an exit 0 line. The problem is you have to do that every time util-linux is upgraded. I'm adding a new udev rule in /etc where you only have to do it once.

Removing fake-hwclock is optional, it could be nice as a backup. I'm removing it to make it more obvious when the RTC isn't working. The system time starts at 2016-11-03, so if you see this time it is a good indication the RTC isn't working.

All commands must be run as root.

darkbibble
Posts: 629
Joined: Mon Mar 09, 2015 5:20 pm
Location: corby, england

Re: RTC DS3231 setup on stretch

Sun Apr 01, 2018 10:14 am

i folloed the instructions but its disabled my pitop speaker, im running latest stretch raspbian.
Q; How many Windows users does it take to fix a Linux problem??
A; Whats a Linux problem

dfries
Posts: 9
Joined: Mon Mar 27, 2017 1:32 am

Re: RTC DS3231 setup on stretch

Sun Apr 01, 2018 4:15 pm

darkbibble wrote:
Sun Apr 01, 2018 10:14 am
i folloed the instructions but its disabled my pitop speaker, im running latest stretch raspbian.
I don't have a pi-top speaker to test with, I read that it does use I2C, which is how the DS32131 is also connected. I2C uses addresses, as long as they have different address all devices should be still accessible. Start by commenting out the /boot/config.txt while the real time clock is still plugged in to see if your speaker works. If it does, try asking some pi-top forums for how to get both working at the same time.

WGB
Posts: 4
Joined: Mon May 21, 2018 4:59 pm

Re: RTC DS3231 setup on stretch

Mon May 21, 2018 5:02 pm

This a huge help, I had a heck of a time trying to figure out why it would stop syncing with the RTC periodically. The udev rule takes care of that perfectly.

jose2k72
Posts: 1
Joined: Thu May 24, 2018 8:50 pm

Re: RTC DS3231 setup on stretch

Thu May 24, 2018 9:05 pm

I have followed the instructions but I receive an error saying that I can not access the hardware

Code: Select all

sudo hwclock --systohc
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --debug option to see the details of our search for an access method.

Code: Select all

sudo hwclock --systohc --debug
hwclock from util-linux 2.29.2
hwclock: cannot open /dev/rtc: No such file or directory
No usable clock interface found.
hwclock: Cannot access the Hardware Clock via any known method.
I also have another sensor, but I do not think they are in conflict because it is in a different address, 0x76. This is the result of the i2cdetect execution

Code: Select all

 i2cdetect -y 1
     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: -- -- -- -- -- -- 76 --
Any suggestions? I'm missing something to do or configure?

UPDATE:
============================================================
At the end of the day I could not start this with the strech version and I had to install a jessie version.
I'm not sure where I read it, but somewhere I found someone saying that strech had some kind of problem with the i2c. Or rather the most current kernels are those who have this problem.
The truth is that in my project I use 2 i2c devices and with neither of the two I could communicate until I realized the installation of the jessie version.
If anyone has any idea why it happens and maybe how to solve this issues, please publish it, because until now I have not found any answer, good or bad.
Last edited by jose2k72 on Thu Jun 07, 2018 7:21 pm, edited 1 time in total.

dfries
Posts: 9
Joined: Mon Mar 27, 2017 1:32 am

Re: RTC DS3231 setup on stretch

Sat May 26, 2018 12:19 am

Thanks for that, i2cdetect. I have UU instead of 68 for i2cdetect. UU means a driver has claimed that address, check /boot/config.txt, which also requires a reboot.

WGB
Posts: 4
Joined: Mon May 21, 2018 4:59 pm

Re: RTC DS3231 setup on stretch

Sun May 27, 2018 4:04 pm

@dfries Odd and quite possibly unrelated question, but do you happen to know how to disable the "11 minute" rtc kernel time sync mode? I'd rather have the RTC only updated from the system clock at shutdown, but I'm not sure it's possible...

dfries
Posts: 9
Joined: Mon Mar 27, 2017 1:32 am

Re: RTC DS3231 setup on stretch

Thu Jun 07, 2018 3:44 pm

@WGB Here's a link I found, just curious why would you want to disable keeping the RTC in sync except on a clean shutdown?
https://unix.stackexchange.com/question ... ernel-mode

WGB
Posts: 4
Joined: Mon May 21, 2018 4:59 pm

Re: RTC DS3231 setup on stretch

Sat Jun 09, 2018 12:13 am

dfries wrote:
Thu Jun 07, 2018 3:44 pm
@WGB Here's a link I found, just curious why would you want to disable keeping the RTC in sync except on a clean shutdown?
https://unix.stackexchange.com/question ... ernel-mode
Thank you!

I'm interested in disabling the 11 minute sync mode because it seems to cause odd corruption of the RTC's stored time value every once in a while. Since the DS3231 seems to drift very little, I'm not too worried about only synchronizing it once every few weeks. It's not a big issue either way, but if I can at least reduce the sync frequency I'd be happy. Overall I'm already up and running quite well thanks to your original instructions!

Axilot
Posts: 46
Joined: Fri Jan 25, 2013 2:57 pm

Re: RTC DS3231 setup on stretch

Thu Nov 29, 2018 12:26 pm

Thank you!
I followed other guides but this is the only one that work and it is the easiest way too.
It works on Ubuntu Mate 16.04 also!

UF_DoC
Posts: 54
Joined: Wed Jul 01, 2015 9:00 am

Re: RTC DS3231 setup on stretch

Fri Mar 08, 2019 2:54 pm

Thanks dfries,

Im curious why you needed to specify the --rtc parameter in the udev file and How is $root/$name variables defined?

dfries
Posts: 9
Joined: Mon Mar 27, 2017 1:32 am

Re: RTC DS3231 setup on stretch

Wed Mar 13, 2019 12:49 pm

If there were multiple real time clocks specifying --rtc makes sure the one detected is the one read. It is a good idea to specify it.
udev defines $root/$name Replace the line with the following gives "root /dev name rtc0" or /dev/rtc0 once they are put together.

Code: Select all

KERNEL=="rtc0", RUN+="/bin/bash -c '/bin/echo root $root name $name >> /tmp/rtc.txt'"

wilsonb
Posts: 14
Joined: Thu Sep 11, 2014 4:13 pm

Re: RTC DS3231 setup on stretch

Mon Mar 18, 2019 7:25 pm

Hi,

Using a Pimironi Pixel 4" LCD w/ RPI 3 B+
Was wondering if it was possible at boot up to first enable the pins for ds3231 and read time. Then disable and use pins for LCD instead after clock was set.

dtoverlay=i2c-rtc,ds3231

dfries
Posts: 9
Joined: Mon Mar 27, 2017 1:32 am

Re: RTC DS3231 setup on stretch

Tue Mar 19, 2019 12:17 am

https://learn.pimoroni.com/tutorial/san ... perpixel-4

If that's what you are talking about, it says i2c is broken out on the back of the display that you could connect the RTC, no need to try to disconnect it.

alexthecamel
Posts: 1
Joined: Fri May 24, 2019 12:10 am

Re: RTC DS3231 setup on stretch

Fri May 24, 2019 12:17 am

Hi,

I've followed these instructions exactly on a clean install of Stretch Lite, 'sudo hwclock' gives the correct time and date, as does 'date' .

When I reboot and check via 'sudo journalctl -b' I still have the default Nov 03 17:16:43 on all journal entries until after /boot is mounted.

Is this expected behaviour? Even with a RTC installed those early boot logs will always be incorrect or can that be fixed?

Many Thanks

dfries
Posts: 9
Joined: Mon Mar 27, 2017 1:32 am

Re: RTC DS3231 setup on stretch

Sun May 26, 2019 10:37 pm

`journalctl -b` also starts out with Nov 3, for me. Unfortunately the way things are setup right now, the module and the hardware real time clock isn't available early enough in boot to load the current time, that might require a kernel with the rtc module and device tree entries built in.

As I had noted in the instructions removing fake-hwclock is optional. If having better times early on in the logs is important to you, install that package again. By default it will write the current time to storage every hour, so the times you see could be an hour and however long the system was powered off old, but it won't start back in November 3.

dfries
Posts: 9
Joined: Mon Mar 27, 2017 1:32 am

Re: RTC DS3231 setup on stretch

Mon Jun 03, 2019 12:30 pm

TerryMulhern wrote:
Mon Jun 03, 2019 6:08 am
Thank you for the setup and the source. Have you tried to run NTPD in this environment? Or perhaps anyone here who successfully managed that?
I have ntpd running on all my RPI systems, with or without a hardware RTC.

rudiratlos
Posts: 246
Joined: Tue May 01, 2012 8:47 am
Location: Germany (old europe)

Re: RTC DS3231 setup on stretch

Wed Nov 27, 2019 5:03 pm

according to this discussion https://bugs.debian.org/cgi-bin/bugrepo ... bug=855203 I've setup my ds3231 as follows:

add this single entry to /boot/config.sys

Code: Select all

dtoverlay=i2c-rtc,ds3231
create the file /etc/systemd/system/hwclock-start.service

Code: Select all

[Unit]
Description=hwclock-start to read rtc and write to system clock
After=sysinit.target

[Service]
Type=oneshot
ExecStart=/sbin/hwclock --hctosys --utc

[Install]
WantedBy=basic.target

Code: Select all

# cd /etc/systemd/system
# chmod 644 hwclock-start.service
# chown root:root hwclock-start.service

# systemctl daemon-reload
# systemctl enable hwclock-start.service
# systemctl start hwclock-start.service
that's it

nothing to edit more

checks:

Code: Select all

# timedatectl status

               Local time: Wed 2019-11-27 16:57:12 UTC
           Universal time: Wed 2019-11-27 16:57:12 UTC
                 RTC time: Wed 2019-11-27 16:57:12
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

Code: Select all

# service hwclock-start status

● hwclock-start.service - hwclock-start to read rtc and write to system clock
   Loaded: loaded (/etc/systemd/system/hwclock-start.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Wed 2019-11-27 16:37:22 UTC; 18min ago
  Process: 391 ExecStart=/sbin/hwclock --hctosys --utc (code=exited, status=0/SUCCESS)
 Main PID: 391 (code=exited, status=0/SUCCESS)
Nov 27 16:36:54 pump systemd[1]: Starting hwclock-start to read rtc and write to system clock...
Nov 27 16:37:22 pump systemd[1]: hwclock-start.service: Succeeded.
Nov 27 16:37:22 pump systemd[1]: Started hwclock-start to read rtc and write to system clock.

Code: Select all

# journalctl -b | grep clock

Nov 27 16:36:46 pump systemd[1]: System time before build time, advancing clock.
Nov 27 16:36:46 pump fake-hwclock[106]: Wed 27 Nov 16:36:46 UTC 2019
Nov 27 16:36:54 pump systemd[1]: Starting hwclock-start to read rtc and write to system clock...
Nov 27 16:37:22 pump systemd[1]: hwclock-start.service: Succeeded.
Nov 27 16:37:22 pump systemd[1]: Started hwclock-start to read rtc and write to system clock.
you can see that this setup is compatible with fake-hwclock.

Return to “General discussion”