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](https://cyirc.org/wp-content/uploads/2024/02/how-to-use-the-real-time-clock-functionality-in-raspberry-pi-5-magpimonday_65ca174c8ef47.webp)
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.
![](https://cyirc.org/wp-content/uploads/2024/02/how-to-use-the-real-time-clock-functionality-in-raspberry-pi-5-magpimonday_65ca174d008dd.png)
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.
![](https://cyirc.org/wp-content/uploads/2024/02/how-to-use-the-real-time-clock-functionality-in-raspberry-pi-5-magpimonday_65ca174daf952.png)
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
![](https://cyirc.org/wp-content/uploads/2024/02/how-to-use-the-real-time-clock-functionality-in-raspberry-pi-5-magpimonday_65ca174ee1021.png)
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.