Measure battery % through a voltage detector on a Wemos D1 Mini

Hi everyone,
I’m not super familiar with ESPHome, this is actually my first time using it.
I am building a touch sensor wifi button that is battery operated. It features a USB Type C charging module and a lithium 3.7v battery.
The Wemos D1 Mini does nothing but send a mqtt message when it wakes up through the touch button and an phototransistor by grounding the RST pin. Thanks to one user’s help on the board, I managed to achieve this by programming the Wemos D1 Mini.
I would like the Wemos D1 Mini also to send a mqtt message with the battery percentage (ideally, but also the battery voltage would be fine).
I have seen a guide online that connects the following voltage detector to one pin of the Wemos D1 Mini, but I have no idea how to program ESPHome to detect the voltage and convert it to a battery percentage.
Could someone help me, please?

I will make a post with the finished project once it is completed and the case 3D printed.

sensor:

  - platform: adc
    pin: A0
    name: esp12f Battery
    id: battery_voltage_12f
    filters:
      - multiply: 4.79
    accuracy_decimals: 3
    update_interval: 60s

It looks like I’m stalking you. Try adding this to esphome yaml. It will send the voltage as an MQTT. You may have to measure the voltage with an external meter and change that multiply factor to suit your needs. ( with esp12f I need to use a voltage divider as 3v would burn out the A0 pin. change anything else to suit your needs. I started with wemos D1 but it uses too much battery so I moved over to esp12f. My device )is outside so I charge it by small solar panel.

  - platform: template
    sensors:
      esp12f_battery_fullness:
        friendly_name: ESP12f Battery Capacity
        device_class: battery
        unit_of_measurement: "%"
        value_template: >
            {{ ((states('sensor.esp12f_battery')|float-2.64)/0.0156)|round(2) }}

I created the battery % in yaml in HA as I had to adjust it a lot at start to get it accurate and didn’t want to keep flashing the ESP as I have to take it out of deep sleep and go to OTA mode. The 2.64 was the lowest voltage the esp could work at. It’s a start. You’ll work it out.

2 Likes

@Spiro when I will post the completed project, you’ll be for sure in the “thank you list”.
Any idea on how to calculate the multiplier based on the voltage sensor I’m using? I don’t really get how it works to be honest…

https://ohmslawcalculator.com/voltage-divider-calculator

I use this voltage divider calculator to get roughly the right resistors so I don’t damage the A0 pin. I see what voltage then the esp sends out and I measure with a multimeter across the battery terminal. So if esp says 1 volt and I measure it at 4.2v the factor is 4.2. If you don’t have a multimeter then you just charge battery until it’s fully charged and assume it 4.2V. I think the D1 may be able to measure up to 3v due to internal resistor but not sure. I not an electrical engineer but I think the larger the resistors you use for the devider the less of the battery power gets used up by your measuring. If too big a resistance then tiny disturbances in the circuit currents will have big impacts on accuracy.

1 Like

Awesome!!! Thank you so much once again, my stalker :smiley: :heart:

Formula for the D1 mini:

X kOhm = (Vmax * 100 kOhm) / Vesp - 100 kOhm - 220 kOhm

Vmax is the maximum that you want to measure.
Vesp for the 8266 is 1 Volt.
X kOhm would be the single resistor that you simply put in front of the ADC input of the D1 mini without any additional voltage divider resistors.

Example für 5 Volt max voltage:
X kOhm
= (5 Volt * 100 kOhm) / 1 Volt - 100 k Ohm - 220 kOhm
= 500 kOhm - 100 kOhm - 220 kOhm
= 180 kOhm

Example für 10 Volt max voltage:
X kOhm
= (10 Volt * 100 kOhm) / 1 Volt - 100 k Ohm - 220 kOhm
= 1000 kOhm - 100 kOhm - 220 kOhm
= 680 kOhm

1 Like

Sorry guys, I know it wasn’t in the original question, but I can’t get my head around on how to send the mqtt message with the voltage measured by pin A0 when the wemos wakes up…

If you are running MQTT(instead of API) then any sensor you have in your YAML in esphome will be sent to a new sensor on your HA.

sensor:

  - platform: bmp280
    address: 0x76
    update_interval: 60s 
    temperature:
      name: Esp12f Temp
      oversampling: 16x
    pressure:
      name: Esp12f Pres
      oversampling: 16x

 
  - platform: adc
    pin: A0
    name: esp12f Battery
    id: battery_voltage_12f
    filters:
      - multiply: 4.79
    accuracy_decimals: 3
    update_interval: 60s

batteryMQTT
As seen in MQTT explorer.


and in entities