Skip to content

How-To RTC

Description

The Pi-Tron CM4 has a built-in RTC module, type RV-3028, for time keeping when the device is power off. In order for the RTC to keep the time when there is no power a backup battery is needed. It is recommended to install the backup battery before activating the RTC in the Raspberry Pi OS.

At present the Raspberry Pi OS has a RV-3028 Kernel module already onboard, all that is needed to activate the RTC is to add an overlay entry to the config.txt.

pitron_cm4_rtc_bat

Requirements

  • 24 volts power supply with 2 pin power plug for the Pi-Tron CM4.
  • Development computer with network access to the Pi-Tron CM4 or alternatively connect an HDMI monitor, mouse and keyboard.
  • To login into the Pi-Tron CM4 via network, it is requires that the SSH server is running.
  • The login details are by default: username: pi and password: raspberry
    • Note: Newer Raspberry Pi OS versions require the user to create their own login credentials during first boot. See this blog post.
  • The RTC backup battery, type CR2023 with Molex wire connector, must be attached. Click for example.
  • An active Internet connection to install additional software and to get the current time and date.

How-To

  • Power on the Pi-Tron CM4 and let it boot.
  • Log in into the Pi-Tron CM4 and remove the fake-hwclock module:

    sudo apt remove fake-hwclock
    sudo update-rc.d -f fake-hwclock remove
    
  • Edit the hwclock-set file to always set the systems time and date at boot

    sudo nano /lib/udev/hwclock-set
    
  • and comment out these three lines at the top by adding a # (hash) sign in front of each line:

    #if [ -e /run/systemd/system ] ; then
    # exit 0
    #fi
    
  • Edit the config.txt (Buster and Bullseye)

    sudo nano /boot/config.txt
    
  • Edit the config.txt (Bookworm and later)

    sudo nano /boot/firmware/config.txt
    
  • and at the end of the file add the following overlay entry:

    dtoverlay=i2c-rtc,rv3028
    
  • Save the changes and leave the editor with Ctrl+O, Enter and Ctrl+X.

  • Now enable the I2C 1 bus using the raspi-config program:

    sudo raspi-config
    
  • Then choose: Interface OptionsI2C<Yes><Ok>

  • Back in the main menu, press the Tab key until <Finish> is selected and hit Enter to leave the raspi-config program.

raspi-config-main

raspi-config-interfaces

raspi-config-i2v-enable

raspi-config-i2c-enabled-ok

  • To be able to see devices on the I2C 1 bus, we use the program i2cdetect from the i2c-tools software package. Install the I2C programs by running:

    sudo apt update
    sudo apt install i2c-tools
    
  • Reboot the Pi-Tron CM4 to enable the I2C 1 bus.

    sudo reboot
    
  • After the reboot, log back in into the Pi-Tron CM4 and run the i2cdetect program to see if the RTC is present at address 0x52.

    sudo i2cdetect -y 1
    
  • The result should look something like the following table:

        0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:                        -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: 50 -- UU -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
    
    • Note: In place of the address 0x52 you see 2 U's, which means this address is in use by a driver, in this case the RTC driver for the RV-3028. This is correct since we added the overlay to the config.txt file earlier.
  • To further check that the RTC is working, run the following command which should produce one line which says that an rtc-rv3028 module was registered as rtc0.

    pi@raspberry:~ $ dmesg | grep rtc0
    rtc-rv3028 1-0052: registered as rtc0
    
  • Another check to perform is to run the timedatectl program to see all clocks on the device and how they are currently set.

    pi@raspberry:~ $ timedatectl
    
                    Local time: Thu 2022-01-01 10:00:00 CEST
                Universal time: Thu 2022-01-01 09:00:00 UTC
                      RTC time: Thu 2022-01-01 09:00:00
                     Time zone: Europe/Berlin (CET, +0100)
     System clock synchronized: no
                   NTP service: active
               RTC in local TZ: no
    
    • Note: If the line RTC time is empty, then the RTC is not active or somewhere an error occured. Check the Kernel log to see if there was any problem loading the RTC Kernel module.
    • Note 2: If timedatectl produces an error claiming that the RTC could not be read, this means no time and date was ever written to the RTC after the backup battery was installed. Have look at the next steps.
  • If the time and date of the RTC are incorrect or it is not possible to read from the RTC, setting time and date of the RTC can be done using the date and hwclock programs. First set the system time using date (the format below is: yyyy-mm-dd hh:mm:ss).

     sudo date -s "2022-01-01 10:00:00"
    
    • Note: The time and date setting also relies on the timezone setting. To get the correct time for your location, use the raspi-config program to adjust the timezone setting in the Raspberry Pi OS. After that set time and date again.
  • Verify the system time and date by simply entering date without any parameters.

    pi@raspberry:~ $ date
    Sat  1 Jan 10:00:00 CET 2022
    
  • Now that the system time and date are correct they can be written to the RTC using the hwclock program.

    sudo hwclock -w
    
  • Now try the timedatectl command again and the list should now show the correct local time and date as well as the UTC time/date which again should be the same as is stored in the RTC.

  • At this point another reboot of the system is recommended to check that time and date are indeed restored from the RTC at boot.
  • After the reboot check the Kernel log. The following command should produce 2 lines, where latter states that the system clock was set by the RTC.
    pi@raspberry:~ $ dmesg | grep rtc-rv3028
    rtc-rv3028 1-0052: registered as rtc0
    rtc-rv3028 1-0052: setting system clock to 2022-01-01T09:00:00 UTC (1641027600)
    


This concludes the How-to for the RTC on the Pi-Tron CM4. The RTC should now be operational and the system clock is always restored at boot. The backup battery insures that the RTC keeps the time when the Pi-Tron CM4 is powered off or disconnected from power.

Restrictions

  • No known Restrictions