WEMOS 12V DC Battery Monitor - Help!

Not sure where to start with this post so ... I will just jump right in.

we have created a really nice daughter board that utilizes the existing footprint of the WEMOS Power Module. This board is supposed to be used as a voltage divider for monitoring our power system when we connect it to a 12V DC car battery. The board gets 2 resistors on it 1M and 10K resistors to drop 12V DC down to .12V DC to be read by the ADC chip.... Problem I am having is the calculation is off.

When the battery is read with a meter it shows 12.12V DC but the system calculates it at 13.76V DC and the readings are not consistent, voltage fluctuates each reading.

Any help would be most appreciated.
Jeff

The code we are using was found on the web, and nobody remembers where.

String getBatteryVoltage() {
float voltage = 0.0; int sensorValue;
sensorValue = analogRead(A0); // read the input on analog pin 0:
voltage = sensorValue * (3.2 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3.2V):
voltage = voltage * 100;
return String(voltage);
}

IMAG0022.jpg

IMAG0021.jpg

WEMOS-IAT-ADC-Voltage-Divider-v1.0.jpg

What is the source of the 3.2V analog reference voltage?

The voltage reference for the esp8266's analog input is 1.0V according to everything I've read. It is not Vcc (=3.3V). The Wemos boards have a voltage divider consisting of 220K & 100K, so that it can accept voltages up to 1.0*(220+100)/100 = 3.2V.

If you want to increase this range, you can just put another resistor in series with the Wemos analog pin. In effect this increases the value of the on-board 220K. For example if you put 180K in series, the voltage divider becomes 400K + 100K, so you can measure voltages up to 5.0V.

+1
Don't use a voltage divider. Just use a single resistor between battery and analogue pin.
Use 100k per volt, with a threshold of 3.2volt.
1Megohm can do 10 + 3.2 = 13.2volt.

Just use a simple formula, that you can calibrate.

float voltage;

voltage = analogRead(A0) * 0.01289; // calibrate by changing the last digit(s)

Note that the analogue input of an ESP8266 is not that great.
I had one return 8-1024 instead of 0-1023 (not a typo).
Leo..

If we install 1Megohm in series what heppens when the user has more than 12V dc?
The Wemos Power Shield can accept 7-24V DC. I don't want to burn up the ADC on the module.

With 1M, voltages of up to 13.2V can be read. Voltages over 13.2V will also read 13.2V, because the voltage at the esp pin will be more than 1.0V.

To avoid damage to the esp pin, the voltage at the pin on the esp chip itself must not exceed 3.3V. This corresponds to an input voltage to your circuit of 3.3 / 100K * (1M + 220K) = 40.26V

Someone please correct me if I am wrong about this!

So, If I read this correctly, using a 1M resistor in series on the ADC pin I can accept up to 40.26V input, but anything over 13.2v will read as 13.2v?? Correct? and this will not hurt the ADC pin?

I do agree with PaulRB, although I have never tried it.
It should actually be higher than 40.26volt if you add the pin protection threshold (~0.5volt) to that 3.3volt.
And >= 1Megohm limits fault current to a tiny value.
Leo..

As you were so concerned, I searched for some reference to back up my belief. But I could not find anything 100% conclusive.

Many web pages say that the maximum input voltage on any esp8266 pin is 3.6V.

It is generally accepted to be a bad thing to apply a voltage to any MCU's pin that is higher than Vcc, the power supply voltage. So while 3.6V is given as the limit, I would expect that is only if the supply is also 3.6V. If the supply is 3.3V, as it is for Wemos because of the on-board 3.3V regulator, then the max voltage you could apply to any pin is 3.3V.

But if you look for confirmation that "any pin" includes the analog input pin, its hard to find anything that isn't vague or ambiguous.

So all I can say is that, in theory, you could go up to 40.26V.

Thank you all for the help.The 1M seems to be working just fine.
Calculation seems to be almost spot on... off by a fraction but we can live with that.

Thanks again,
Jeff