Battery_level attribute from ESP

Hey guys,

thanks for sharing of all these snippets but how did you wired your ESP board???

E.g. i’m using a battery shield. It gives me exactly 5V. Perfect to power my esp. I’m created my own circuit divider with 2 resistant and wired it directly to the battery and A0 of my ESP.

Did you do it differently??

Because, using “VCC” gives me always 100% and wiring A0 over the bat. shield the same! VCC is the internal power isn’t it? And wiring about the battery out pols to A0 is also always 100% because it ensure, that the battery always gives the right power supply.

Neither VCC nor A0 wired over bat. shield decrease over the time…different when I wired A0 over the circuit divider to the battery directly!

So, what does you circuit look like???

Thanks,
Isicom

I am really late to the forum, but I have a question.

I recently received a gift of dozens of components from a friend. Among the pile of stuff is like 30 adafruit LC709203F Battery Fuel Gauge. I have played with the dev board and it seems to be a little more precise than setting up a pin to the battery with voltage divider. My problem, is I have no idea how to use it with all my devices that are all ESP32 based.

Could someone who knows hook me up with a suggestion of what to do? I see that adafruit’s “whipper snapper” or AFIO has usage for this so it has to be possible…

Maybe a custom sensor…but again I don’t know how to do that, and I can’t find a how to that is specifically adding a i2c sensor that does not have a pre-built platform.

Any insights is appriated.

Hi… I also have bought some ESP32 devices (from EzSBC modell ESP32_Bat_Pro) with LC709203F battery sensor.

They have a Github repository with code sample for how to use the sensors.

I also could not find the sensor on the esphome documents.

Is somebody able to assist in adding the sensor to esphome?

I found a project who already included the sources - it was not working out of the box, but I managed to get it working: GitHub - ThomasCr/lc709203f: esphome support for lc709203f

Good Morning
I have a ESP with an integrated 18650 battery, could I monitor the battery?
if so what code would work.

This is an example of the board I have.
https://www.amazon.co.uk/battery-charger-Bluetooth-Transceiver-Development/dp/B0BC2C7PJ7/ref=sr_1_7_sspa?crid=18PRP7LMFU7YJ&keywords=esp32+18650&qid=1687586878&sprefix=esp32+18650%2Caps%2C142&sr=8-7-spons&sp_csd=d2lkZ2V0TmFtZT1zcF9tdGY&psc=1

Good Afternoon, So I have tried the above code out and can see some changes.

I currently solder 2 x 100k resistors from + - on the battery to a pin on the board as I understand there is nothing on the board to do this.

what is the correct way to wire this? using a 18650 which is 4.2v max

really would appreciate any help with this

You can follow some crumbs over here;)

It should be just 1 100k resistor:

Please let me know what device you are using as some might have other output or way of working.

Some boards already have a 100k resistor added in-line for example.

1 Like

Good Evening

So I have brought one of these battery shields with a 3.7 37000mAh battery and installed a 100k resister as suggested in this diagram.

I have Battery Shield V1.2.0

using this code

- platform: adc
    pin: A0
    name: "A0 Voltage of 3.7V"
    update_interval: 15s
    accuracy_decimals: 3
    filters:
      - multiply: 3.7
    id: server_battery_voltage
    entity_category: diagnostic
    unit_of_measurement: "V"  
    device_class: battery
  - platform: template
    name: "Server_Battery_Percentage"
    update_interval: 15s
    accuracy_decimals: 0
    lambda: |-
      return (((id(server_battery_voltage).state-3) /0.7) * 100.00);
    entity_category: diagnostic
    unit_of_measurement: "%"  
    device_class: battery

but when using a volts meter it’s 4.1 volts still.

what am I doing wrong???

[19:47:26][D][sensor:093]: ‘A0 Voltage of 3.7V’: Sending state 3.70000 V with 3 decimals of accuracy
[19:47:26][D][sensor:093]: ‘Battery_Percentage’: Sending state 100.00001 % with 0 decimals of accuracy

return ((id(battery_$devicename).state-3) /1.2 * 100.00);

my battery is currently at 4.1v
I have changed 0.7 to 1.2 and now getting

[07:20:24][D][sensor:093]: ‘Battery_Percentage’: Sending state 36.95476 % with 0 decimals of accuracy
[07:20:25][D][sensor:093]: ‘A0 Voltage of 3.7V’: Sending state 3.45791 V with 3 decimals of accuracy

fyi
As I have v1.2.0 I have soldered jumper 2. Do I need any other resistor? I have seen that I might need a 130k resistor.

Depending on your resistor you might need to change the calculation. The best thing I could recommend is using a variable voltage input and try to see what the raw input is on A0 when the voltage is 3V (with the resistor connected at all times) and then going up to 4.2V
If it says 1 at like 3.5V, try adding a higher-ohm rated resistor.

ok I will need to invest in a variable voltage power supply.

This is what I found on the internet. could this help me?

The new battery shield
There now is a new version (V1.2.0) of the battery shield that has an inbuilt resistor connecting A0 to the battery, through Jumer J2 (‘Jumper’ being a big word for 2 solderpads), so if you want to measure the battery voltage, all you need to do is to put some solder connecting J2
However, rather than a 100k resistor, a 130k resistor was used. The voltage divider thus becomes 100/(130+220+100), so for a full reading of 1023 (=1Volt on A0) a total of (1/100k)*(130+220+100)=4.5Volt would be necessary.
In reality the Lipo cell will not give off 4.5Volt.

what I don’t understand is that with no resistor from bat+ to A0 i get around 0.92871 (jumps about a bit 0.92969, 0.92578)

as soon as I put a 100k resistor the voltage jumps up to above 1 volt why?

The best way to check is to use a multimeter, or at leat a meter to measure the ohm rating. Some D1 mini boards don’t work quite well with the A0. I’ve had mine die after a year. At this point the battery voltage was always 0…
Try to measure with and without a battery connected.
You could be overvolting the A0, which might cause damage.

These test where performed without any additional resistors.

Without Battery
ok so testing from the bat+ to A0 with the D1 Mini v3.0.0 disconnected = 129.4

when connecting the battery shield and the d1 mini wemos v3.0.0 it starts at about 90 and then counts up. i got to 116 something and it was still going. (takes a while to get there

With Battery
testing from the bat+ to A0 with the D1 Mini v3.0.0 disconnected = 129.3
connecting the d1 mini Wemos V3.0.0 together i get 20.36

did you ever manage to work this out? still trying to solve this myself

Hi All
I see this tutorial very interesting for this purpose :slight_smile: Power ESP32/ESP8266 with Solar Panels and Battery | Random Nerd Tutorials
It’s explain well how doing Solar Panel + Battery level sensor for ESP32/ESP8266… but formula is for Arduino and not for ESPHOME Yaml Code… so If someone could make it because I’m begener with ESPHOME…
Alexandre from France

I believe pin should be
pin: A0 instead of
pin:VCC

Hi guys,

so i am struggeling with the same problem…
i have an esp8266 running with a battery shield 1.2.0. I did solder the J2 “jumper” and should now have a 130K resistor between battery+ and A0.

This is my code:

#Batterylevel of Device
  - platform: adc
    pin: A0
    name: battery_voltage
    id: vcc
    update_interval: 15s
    accuracy_decimals: 3
    filters:
      - multiply: 4.2
 
  - platform: template
    name: battery_level_percentage
    unit_of_measurement: '%'
    accuracy_decimals: 0
    update_interval: 15s
    lambda: |-
      return ((id(vcc).state -3) /1.2 * 100.00);

In HA for battery_voltage i get a value of 0,008V and accordingly for battery_level_percentage i get -249%

The battery voltage, when measuring with a multimeter, is about 4,16V. The 130k Ohm resistors should work well with that, right?

So i looks like i have to low voltage on the A0 pin, right?

What am i missing here?

Should the D1 mini battery shield prevent a deep discharge or should i somehow do that myself?

Any help would be apreciated.

regards,
markus