Battery Monitoring
Summary:
- I made my first voltage divider. You need two resistors. I landed on 2 x 300kΩ
- Wire them like below. I think it’s right. (Edit, Its not quite right as battery power should go to esp battery terminal) wanted to keep the diagram simple and didn’t want to draw up my full wiring yet. You just add extra wires for power and ground as you need for your other devices. Edit: best to see the wiring diagram now.
Longer version and resources:
- Article: ESP8266 battery level meter | ezContents blog 37,
- Thread: ESPHome Battery Level Sensor 48,
- Some help/checks from @ssieb over on Discord ,
From what I could understand the sum of the resistors should be high and the divided voltage should be < 2.4V
Which led me to my 2 x 300kΩ selection. I also like this “symmetric” design. Doesn’t matter which way you wire it.
Battery sensor snippet
#Notes:
#Voltage divider: Used 2 x 300K Ohm resistors
- platform: adc
id: batt_voltage
name: Battery Voltage
internal: true
pin: ${batt_voltage_pin} #ADC1
update_interval: never
accuracy_decimals: 2
attenuation: auto
filters:
# #Scale it back up from voltage divided value 2 x 300K > 2.1. 4.2/2.1 = 2.
- multiply: 2
on_raw_value:
then:
#Sensor update counter.
- lambda: id(count_batt_voltage).publish_state(id(count_batt_voltage).state +1);
#Intermediate sensor. Might consolidate them later.
- platform: copy
source_id: batt_voltage
id: batt_voltage_filtered
icon: "mdi:battery"
internal: false
name: Battery Voltage
unit_of_measurement: V
accuracy_decimals: 2
filters:
- median: #Use moving median to smooth noise.
window_size: 10
send_every: 10
send_first_at: 10
#Convert the Voltage to a battery level (%)
- platform: copy
source_id: batt_voltage_filtered
id: batt_level
internal: false
icon: "mdi:battery"
name: Battery Percent
unit_of_measurement: '%'
accuracy_decimals: 0
filters:
# Map from voltage to Battery level
- calibrate_linear:
- 3.1 -> 0 #Set 3.0 to 0% even though it can go lower (2.4V), for life extention. There's not much capacity below this anyway.
- 4.1 -> 100 #Set 4.05 to 100% even though it can go higher (~4.2V), for life extention.
#Overide values less than 0% and more than 100%
- lambda: |
if (x < 0) return 0;
else if (x > 100) return 100;
else return ceil(x / 5) * 5;
on_value:
then:
#Publish that data is recieved
- binary_sensor.template.publish:
id: batt_level_recieved
state: ON