RTC
This section introduces how to use the RTC function on the Raspberry Pi, laying the foundation for subsequent project development.
info
To use the full RTC functionality, please connect the RTC battery.
RTC Key Notes
- The RTC device on the Raspberry Pi is
/dev/rtc0 - When the RTC device is connected and functioning correctly, if the Raspberry Pi automatically synchronizes time over the network after connecting, then the RTC time will be updated after the automatic network time synchronization
- Newer versions do not have the
hwclockrelated commands installed by default. Please install the necessary tools:sudo apt install -y util-linux-extra
RTC Operations
- The following tests use the
hwclocktool
Get RTC Time
- If running
hwclockfails, refer to RTC Key Notes and execute the installation command - View RTC time
sudo hwclock -r - Read the time from a specified RTC device (
-f: followed by the selected device,/dev/rtc0: specifies the RTC device to operate on)sudo hwclock -f /dev/rtc0 -r
Set RTC Time
- If running
hwclockfails, refer to RTC Key Notes and execute the installation command - Set the RTC clock time:
sudo hwclock --set --date="3/7/2024 16:55:05"
Synchronize RTC Clock
- If running
hwclockfails, refer to RTC Key Notes and execute the installation command - Synchronize the RTC time with the current system time (set the RTC time to the current system time)
sudo hwclock -w - Synchronize the system time with the current RTC time (set the system time to the current RTC time)
sudo hwclock -s
Scheduled Shutdown
- Use the
shutdowncommand to automatically shut down the system after a specified time. - Note:
- Shutdown after 10 minutes
sudo shutdown -h +10 - Automatically shutdown at 23:30 (Note: before this operation, please verify that the current time is correct and whether time resynchronization is needed)
sudo shutdown -h 23:30
Scheduled Power-on - Low Power Wake-up
warning
To enable this feature, an RTC battery must be connected to ensure the RTC continues functioning even when external power is disconnected.
- Enter the EEPROM configuration
sudo -E rpi-eeprom-config --edit - Add the following content at the end of the configuration file to enable the scheduled wake-up function:
POWER_OFF_ON_HALT=1
WAKE_ON_GPIO=0 - After modification, reboot the Raspberry Pi for the changes to take effect:
sudo reboot - You can test the feature using the following method (Effect: after configuration, shut down the system, and the internal RTC will automatically wake up and restart the device after 10 minutes.)
- Set a wake-up alarm 600 seconds (10 minutes) from the current moment:
echo +600 | sudo tee /sys/class/rtc/rtc0/wakealarm - Execute shutdown and wait for the automatic power-on at the scheduled time:
sudo halt # or sudo poweroff
- Set a wake-up alarm 600 seconds (10 minutes) from the current moment:
RTC Battery Charging
warning
This operation can only be performed when a rechargeable RTC battery is connected.
- Open the config.txt file (choose the correct path based on your system version):
sudo nano /boot/config.txt # For some older system versionssudo nano /boot/firmware/config.txt # For most newer system versions - Add the charging configuration to config.txt (where 3000000 indicates the maximum voltage: charging stops when the voltage reaches 3V; trickle charging starts when the voltage drops below 3V):
dtparam=rtc_bbat_vchg=3000000