Measuring 12v battery with ADC

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
newb
Posts: 43
Joined: Wed Jan 10, 2018 8:19 pm
Location: Bulgaria

Measuring 12v battery with ADC

Post by newb » Sat Dec 15, 2018 12:27 pm

Hi community, I've got the following setup:

12v battery-->voltage divider-->ADC pin of NodeMCU v3 (esp8266)

I read this particular board has its own internal voltage divider (220kOhm and 110kOhm resistors) which allows 3.3V at the ADC pin, bringing it to 1V at the esp chip.

I've put between the battery and the ADC pin three additional resistors in series 1 x 680kOhm and 2 x 100kOhm), so that at the ESP, 12v is lowered to 1v. adc.read() reads 1024, however I'm struggling to convert this to the actual battery voltage.

Can anyone help me with the formula? Currently I've come up with the following, which I found on the Arduino forum and modified to my case, but I'm not sure it's correct.

Code: Select all

voltsPerBit = 1/1023
ratioV = (1100000 + 100000) / 100000
rawVolts = d1 * voltsPerBit
battery = rawVolts * ratioV
I would appreciate any suggestions.

PS: Battery is being charged from solar panel with cmp12 charge controller https://www.amazon.co.uk/MagiDeal-Contr ... ords=cmp12 (not best option but rather cheap one). Does it make any sense to measure voltage when the controller is charging the battery?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Measuring 12v battery with ADC

Post by Roberthh » Sat Dec 15, 2018 12:57 pm

First of all, the value of the series resistors is too small. They would transform a range 0-1V into 0-12V. But a fully charged 12V (lead) battery has a voltage of about 14.4 V. Why not take something like 1.5 MOhm. Besides that, you calculation seems OK, assuming that d1 is the value obtained from the adc. And you have to add a calibration factor, since the full range of the adc is not exactly 1V. And since the adc is pretty noisy, you have to average over many samples.

newb
Posts: 43
Joined: Wed Jan 10, 2018 8:19 pm
Location: Bulgaria

Re: Measuring 12v battery with ADC

Post by newb » Sat Dec 15, 2018 4:29 pm

Thanks for your guidance, Robert.
I replaced the resistors in series with one 1M and one 2k2 one, added a calibration factor and all seems relatively correct. I do 10 readings and get the average. I suppose this is the best I can get.

I found this formula for adjusting the scale factor. I'll leave it here in case someone else needs it:
new scaling factor=(actual voltage (multimeter value) / read voltage (adc converted to volts value) )* old scaling factor (e.g.1/1023)

Post Reply