Measuring 12v on ESP32 or ESP8266

I’m looking to make a device that will monitor the voltage of a generator battery that should be around 12v, maybe as much as 14v. I have a plan on what to do, but would like confirmation before I attempt this. May start off with a esp8266 but may switch to a esp32 to be able to hook up more than 1 monitored point on the generator.

I would have 12v or so coming from the battery to a DC0-25v that will divide the voltage by 5. The esp32 or esp8266 can handle input voltages up to 3.3v. So, as long as the input voltage from the battery is at 16.5v (16.5/5) or less, I should be good there. Through code, I would then us a 5x multiplier to bring the reading back up to the actual battery voltage.

Couple of questions:

  1. Is there a built in voltage divider on the esp8266 (nodemcu v2 ESP8266-12E) or the esp32 (ESP32-C6)? I’ve read multiple articles that contraindicate each other. If there is one, then I would need to use a multiplier filter for that also.

  2. I know the 8266 only has one usable input for adc, pin A0. The esp32 has multiple ones. Does it matter which one or are there certain ones that are best for this type of application?

I’ll stop my questions here for now. This is my first experience working on these boards, so I am learning here. Once I get a better understanding of what is needed for this first part, I may (probably will) have more questions to ask.

I appreciate any and all help with this,

the_buzz_man

I’ve looked into a similar solution for a variety of 12V batteries (cars, boat, RV, spares, generator, etc) and I remember reading something (and this is where I stopped trying to do this on an ESP) that the nature of how lead acid batteries work make the solution problematic. I would have to try to find the original write-up, but I believe it had to do with reporting proper voltage even though the CCA would be too low to actually start something with the battery. Perhaps they were wrong but they were an expert in some field that I don’t recall (electrical engineer or something similar).

Take a look here.

a voltage divider will work well to measure the voltage

you need to look at the datasheet for whatever device you choose to go with to see what the max input is, and it varies between models quite a bit. even ESP32’s in their raw form have differences between ESP32, ESP32-S3 and ESP32-C3 etc.

for a 12v battery that charges at 14.4v i would drop the max down to about 2v as that would be compatible with more devices. and keep the values in the 100’s of k ohms. 600kohm and 100kohm would work well. the result can be multiplied by 7 to get the real value.

expect to see some thermal drift, and as @CO_4X4 mentioned voltage measurement of a battery just sitting there isn’t the best way to check the state of a battery. it may look charged but may not have the capacity to start a car. but for checking the voltage of a battery with some loads connected to make sure you dont over-discharge it should be ok.

if you want something better than that you need to measure current and voltage perform coulomb counting and analyze the data over time. or go an off the shelf solution like a gas gauge… but i don’t know of any that have ESPhome components

1 Like

Why not simply use an ina219? I have three of those in my boat, and they work perfectly:

They are very cheap and can handle the voltage.

https://www.aliexpress.com/item/1005005933483135.html

I’m very interested to hear and see the proposed and/or working solution! I’ve wanted to do this for years but after reading that article kind of gave up for now, but if there is a solution that works I would love to see the complete build of what the solution looks like - i.e., which sensors wired to what GPIO and powered by what method.

I originally planned a power reducer off the battery as my ESP power, then have a voltage sensor on the terminals.

I have a dozen 12V lead acid batteries that would fantastic to monitor! Especially a couple of “garage queen” cars that sit idle 95% of the time where I can tuck this device in the engine compartment even when the car is being driven.

I’ve looked into using the INA219, INA226, and the INA3221, but not quite sure what the benefit of using one of them over the DC0-25v method is. Can you explain why it would be better or easier? Also, in this use case, is the INA226 better than the INA219?

From what I read when I started on this, the INA219 was more stable and precise, and there is no need for more soldering than the pin array. But of course that may be wrong, I just find it very easy to put one of those up with four Dupont cables to the ESP. I have not tried the 226, so I can’t say anything about that.

both the INA219 and INA226 would work one has a slightly higher voltage range, maybe if you change to a 24v system the 226 would be better… they are both cheap

the advantage is you get voltage and current, so you can coulomb count. the DC0-25v is just a voltage divider… so you can only measure voltage.

what sort of charge current and discharge current are you expecting?

Just thrown this out there, have your thought about using one these BM2 bluetooth battery monitors in conjunction with something like a M5Stack Tough ESP32 IoT Development Board Kit? I’m pretty sure you could do a multi-sensor setup, monitoring the battery, temperatures, humidity via bluetooth and direct sensors such as fuel, oil levels and others wired to the device. These BM2 devices have been extensively reverse engineered, so reading it’s values with espHome should not be a problem, as are the numerous BLE temperature and humidity sensors (again, OTS and cheap to replace).

Most all ‘off the shelf’ for replacement and ruggedized for a generator room environment. And your not having to reinvent a wheel.

Good hunting!

Honestly, I have no idea. I’ve never messed with current. All I have ever done is checked voltage. The only thing I really know is to measure current, you have to break the line and have the current pass through the measuring device, which I take it would be the INA 226 (which is what I just ordered). By the end of the week, I will have a couple DC0-25v, a couple INA3221, and a couple INA226. I will also have (2) ESP8226-12E and a ESP32-C6 to play with.

I know I have a lot to learn, but I welcome the challenge.

The article was probably to do with the fact that voltage alone isn’t the best way to measure the capacity of a battery. It does work better with lead-acid batteries than with li-ion or lifepo4. It’s good enough to keep an eye on the health of the batteries or charging issues and you can see if one is low or dead etc. Be careful putting that stuff in a car that doesn’t drive regularly or you will have a dead battery.

The main focus of this project is to just monitor voltage of the battery. The generator has a built-in trickle charger, so the battery should stay charged. I’ve had an issue in the past where it was overcharging the batteries and made them blow up. Monitoring voltage should allow me to know if this is happening again. I also have another point on the generator that only gets 12 volts whenever the generator is running. I would like to monitor that point also so I can get a notification that the generator did turn on. I will tackle this once I get the first part completed and working.

oh, that’s not good. Chargers should have an over voltage shut off built in. You might want to investigate that and see if it needs replaced or else you’ll need to run it through a relay so you can stop the charging.

It’s already been replaced. I just want to make sure this one doesn’t start acting up down the road.

oh gotcha. That’s probably best you got it replaced because they adjust the voltage up/down for optimal charging as well. Good idea to monitor it though. Does the battery start the generator? You could use something like the INA219 to do everything. It monitors voltage, current, power. Spikes in current or power would indicate the battery had a load on it for whatever. There are a few INA sensors so one may be better than the others. This is just an example.

OK, so I got the first run at this working well. I pretty much followed the video below, which helped me understand what was going on.

Shout out to mostlychris for making this excellent tutorial.

Basically, I hooked a 12v battery to the DC0-25v voltage divider which divides the voltage by 5. I then hooked the divider to the ESP8266 via the A0 pin and a ground pin. The A0 pin has its’ own voltage divider of 3.3v. So, in the 8266 yaml code, I multiplied the incoming voltage by 3.3. See code below:

  • platform: adc
    pin: A0
    name: “test battery”
    update_interval: 30s
    filters:
    - multiply: 3.3

This gives me a reading of 2.69v. I then created a template sensor with the following code that multiplies it by 5 to get it back to the original voltage. I used a float to fine tune it to what my multimeter was showing. See code below:

{{ (((states(‘sensor.nodemcu_2_test_battery’) |float -0.158 ) * 5)) | round(4)}}

This gives me a reading of 12.66v which is exactly what my meter was showing.

Looks like this on my breadboard:

Next, will play with using an INA226 to get current and voltage. At least I know this will work for what I am wanting.

the_buzz_man

One of the things that has prevented me from putting this under the hood of any car on a permanent basis is having an undercurrent cutoff. In my head I have this always monitoring the battery but don’t want the device itself to drain the battery fully, so I would need to say “if voltage is less than X then cut off the power to the ESP”. The only thing I’ve found so far is a solar charge controller that would cut power below a certain point, I’ve never found anything that I could put between the ESP and the power lead to cut it off automatically.

BTW, I had watched this same video a while ago and it looked pretty simple to assemble, just the wonkiness of the float factor to dial it in was the only part that had to be fiddled with on a device-by-device basis.

Be carefull with 12V. Current is a lot verry quickly

Therefor battery cables in boats and cars with 12V systems are much bigger in diameter compared to a 230v house system with 16A per breaker.

The starter of my boat engine ( small 1 cylinder 17PS diesel engine) can draw easely +100A during starting. The cables to the starter are 35mm2

100 watt / 12 v = 8 A
100 watt / 230v = 0,43 A

CO_4x4 - I shouldn’t have to worry about discharging the battery as the generator has a built-in maintainer.

bgrr - I understand what you are saying. Will need to verify what the components can handle. I do have a question though, and pardon my inexperience with electrical…Would I need to worry about current (amps) if I stick with the voltage divider and not use one of the current sensing devices (INA226)? My main goal is to make sure the battery remains charged properly and does not drop or get overcharged by the maintainer.