Pico W: How do I read battery power?

I’m trying to build a battery-powered (Li-ion) temperature/humidity/motion sensor using the Pico-W, a DHT22, and a PIR sensor. I’ve got it working, but ran into a snag. I need to monitor the voltage of the battery so I can change it when it gets low. Here’s the latest code I’ve tried:

sensor:
  # Temperature and Humidity
  - platform: dht
    model: DHT22
    pin: 16
    temperature:
      name: "My Temperature"
    humidity:
      name: "My Humidity"
    update_interval: 60s

  # Read Battery Voltage
  - platform: adc
    pin: 24
    id: "VSYS"
    internal: true

  # Convert to Battery Percentage
  - platform: template
    name: "Battery Level"
    unit_of_measurement: '%'
    update_interval: 5s
    lambda: |-
      return ((id(VSYS).state-3) /1.2 * 100.00);

# PIR Motion Sensor
binary_sensor:
  - platform: gpio
    pin: 15
    name: "PIR Sensor"
    device_class: motion

I receive the following error when I try to compile:

INFO ESPHome 2023.7.0
INFO Reading configuration /config/esphome/pico-test.yaml...
Failed config

sensor.adc: [source /config/esphome/pico-test.yaml:42]
  platform: adc
  
  RP2040: Only pins 26, 27, 28 and 29 support ADC.
  pin: 24
  id: VSYS
  internal: True

I thought about connecting physical pin 39 (VSYS) directly to physical pin 34 (GP28/ADC2), then changing the code to use pin 28. I don’t want to fry my Pico (I’m new at this), so I thought I should ask here before moving forward. Will that work, or is there a better way to go about this?

Check out the schematic of the powerchain (Section 4.4 of the Pico data sheet - Raspberry Pi Pico Datasheet) and you will see that VSYS carries the 5V from the USB connector (or whatever you might be putting into that pin from an external power source (Section 4.5).
However the ADC pins have a maximum of 3V3 that they can take so DON’T connect the VSYS pin to any of them.
What is the voltage of the Li-Ion battery - it is 3V7 or are there multiple cells with a higher voltage?
Regardless, I would put in a voltage divider circuit such that whatever is the maximum supply voltage, the output of the divider is below the 3V3 threshold.
The other thing to consider is the reference used by the ADC. If you use the internal one then as the battery drops below the point where the SMPS in the Pico starts to boost the voltage then the battery will drop faster. You could use a zener arrangement to provide your own reference to the ADC-REF pin.
Susan

1 Like

I’m using a single battery. Maximum voltage will be 4.2V, corresponding to 100%. Minimum voltage will be 3.2V, corresponding to 0%. I was over-thinking the voltage divider idea, not realizing it was just a pair of high-ohm resistors. It’s working properly now, with an adjustment to the code to compensate for the halved input voltage.

Thanks for the helpful input.