ESPHome - Get battery level from A0 on D1 Mini

Hi,

I’ve a D1 test board that is connected to a battery, it comes online as I am able to see it within HA. The code of the board is:

esphome:
  name: ${name_of_board}
  platform: ESP8266
  board: d1_mini

sensor:
  - platform: adc
    pin: A0
    name: "A0 Voltage x 4.2"
    update_interval: 15s
    accuracy_decimals: 3
    filters:
      - multiply: 4.2
    id: battery_$name_of_board
    entity_category: diagnostic
    unit_of_measurement: "V"  
    device_class: battery  
  - platform: template
    name: "D1003_Battery_Percentage"
    unit_of_measurement: '%'
    update_interval: 15s
    accuracy_decimals: 0
    lambda: |-
      return ((id(battery_$name_of_board).state-3) /1.2 * 100.00);
    entity_category: diagnostic
    device_class: battery

But this is producing some goofy battery reading in the UI.
Screenshot from 2023-12-06 19-21-54

I got the above code from one of thread in this forum and it is supposed to be working for few others so not sure why I keep getting these goofy battery readings. There are not other sensors attached to any of the other GPIOs, let me know if anyone can help troubleshoot.
Thanks.

D1 mini? Actually that board doesn’t break out the ADC which is limited to 1V but instead the A0 pin which comes with a voltage divider for measurements up to 3.3V if I remember correctly. Without a change (additional resistor?) to the voltage divider you are not able to measure the range you want (up to 4.2V?).

What does your multimeter tell you it should be?

As @indeeed said, there is a voltage divider in play on a d1 mini. 3.3 v at the dev board pin is seen as 1v by the essp8266 chip.

Even so, i can’t get my head around what you are seeing.

1 Like

@nickrout even your confusion was helpful!!

I checked the A0 pin with multi-meter and it actually shows “0” as the display only has 2-digit so the “0.008” seems expected! I also read somewhere in the thread that D1 Mini has a built-in voltage divider (not sure what that means) so tested little more and tweaked the code to following:

api:
logger:

esphome:
  name: ${name_of_board}
  platform: ESP8266
  board: d1_mini

sensor:
  - platform: adc
    pin: VCC
    id: vcc
    internal: true
  - platform: template
    name: ${name_of_board} Battery Percentage
    unit_of_measurement: '%'
    update_interval: 15s
    accuracy_decimals: 0
    lambda: |-
      return ((id(vcc).state/3.3) * 100.00);
  - platform: template
    name: ${name_of_board} Battery Raw
    update_interval: 15s
    accuracy_decimals: 3
    lambda: |-
      return (id(vcc).state) ;

switch:
  - platform: gpio
    pin: GPIO2
    name: ${name_of_board} LED
    inverted: true
    restore_mode: ALWAYS_ON

Now I see voltage numbers as below:

But I’ve checked all the pins on D1 and none of them actually shows “2.889 V” so not sure what this output represents.

Is the battery actually connected at this stage?

Yeah and it is in the docs too. Start with the fact that the board (ie the D1 mini) has a circiut between the board pin (what you connect to with a wire) and the chip (the esp8266 on the board).

A voltage divider does this: The voltage at the pin on the D1 board is divided by 3.3 and then fed to the ADC on the chip, so that when you give the board 3.3v (the usual high on an electronics circuit these days) the chip will see 1v, which is the maximum of its range.

So if the chip is seeing 1v, the actual voltage at the board is 3.3v. If the chip sees 0.5v the board is being fed 1.65v.

There is this note in the docs.

This component prints the voltage as seen by the chip pin. On the ESP8266, this is always 0.0V to 1.0V Some development boards like the Wemos D1 mini include external voltage divider circuitry to scale down a 3.3V input signal to the chip-internal 1.0V. If your board has this circuitry, add a multiply filter to get correct values:

sensor:

  • platform: adc

    filters:
    • multiply: 3.3
1 Like

@nickrout thanks for the explanation and it also explains the “3.3” multiplier in the code that I copied from the other thread. I really appreciate the explanation!

Yes the board is powered by battery and not any other power source but the battery is old and lost the charge so waiting for the charger to be delivered before I can resume my testing.

1 Like