How to use Real-Time-Clock (RTC) in BrainyPi

How to use Real-Time-Clock (RTC) in BrainyPi

This article describes how to use internal Real-Time Clock (RTC) on BrainyPi.

RTC on BrainyPi

BrainyPi uses the RK808 PMIC, which is a power management IC that includes an RTC module. The RTC module is responsible for keeping track of time, and it
can be accessed using the /dev/rtc0 device node in Rbian. The RTC module on the RK808 PMIC supports seconds, minutes, hours, days, months, and years.

Connecting a RTC battery connector to BrainyPi

To use the RTC function on BrainyPi, you need to connect a RTC battery connector to the board. The RTC battery connector is a 2-pin header that allows you to connect a CR2032 or similar coin cell battery to power the RTC circuit.

Here are the steps to connect the RTC battery connector to BrainyPi:

  1. Buy “CR2032 3V 220mAh Lithium Coin cell battery with 2pin connector”

  2. Locate the RTC battery connector on BrainyPi. It is a 2-pin header located near the HDMI port.

  3. Connect the Battery Connector as per the image below. Note: +ve terminal is near the HDMI port, connecting the battery terminals incorrectly may cause harm to BrainyPi.

  4. Once the battery is connected, the RTC will start keeping track of time.

Using RTC on BrainyPi

Now that you have connected the RTC battery connector to BrainyPi, you can start using the RTC function.

Steps to use RTC on BrainyPi:

We will use a combination of timedatectl and hwclock commands in Linux to set time using RTC

1. Read current time from RTC

You can use the following command to read the current time from the RTC module:

sudo hwclock -r

2. Update RTC time to system time

You can use the following command to set the system time to the time stored in the RTC module:

sudo hwclock -w

3. Update system time to the time stored in the RTC

You can use the following command to set the system time to the time stored in the RTC module:

sudo hwclock -s

Advanced RTC features in BrainyPi

Updating system time to RTC time automatically on boot up

  1. Open the /etc/rc.local file for editing:

    sudo nano /etc/rc.local
    
  2. Add the following line before the exit 0 line:

    /sbin/hwclock -s
    
  3. Save and close the file.

Now, when the system boots up, the hwclock -s command will update the system time to the time stored in the RTC module.

Using the alarm feature to wake up BrainyPi from Sleep

When in sleep mode, an RTC can be used to wake the system up.

To enable wake in 60 seconds from now, one can use this command

echo +60 | sudo tee /sys/class/rtc/rtc0/wakealarm

To trigger sleep in BrainyPi you can use the command

echo "mem" | sudo tee /sys/power/state