Created
October 6, 2025 09:43
-
-
Save helgibbons/c1fde0accf3ca3991159bb6b004da53e to your computer and use it in GitHub Desktop.
Read system voltage on Pico W and estimate battery percentage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| full_battery = 4.2 | |
| empty_battery = 2.8 | |
| def get_vsys(): | |
| # Pico W voltage read function by darconeous on reddit: | |
| # https://www.reddit.com/r/raspberrypipico/comments/xalach/comment/ipigfzu/ | |
| conversion_factor = 3 * 3.3 / 65535 | |
| import network | |
| from machine import Pin, ADC | |
| wlan = network.WLAN(network.STA_IF) | |
| wlan_active = wlan.active() | |
| try: | |
| # Don't use the WLAN chip for a moment. | |
| wlan.active(False) | |
| # Make sure pin 25 is high. | |
| Pin(25, mode=Pin.OUT, pull=Pin.PULL_DOWN).high() | |
| # Reconfigure pin 29 as an input. | |
| Pin(29, Pin.IN) | |
| vsys = ADC(29) | |
| return vsys.read_u16() * conversion_factor | |
| finally: | |
| # Restore the pin state and possibly reactivate WLAN | |
| Pin(29, Pin.ALT, pull=Pin.PULL_DOWN, alt=7) | |
| wlan.active(wlan_active) | |
| voltage = get_vsys() | |
| percentage = 100 * ((voltage - empty_battery) / (full_battery - empty_battery)) | |
| if percentage > 100: | |
| percentage = 100.00 | |
| print('{:.2f}V'.format(voltage)) | |
| print('{:.0f}%'.format(percentage)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment