Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By donbrew
#95197 Using a D1 mini for weather station. Battery monitoring works pretty good with the batter+ connected to A0 via 100k resistor in other sketches.
When I use D1 and D2 for I2c connected to BME280 and a BME280 the A0 readings get erratic, some times the reading goes over 4.2 volts when the actual voltage is 4.0 the A0 reads 3.3 volts and other crazy readings the lower the actual voltage goes. Using a DVOM for actual readings.

I tried using an ADS1115 and that was even crazier.

I tried getting 10 readings at 50 msec intervals and averaging. No luck.

The code I'm using is
Code: Select allvolt = (analogRead(0) * 4.2) / 1023;

And every variation I can think of.

Is there some bit of code to fix it?
User avatar
By donbrew
#95209 Because Li-Ion battery is a maximum 4.2 volts. I am measuring the battery not the regulator output. If I wanted to measure the voltage supplied to the NodeMCU there is an easier way.
Code: Select all ESP.getVcc()


I am aware that the analog reading is not linear, but this is a ridiculous non-linearity.
User avatar
By donbrew
#95210 I think I found the solution buried in the docs.

I need to declare
Code: Select allADC_MODE(ADC_TOUT);


for getting Vcc
Code: Select allADC_MODE(ADC_VCC);


That resets the 107th bit to the correct value.

I guess I should explain the equation I am using:
the ADC reads 1023 pieces of digital data
the input is 0 to 1.0 (analog 0 = digital 0, analog 1.0 = digital 1023 )
the max voltage of a 18650 battery is 4.2 volts

So, (4.2 max battery volts) Times ((ADC reading) Divided by 1023) Equals the human readable value

I am not a teacher or expert, please excuse the inaccurate language.