DS3231 Real Time Clock installation guide for RPi / /LE8

  • Hi everyone,

    There have been some questions in these forums about RTC support in LE, and there's various info on the web, some of which is confusing because it applies to Raspbian or to older versions of OE, so I thought it might not be a bad idea if I simply documented my procedure for installing a Real Time Clock in my Raspberry Pi with LibreElec 8. This worked for me, right off the bat.

    [Update, September 2024: This still applies to later LE versions, including LE12. Tested and found to be working on the RPi3 and RPi4. Note that the RPi5 has a RTC built in and that LE has support for same.]

    These instructions are for the DS3231 RTC modules that can be ordered for next to nothing from a variety of sources.

    1. Unplug your Pi and Install the RTC module onto the GPIO connector pins 1-4 as follows:

    DS3231.jpg

    2. Switch your RPi back on and SSH into it.

    3. Enable kernel support:

    3a. Remount LE system partition r/w: mount -o remount,rw /flash

    3b. Edit /flash/config.txt and add (somewhere near the end): dtoverlay=i2c-rtc,ds3231

    3c. Remount LE system partition r/o: mount -o remount,ro /flash

    3d. Reboot.

    4. Adjust the RTC's time:

    4a. Verify local system time with the 'date' command. If local time is off, either adjust it manually or connect your Pi to the Internet and wait for it to the the correct time from an NNTP server.

    4b. Transfer local time to RTC: hwclock -w

    4c. Verify RTC time: hwclock -r

    That's it. You're done. Your LibreElec RPi will now have the correct time every time it powers up. Don't worry about loading kernel modules, disabling fake clocks or other configuration changes. In LE8 you don't need them; whatever instructions you may have found referring to that sort of thing applies to other operating systems or older versions.

    Note: edits to /flash/config.txt should survive LE upgrades; edits to /flash/distroconfig.txt won't. Should you end up having to reinstall LE, don't forget to restore your edits to /flash/config.txt.

    Edited once, last by frankvw: Updated old thread to note that it still works in current LE versions. (September 6, 2024 at 6:43 AM).

  • Very nice "How To" - confirmed to work on LE9.

    If you feel confident, this could be added to our Wiki pages Here

    I'm happy to add it to the Wiki, but not sure where; none of the current categories seems to fit. The "RPi" categories seems the closest one but that only lists Pi specs at the moment. Suggestions welcome!!

  • I guess this never got added to the wiki :( This line needs to be changed for other chips:

    3b. Edit /flash/config.txt and add (somewhere near the end): dtoverlay=i2c-rtc,ds3231

    with

    Code
    dtoverlay=i2c-rtc,ds1307

    or

    Code
    dtoverlay=i2c-rtc,pcf8523

    Also, I'm not sure if this step is necessary:

    4. Adjust the RTC's time:

    4a. Verify local system time with the 'date' command. If local time is off, either adjust it manually or connect your Pi to the Internet and wait for it to the the correct time from an NNTP server.

    4b. Transfer local time to RTC: hwclock -w

    4c. Verify RTC time: hwclock -r

    I've just added a used ds1307 rtc and the date from hwclock -r was UTC, I'm on GMT, so an hour behind. Doing hwclock -w and then rebooting with no network resulted in it being an hour ahead for some reason! It's now sorted itself out though and keeps the correct time after losing power and no network.

    Now I just need to figure out a script to keep trying to reconnect the wifi if it's unavailable after power loss. Then my pi might just get itself back ship shape after a rare power cut!

  • Hi everyone

    Sorry to revive this post but I can't find a solution for my problem

    I've followed this guide to install a rtc in my raspberry pi with Libreeec, but it seems not to work properly. After the reboot without connection, the on screen clock is wrong, probably is showing the yesterday time but if I use hwclock -r the time is perfect.

    What should I do to solve this issue

    Thanks in advance

    PS. Sorry for my english

  • I've followed this guide to install a rtc in my raspberry pi with Libreeec, but it seems not to work properly. After the reboot without connection, the on screen clock is wrong, probably is showing the yesterday time but if I use hwclock -r the time is perfect.

    Use the date command to verify that the hardware clock updates the system clock. E.g. on my RPi:

    Code
    LibreELEC:~ # hwclock -r
    Fri Sep  6 05:03:48 2024  0.000000 seconds
    
    LibreELEC:~ # date
    Fri Sep  6 07:03:50 SAST 2024

    As you can see the two show the same time. The output of hwclock shows GMT while the output of date shows my local time (.I'm on South African Standard Time a.k.a. SAST which is GMT + 2 hours.)

    If this isn't the case on our system that means the hardware clock doesn't update the system clock correctly and we can look into why that is. If it is correct, there's something else wrong and we'll take it from there. So please post the output of the above commands on your system and let us know in what timezone you live, then we'll know more about what might be goin on.

    // FvW

  • For historical reasons hwclock default to local time. Use hwclock -u -r to use stored UTC time correctly.

    Eh? On my RPi3 with LE9 (soon to be replaced by an RPi4 with LE12) the output of hwclock -r comes back with UTC time, not local time (as per the output in my above post.) So why is that?


    // FvW

  • Thanks for the answer.. this is the result

    I live in Italy so GMT +1

    Code
    LibreELEC:~ # hwclock -r
    Fri Sep  6 18:44:09 2024  0.000000 seconds
    LibreELEC:~ # date
    Fri Sep  6 20:44:14 CEST 2024
  • the output of hwclock -r comes back with UTC time, not local time (as per the output in my above post.) So why is that?

    When hwclock was designed for PC Compatible the time was usually stored in the HW clock as local time (from Windows), Therefore default setting is assuming stored clock is local time.

    Linux is storing UTC in HW clock to be independent from local time setting and having a monotone clock. When using hwclock you have to add option -u or --utc.

    I live in Italy so GMT +1

    You are currently having Daylight Saving Time ending last Sunday in October, CEST = GMT+2. Try hwclock -u -r

  • I live in Italy so GMT +1

    Code
    LibreELEC:~ # hwclock -r
    Fri Sep  6 18:44:09 2024  0.000000 seconds
    LibreELEC:~ # date
    Fri Sep  6 20:44:14 CEST 2024

    I'm seeing one problem here right away. You're at GMT+1 but the difference between the hardware clock and the on-board clock is two hours, not one. So there's something wrong with you time settings. Daylight Savings Time seems the most likely candidate, but no DST is showing up in the output of the date command. So first let's found out more about that.

    As soon as you note that the on-screen clock is out of sync, note the time it shows and post it here. Then use the timedatectl command and post the output here. In my case the output is:

    Code
                          Local time: Sat 2024-09-07 09:49:48 SAST
                     Universal time: Sat 2024-09-07 07:49:48 UTC
                           RTC time: Sat 2024-09-07 07:49:49
                          Time zone: Africa/Johannesburg (SAST, +0200)
          System clock synchronized: yes
    systemd-timesyncd.service active: yes
                    RTC in local TZ: no

    I'm beginning to suspect that the problem might lie with the on-board clock on your RPi which for some reason appears to get out of sync, but at this point I don't have the details so this is more speculation than anything else. So once you note the time on the screen is off please provide the above details so we can home in on the cause of the problem.

    // FvW

  • Sorry for delay

    I've made both the tests and these are results

    Code
    LibreELEC:~ # hwclock -r
    Sat Sep  7 23:26:46 2024  0.000000 seconds
    
    LibreELEC:~ # hwclock -u -r
    Sun Sep  8 01:27:19 2024  0.000000 seconds
    
    
    LibreELEC:~ # timedatectl
    Failed to query server: The name org.freedesktop.timedate1 was not provided by any .service files

    I'm still not connected but I'm doing so on purpose because this pi will be not connected on internet

    After connection to internet these are new results

    Code
    LibreELEC:~ # hwclock -r
    Sat Sep  7 21:38:09 2024  0.000000 seconds
    
    LibreELEC:~ # hwclock -u -r
    Sat Sep  7 23:37:54 2024  0.000000 seconds
    
    LibreELEC:~ # timedatectl
    Failed to query server: The name org.freedesktop.timedate1 was not provided by any .service files
  • If the RTC ts having a battery you always should check the voltage.


    My experience of the following is from Generic, I do not owe any RPI.

    • RTC state can be shown with cat /proc/driver/rtc
    • Boot activity: journalctl -k |grep rtc_cmos (your rtc module name is definitely different):
    Code
    Sep 08 08:21:21 LibreELEC kernel: rtc_cmos 00:05: RTC can wake from S4
    Sep 08 08:21:21 LibreELEC kernel: rtc_cmos 00:05: registered as rtc0
    Sep 08 08:21:21 LibreELEC kernel: rtc_cmos 00:05: setting system clock to 2024-09-08T06:21:19 UTC (1725776479)
    Sep 08 08:21:21 LibreELEC kernel: rtc_cmos 00:05: alarms up to one month, y3k, 114 bytes nvram, hpet irqs
  • I've already checked the battery and it's ok, these are results and I don't undestand why I see ds1307, mine is ds3231 and in config.txt is correct

    Code
    LibreELEC:~ # cat /proc/driver/rtc
    rtc_time        : 11:00:31
    rtc_date        : 2024-09-08
    24hr            : yes
    
    Sep 08 12:43:58 LibreELEC kernel: rtc-ds1307 1-0068: registered as rtc0
    Sep 08 12:43:58 LibreELEC kernel: rtc-ds1307 1-0068: setting system clock to 2024-09-08T10:43:58 UTC (1725792238)
  • OK. So it looks like your hardware clocks might be the problem. As mglae already noted, the battery of your RTC might be flat. Get a new RTC; it's easier and cheaper than trying to replace the battery (which is soldered in).

    Not sure why you get an error from timedatectl; on my RPI3 and 4 it both works fine.