Solution for how to test the power status of Raspberry Pi. - Yahboom

* This Blog from Muquan Zhu.

When we are using Raspberry Pi board, sometime you can see a yellow lightning bolt or a prompt of "Low voltage warning Please check your power supply" in the upper right corner of the system desktop.

When the voltage is insufficient, it can cause many problems.
Such as unstable system operation, data loss, and even damage to the SD card.

Even if a high-quality power adapter is used to meet the working voltage and current of the Raspberry Pi, repeated plugging and unplugging after long-term use will lead to poor contact and high resistance, which will make the input voltage of the Raspberry Pi insufficient.

When se input following command on command terminal for Raspberry Pi

vcgencmd get_throttled

We will get a hexadecimal number that reflects status information related to the current system frequency, input voltage, etc.

After this number is converted into binary, the 8 bits in it represent the status of the 8 flags.

For details, please refer to this link on Raspberry Pi website: https://www.raspberrypi.com/documentation/computers/os.html

 

Let's focus on two of them:
If the 0th bit of this number is 1, it indicates that the current input voltage is insufficient;
If the 16th bit of this number is 1, it indicates that there has been an insufficient input voltage after startup;
In fact, as long as we detect these two flags in real time, we can monitor the input voltage of the Raspberry Pi.

Install vcgencmd library

Input following command on command terminal to install the vcgencmd library.
sudo pip3 install vcgencmd -i https://mirrors.aliyun.com/pypi/simple/

Validate vcgencmd library with Python3 environment

About code

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# cython: language_level=3

#Flag Bits

UNDERVOLTED         = '0'

CAPPED              = '1'

THROTTLED           = '2'

SOFT_TEMPLIMIT      = '3'

HAS_UNDERVOLTED     = '16'

HAS_CAPPED          = '17'

HAS_THROTTLED       = '18'

HAS_SOFT_TEMPLIMIT  = '19'

 

from vcgencmd import Vcgencmd

from colorama import init

from colorama import Fore, Back, Style

import time

init(autoreset=True)

vcgm = Vcgencmd()

def print_log(flag, info):

    if flag:

        print(Fore.RED + Style.BRIGHT + info, end = '  ')

    else:

        print(Fore.GREEN + Style.DIM + info, end = '  ')

 

while True:

    print('[{}] '.format(time.strftime('%M:%S')), end = '')

    output = vcgm.get_throttled()

    flag = output['breakdown'][UNDERVOLTED]

    print_log(flag, 'UNDERVOLTED')

    flag = output['breakdown'][CAPPED]

    print_log(flag, 'CAPPED')

    flag = output['breakdown'][THROTTLED]

    print_log(flag, 'THROTTLED')

    flag = output['breakdown'][SOFT_TEMPLIMIT]

    print_log(flag, 'SOFT_TEMPLIMIT')

    flag = output['breakdown'][HAS_UNDERVOLTED]

    print_log(flag, 'HAS_UNDERVOLTED')

    flag = output['breakdown'][HAS_CAPPED]

    print_log(flag, 'HAS_CAPPED')

    flag = output['breakdown'][HAS_THROTTLED]

    print_log(flag, 'HAS_THROTTLED')

    flag = output['breakdown'][HAS_SOFT_TEMPLIMIT]

    print_log(flag, 'HAS_SOFT_TEMPLIMIT')

    print()

    time.sleep(1)

#EOF

 

Test

If you are using the normal power supply on the Raspberry Pi 4B, and you will get the following results. 

If the system displays a red message, it indicates that the relevant flag bit is 1.
We can see that the red UNDERVOLTED indicates that the input voltage is too low,
The red THROTTLED indicates that the system operating frequency has been forced down,
The red HAS_UNDERVOLTED indicates that a low voltage condition has been detected,
A red HAS_THROTTLED indicates that it has been downclocked.

 

Solutions

Leave a comment