← index #11185Issue #10406
Related · high · value 0.607
QUERY · ISSUE

rp2: Add something to read vbus and vsys

openby peterharperukopened 2023-04-03updated 2023-04-27
enhancementport-rp2

The process of reading vsys and vbus is different between Pico and Pico W. And it's quite complicated.
Add something to the rp2 class?
Basically we need a way to tell if we're running off a battery and the voltage of the battery.
See https://github.com/raspberrypi/pico-sdk/issues/1222

CANDIDATE · ISSUE

PR2: How to use ADC channel 3/GPIO pin 29 to measure VSYS on Pico W board

closedby devegiedopened 2023-01-04updated 2023-01-06
bug

firmware: rp2-pico-w-20221220-unstable-v1.19.1-782-g699477d12.uf2
board: rp2-pico-w
version information: MicroPython v1.19.1-782-g699477d12 on 2022-12-20; Raspberry Pi Pico W with RP2040

MicroPython program

import machine
import sys

print("sys.implementation:{}".format(sys.implementation))
print("sys.version:{}".format(sys.version))
pin = machine.ADC(29)
adc_reading  = pin.read_u16()
adc_voltage  = (adc_reading * 3.3) / 65535
vsys_voltage = adc_voltage * 3
print("""ADC reading:{}
ADC voltage:{}
VSYS voltage:{}""".format(adc_reading, adc_voltage, vsys_voltage))

on Raspberry Pi Pico W outputs

sys.implementation:(name='micropython', version=(1, 19, 1), _machine='Raspberry Pi Pico W with RP2040', _mpy=4358)
sys.version:3.4.0; MicroPython v1.19.1-782-g699477d12 on 2022-12-20
ADC reading:608
ADC voltage:0.0306157
VSYS voltage:0.09184711

on Raspberry Pi Pico it outputs

sys.implementation:(name='micropython', version=(1, 19, 1), _machine='Raspberry Pi Pico with RP2040', _mpy=4358)
sys.version:3.4.0; MicroPython v1.19.1-782-g699477d12 on 2022-12-20
ADC reading:33032
ADC voltage:1.663319
VSYS voltage:4.989957

equivalent CircuitPython program

import analogio
import sys
from board import VOLTAGE_MONITOR

print("sys.implementation:{}".format(sys.implementation))
print("sys.version:{}".format(sys.version))
pin = analogio.AnalogIn(VOLTAGE_MONITOR)
adc_reading  = pin.value
adc_voltage  = (adc_reading * pin.reference_voltage) / 65535
vsys_voltage = adc_voltage * 3
print("""ADC reading:{}
ADC voltage:{}
VSYS voltage:{}""".format(adc_reading, adc_voltage, vsys_voltage))
pin.deinit()

on Raspberry Pi Pico W (same board) it produces output

sys.implementation:(name='circuitpython', version=(8, 0, 0), mpy=517)
sys.version:3.4.0
ADC reading:33192
ADC voltage:1.67137
VSYS voltage:5.01412

I know that on Pico-W this pin is shared with WiFi module. But I can not get correct output from Pico-W event after fresh .uf2 firmware upload with 0 WiFi module usage.
CircuitPython made special case for this pin. And it works perfectly. CircuitPython indicates active REPL with onboard LED. On Pico-W that LED is controlled by WiFi module, but event with active LED I can get ADC values from REPL prompt.
Maybe this solution can be ported to MicroPhyton?
Also it can be regression with new Pico SDK or latest changes in rp2 MicroPython port. In older forum posts I can find examples that VSYS measurement was working even while using WiFi module. But I don't know where can I get older MicroPython .uf2 files

Some related discussions:
https://forums.raspberrypi.com/viewtopic.php?t=339994
https://forums.raspberrypi.com/viewtopic.php?t=301152

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied