LOLIN32 Lite with EEMB 3,7V 500mAh JST: get charge %

Hi everybody,

I have a LOLIN32 Lite with an EEMB 3,7V 500mAh JST LI-Ion Polymer Battery.

Then I have two sensors

  
  - platform: adc
    pin: GPIO35 
    name: "Akku-Spannung"
    id: battery_voltage
    unit_of_measurement: "V"
    icon: "mdi:battery"
    attenuation: 11db
    filters:
      - calibrate_linear:
          - 0 -> 0
          - 3.3 -> 3.3
      - lambda: return x / 4.2 * 100;
      - filter_out: nan

  - platform: template
    name: "Akkustand in %"
    id: battery_level_percentage
    unit_of_measurement: "%"
    icon: "mdi:battery-charging-30"
    accuracy_decimals: 0
    lambda: |-
      float voltage = id(battery_voltage).state;
      if (voltage > 4.1) return 100.0;
      if (voltage > 3.9) return (voltage - 3.9) * 50.0 + 50.0;
      if (voltage > 3.7) return (voltage - 3.7) * 20.0 + 30.0;
      if (voltage > 3.5) return (voltage - 3.5) * 15.0 + 15.0;
      if (voltage > 3.3) return (voltage - 3.3) * 10.0 + 5.0;
      if (voltage > 3.0) return (voltage - 3.0) * 1.0;
      return 0.0;

I had this code generated by a chatbot, so it might be incorrect. It does create two sensors that show values, but the output for battery percentage (Akkustand in %) seems weird to me. Please take a look

Start at 08:16, I did other testing before that. Why did it spike to almost 60%, then down, back up, back down?

It is currently at 6%. This number has not changed for 6 minutes while the ESP32 is powered via USB.

I am now wondering whether charging is just very slow or if something is wrong with my code or if perhaps the battery is defective.

When I ping the device and unplug USB, the device cannot be pinged any longer. After re-powering it, I get this

But I doubt this is correct because if the battery was charged around 100%, it should power the board when it is not powered via USB, right?

I just read this once more before posting and checked, it is back to

Weird.

What do you recommend? Is the code incorrect? Is the battery not idea? Thanks in advance for your ideas :slight_smile:

This part does nothing at all.

This should return a percentage.
At least that is what the calculation does.

But then the unit is volt.

I believe if the first filter that does nothing does something with this filter that should return a percentage.

Try and remove the calibrate filter.
Then I believe this ADC value should be the percentage

Edit.
I just noticed it’s a 3.7 volt battery. Why is it suggesting to divide with 4.2.

What is the lowest voltage the battery has when it’s at 0%?

LiPo battery voltage range is 3-4.2V from empty to full. You can’t read that directly with esp32, which has ADC capable of reading max ~3.1V. And giving >3.6V to any pin will eventually damage it.

You need ~1:2 voltage divider to scale down the battery voltage.

Try R1=100k, R2=220k


And battery voltage doesn’t have anyhow linear relation to battery charge level, so you need to use multi-point calibration.

Even better approach would be scaling down to <1.1V and using attenuation 0db.

Thanks everybody. I guess don’t trust chatgpt :smiley: I assumed that it was correct when it told me the Lolin32 Lite was capable of measuring the voltage directly without having to connect additional GPIO.

Also, my boards polarity was reversed to the JST battery connector. I am giving up on this project for now.

It’s pretty simple to swap the wires on JST. Making simple voltage divider doesn’t ask much effort either.

You’re absolutely right. But it feels like this project was doomed from the start.

I wanted an “is there still enough water in the dogs’ bowl” sensor with battery / without the need to be connected via any power cable.

It suggested capacitive stuff with copper foil. That did not work as expected.

Then, it recommended the Lolin32 Lite because “it would support any JST battery pack and allow measurement of energy / calculating how much capacity is left in the battery out of the box”. Which doesn’t seem to be the case.

Then the polarity was wrong with multiple battery packs that I tested, which is pretty weird.

Finally, I remembered I could use contact-less water sensors, which I still had lying around. But then realized they needed 5V to run, while all the battery packs I found only provided 3.7V tops. Yeah, step up etc., but all these occurrences combined made me think perhaps I should just look at the water bowl every now and then and have this one thing not be automated. Maybe I’ll get back to it when I got more time and space to work on this project…

I agree. Often our “projects” are not so useful in practice.
On top of that, Esp / Esphome battery life is likely going to be disappointing.

You need just boost converter. Like every USB power bank has…

1 Like

Maybe you just need a bark detector? The dog should usually tell you if it needs anything.

[Above thread is confirmation that AI slurp is alive and active.]

Just wait for the dogs to be so desperately thirsty that they inform me by barking? Don’t really see what’s so bad about AI if people come up with solutions like this without it.