This SHIM provides a convenient way of powering your Raspberry Pi Pico from a LiPo/LiIon battery. You can recharge the battery easily by providing power to the Pico’s USB port.
While trying to implement this example I notice it requires a particular display. I have removed the code for the display and added log to file functionality. Now it will run the pico until the battery is depleted and log the numbers of seconds the program executed for. The led is toggled once per second so you can tell when the program has stopped.
# This example shows how to read the voltage from a LiPo battery
# connected to a Raspberry Pi Pico via a Pico Lipo SHIM
# it uses this reading to calculate how much charge is left in the battery.
# The output is to the console if connected and writes to a file named data.cvs
# Obvisouly you can't test the battery while the USB is connected so the led flashes
# while the program is running and the output to a file named data.csv on the pico
#
# Modified from an example found here
# https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/pico_lipo_shim/battery_pico.py
#
# LIPO Shim
# https://core-electronics.com.au/pimoroni-lipo-shim-for-pico.html
from machine import ADC, Pin
import time
led = Pin(25, Pin.OUT) # Used to indicate the program is running
vsys = ADC(29) # reads the system input voltage
charging = Pin(24, Pin.IN) # reading GP24 tells us whether or not USB power is connected
conversion_factor = 3 * 3.3 / 65535
full_battery = 4.2 # these are our reference voltages for a full/empty battery, in volts
empty_battery = 2.8 # the values could vary by battery size/manufacturer so you might need to adjust them
count = 1
while True:
# convert the raw ADC read into a voltage, and then a percentage
voltage = vsys.read_u16() * conversion_factor
percentage = 100 * ((voltage - empty_battery) / (full_battery - empty_battery))
if percentage > 100:
percentage = 100.00
file=open("data.csv","a") # creation and opening of a CSV file in Write mode
if charging.value() == 1: # if it's plugged into USB power...
print("Charging from usb") # Write data to the screen if connected
file.write("Charging from usb,") # Write data in the opened file
else: # if not
file.write("Running from Battery,")
print("Voltage=" + str(voltage) + "V Percentage=" + str(percentage)+"%")
file.write(str(count) + " seconds," + str(voltage)+"V" + "," + str(percentage) + "%\n") # Writing data in the opened file
file.flush() # Internal buffer is flushed (not necessary if close() function is used)
file.close() # The file is closed
led.toggle()
time.sleep(1)
count=count+1
My program ran for over 5 hours on a tiny 120mah battery.
All it does is sleep, log and flash but I wasn’t expecting it to run that long.
I wonder if it will run longer if I change the flash rate to 15sec.
One of life’s unanswered questions
Always a nice surprise when batteries last longer than expected, usually the reverse is typical.
No doubt there are heaps of small tweaks that could be made to eek out those small incremental battery life gains, some easier than others. Changing the flash rate sounds like a good starting point.
1 Like
And you can get our latest projects and tips straight away by following us on: