Makerverse Real Time Clock with Supercapacitor Backup - Raspberry Pi Guide

Updated 01 March 2022

Introduction

This guide covers the configuration steps required to use the Makerverse Supercap Real Time Clock module as a backup RTC on a Raspberry Pi. This is required for remote applications when Internet access is impractical and accurate time keeping is desired through power failures or reboots. The RTC's 100mF supercapacitor is able to keep the clock ticking for up to 7 days without external power and removes the need to check and maintain RTC backup batteries.

Prerequesites

The Makerverse Supercap Real Time Clock uses the RV3028 RTC chip, support for which was added to the mainline Linux kernel in early 2019 with Raspberry Pi OS upgrading to a compatible kernel in August 2020 (with kernel version 5.4.51).

At the time of writing the NOOBS SD card ships with kernel 5.10.17 and is therefore fully compatible with the Makerverse Supercap Real Time Clock without any updates.

To double-check that you are running a compatible kernel version you can execute uname -r in a terminal and ensure that the number printed is above or equal to 5.4.51. If it isn't then a system update will be required.

For many of the steps in this guide GUI configuration is unavailable or impractical so it makes heavy use of the command line. The ctrl+alt+t keyboard shortcut will open a terminal on the desktop, or you can connect to your Raspberry Pi via SSH. Some basic command line experience is helpful, but not required.

Note that on the Raspberry Pi a copy+paste can be achieved by first highlighting the text to copy and then middle clicking to paste.

Contents

Raspberry Pi I2C Configuration

The RV3028 (and most RTC devices) use I2C for communication. If I2C has not previously been used on your Raspberry Pi it can be enabled through raspi-config. The steps for doing so are as follows:

  1. Open raspi-config as root:
    sudo raspi-config
  2. Use the arrow keys to select Interface Options and press enter:
    makerverse-supercap-real-time-clock-raspi-config-1
  3. Arrow down to I2C and press enter:
    makerverse-supercap-real-time-clock-raspi-config-2
  4. Arrow left or right to select <Yes> and press enter:
    makerverse-supercap-real-time-clock-raspi-config-3
  5. Press enter to get past the confirmation dialog:
    makerverse-supercap-real-time-clock-raspi-config-4
  6. Back at the main menu, press the right arrow twice to select <Finish> and press enter to exit back to the command line.
  7. With I2C enabled, run the following command to install the required I2C tools:

    sudo apt install python3-smbus i2c-tools
  8. To ensure that all changes take effect it is best to now reboot by running:
    sudo reboot

Wiring Guide

Connect the RV3028 module to the Raspberry Pi as-per the image below. The Rasbperry Pi's pin header is being viewed with the Ethernet & USB ports facing downwards.

It is important to note that the Vcc pin must connect to a 3.3V pin on the Raspberry Pi. The RV3028 is rated to 5V but the I2C inputs on the Raspberry Pi are not.

Directly below the 3.3V pin on the Raspberry Pi header are the required SDA and SCL pins, followed by a convinient ground pin two pins under that.

makerverse-supercap-real-time-clock-raspberry-pi-wiring

To confirm that the RV3028 board has been wired correctly run i2c-detect -y 1 and ensure that 52 appears as shown below (any other I2C devices should also show up):

pi@raspberrypi:~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- 52 -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

RV3028 Capacitor Configuration

With I2C communication established we can now perform configuration of the RV3028 device. From the factory the RV3028's supercap charging circuit and automatic backup switchover are disabled so you will need to execute the commands below once to enable them. The first 5 commands enable the trickle charger and automatic battery switchover while the last two write the configuration to the internal EEPROM so the settings are retained on power down.

(These commands are different to those shown in the video - they were changed to fix a bug which ran a 50% chance of introducing a 1ppm clock oscillator calibration error)

reg=`i2cget -y 1 0x52 0x37` # Get register 0x37
reg=$(( reg | (1 << 4) ))   # Set FEDE bit - safegard, should always be 1
reg=$(( reg | (1 << 5) ))   # Set TCE bit - Trickle Charger Enable
reg=$(( reg | (3 << 2) ))   # Set BSM bits to 0b11 - Backup Switchover Mode
i2cset -y 1 0x52 0x37 $reg  # Write back new value of register 0x37
i2cset -y 1 0x52 0x27 0x00  # Commit changes to EEPROM
i2cset -y 1 0x52 0x27 0x11

To confirm that the data was written to EEPROM remove and reconnect power (GND and Vcc) to the RV3028 and run:

i2cget -y 1 0x52 0x37

and ensure that the output is 0x3c or 0xbc (The most significant bit varies between modules - it is the least significant bit of the oscillator calibration value set at the factory):

pi@raspberrypi:~ $ i2cget -y 1 0x52 0x37
0x3c

Enabling the RV3028 Driver

To enable the RV3028 kernel driver add dtoverlay=i2c-rtc,rv3028 to /boot/config.txt and reboot the Pi with the following commands:

sudo sed -i '$ a dtoverlay=i2c-rtc,rv3028' /boot/config.txt
sudo reboot

After a reboot, open a new terminal and run i2cdetect again to make sure the rv3028's address has been replaced by UU - this indicates that the kernel module is working.

pi@raspberrypi:~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- UU -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Raspberry Pi Clock Configuration

By default the Raspberry pi uses the fake-hwclock module to save the current time to the SD card on shutdown and load it back on boot. This means that the clock loses time whenever the system is powered off but at least it doesn't reset to midnight Janurary 1st 1970 on every boot.

Since we are adding a hardware real time clock the fake-hwclock package should be removed and disabled with the following commands:

sudo apt -y remove fake-hwclock
sudo update-rc.d -f fake-hwclock remove
sudo systemctl disable fake-hwclock

Next, the script /lib/udev/hwclock-set needs to be modified by running the command below. It looks scary, but basically it just comments out every line and adds the correct call to /sbin/hwclock required to set sytem time from the RTC:

sudo sed -i -e 's/^\([^#].*\)/# \1/g' -e '$ a /sbin/hwclock --rtc=$1 --hctosys' /lib/udev/hwclock-set

The hwclock-set script is executed by a udev rule (configured in /lib/udev/rules.d/85-hwclock.rules) when the rtc_rv3028 driver is loaded. The --hctosys (hardware-clock-to-system) command line argument tells the hwclock command to set the system clock from the hardware clock.

Upon the next reboot you can confirm that everything is working by by running dmesg | grep rv3028 and checking for the following output:

pi@raspberrypi:~ $ dmesg | grep rv3028
[    8.903284] rtc-rv3028 1-0052: registered as rtc0
[    8.906002] rtc-rv3028 1-0052: setting system clock to 2021-11-10T22:55:55 UTC (1636584955)

Setting The Time

If your Raspberry Pi has been connected to the Internet the clock should be set quite accurately due to the built-in NTP service.

If, however, your Raspberry Pi hasn't been online for a while (and can't be easily connected) the system time and date will need to be set manually then written to the RTC module.

To set the system time and date you can run the following command with the time and date changed as appropriate - the time is in the format DD MMM YYYY HH:MM:SS with HH in 24hr time:

sudo date -s "11 NOV 2021 15:00:00"

The following command will then write the current system time to the RTC:

sudo hwclock -w

and you can confirm the write was performed correctly with:

sudo hwclock -r

Disabling Automated Network Time Updates

This step is typically optional, but if your system has sporadic Internet connectivity it may mix between using the RTC and network time for setting the system time. If this causes issues (eg: RTC time corruption) the network time sync can be disabled with:

sudo systemctl stop systemd-timesyncd
sudo systemctl disable systemd-timesyncd

Conclusion

Your Raspberry Pi's clock should now be safe from reboots and multi-day power failures for many years without ever having to check or replace an RTC battery.

If you'd like some help with this guide, start the conversation below - We're full-time makers and here to help!

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.