How to use the real-time clock functionality in Raspberry Pi 5 | #MagPiMonday

In the latest issue of The MagPi, Alasdair Allan shows you how to use the real-time clock (RTC) functionality in Raspberry Pi 5. Happy #MagPiMonday!

Raspberry Pi RTC Battery
The RTC battery features a lithium-manganese rechargeable battery. Use the J5 connector to attach the RTC battery to Raspberry Pi 5

Raspberry Pi 5 includes an RTC module. This can be battery powered via the J5 (BAT) connector on the board located to the right of the USB-C power connector.

You can set a wake alarm which will switch the board to a very low-power state (approximately 3mA). When the alarm time is reached, the board will power back on. This can be useful for periodic jobs like time-lapse imagery.

The J5 connection (at the bottom of the image) is used to connect the RTC battery to Raspberry Pi 5

To support the low-power mode for wake alarms you should edit the bootloader config:

$ sudo -E rpi-eeprom-config --edit

… adding the following two lines.

POWER_OFF_ON_HALT=1 
WAKE_ON_GPIO=0

Reboot Raspberry Pi so the updated EEPROM takes effect:

$ sudo reboot

You can test the functionality with:

 $ echo +600 | sudo tee /sys/class/rtc/rtc0/
wakealarm 
 $ sudo halt

That will halt the board into a very low-power state, then wake and restart after 10 minutes.

The RTC also provides the time on boot e.g. in dmesg:

 [    1.295799] rpi-rtc soc:rpi_rtc: setting 
system clock to 2023-08-16T15:58:50 UTC (1692201530)

…which may be useful if you don’t have an internet connection for getting time through NTP.

Adding a backup battery

The official battery part is a rechargeable lithium manganese coin cell, with a pre-fitted two-pin JST plug and an adhesive mounting pad. This is suitable for powering the Raspberry Pi 5 RTC when the main power supply for the board is disconnected, with a power-off current draw measuring in single-digit µA, giving a retention time that can be measured in months.

Edit the EEPROM configuration file to enable RTC support

Enabling trickle charging

Trickle charging of the battery is disabled by default. There are sysfs files that show the current trickle charging voltage and limits:

 /sys/devices/platform/soc/soc:rpi_rtc/rtc/
rtc0/charging_voltage:0 
 /sys/devices/platform/soc/soc:rpi_rtc/rtc/
rtc0/charging_
voltage_max:4400000 
 /sys/devices/
platform/soc/soc:rpi_rtc/
rtc/rtc0/charging_
voltage_min:1300000
The J5 socket may have a small plastic cover. Remove this to attach the RTC battery to Raspberry Pi 5

You can check these values with cat, for example:

 cat /sys/devices/platform/
soc/soc:rpi_rtc/rtc/rtc0/
charging_voltage

…returns: “0”, the current charging value. If you add rtc_bbat_vchg to /boot/firmware/config.txt using:

sudo nano /boot/firmware/config.txt

… and add:

dtparam=rtc_bbat_vchg=3000000

…and reboot. Use cat to investigate the values and you’ll see:

 /sys/devices/platform/soc/soc:rpi_rtc/rtc/
rtc0/charging_voltage:3000000
 /sys/devices/platform/soc/soc:rpi_rtc/rtc/
rtc0/charging_voltage_max:4400000
 /sys/devices/platform/soc/soc:rpi_rtc/rtc/
rtc0/charging_voltage_min:1300000

The battery will be trickle charging. Remove the dtparam line from config.txt to stop the trickle charging.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Server Status

Aradippou Chat Larnaca Nicosia

Chat Links

Official Links.

Chat.

Alternative Mirror Links.

1. KiwiIRC 1.
2. KiwiIRC 2.

Other Web Clients.

1. IrcCloud.

Recent Posts

Related Posts:

Archives

Follow me on Mastodon

Super Club Radio

Mighty Deals

CyIRC

CyIRC Tweets