Skip to content

Python script to check batterylevel on powerbank connected to Raspberry Pi

I have project that includes a Raspberry Pi connected to a powerbank as power source. In my python script I would like to check the battery status of the powerbank before starting a function.

From what I’ve been able to look up on the internet there is no way this will work solely with a code and no additional parts(except the powerbank, Raspberry Pi, etc.).

The closest I got was the way we can use code to check the battery status of computers, like the code below.

import psutil

def convertTime(seconds):
    minutes,seconds = divmod(seconds,60)
    hours,minutes = divmod(minutes,60)
    return "%d:%02d:%02d"% (hours,minutes,seconds)

battery = psutil.sensors_battery()
percent = battery.percent
time = convertTime(battery.secsleft)

print("Battery percentage:", percent)
print("Power plugged in:", battery.power_plugged)

# Convert time to hours and min
print("Battery left:", time)

Is there a similiar way for Raspberry Pi devices and would it work if it’s connected to a powerbank / other portable powersources.



Using the battery specs, why not get the maximum and minimum potential difference of the whole battery and interpolate?

You will need to measure this directly from the battery terminals and not through the designated input/output ports which are designed to only allow a certain amount of voltage (5volts usually) and direction of current.