I'm using ESP32 to read an analog value of a liPo battery (3.7v-4.2v).

Since ESP32 DAC's is 3.3v I'm using a voltage divider ( 2x10K resistors ), to split bat's voltage into ESP's GPIO36. So far nothing new or not well documented on the web.

Using the voltage divider, splits the 4.0v into 1.97v (measured using a volt-meter). Battery's negative pole is connected to ESP's Gnd to get a common ground. GPIO36 is connected to the 1.97v line ( and this value is tested using a volt-meter also on ESP's pin to make sure ).

I was expecting to get a digital value of 1.97 (2444 )


when using analogRead(36) I'm getting a digital value of 2180 which correlates to 2180/4095*3.3 = 1.75v.

1) I tried to make a 100 samples over time - to make sure it is not a power up issue. No change.

2) I tried to read its value during loop() and not setup(). No change.

Is DAC's resolution is that poor ( roughly 12% off ), or am I doing something wrong ?

  • 1
    Did you measure the actual supply voltage of the ESP32? It's not necessarily exactly 3.3V. Also, the ADC in the ESP32 can be off by + or - 6%, IIRC, and it isn't linear either; it needs calibrating before using. – ocrdu Jan 18 '20 at 18:12
  • @ocrdu - Yes, it measures 3.29v on ESP (close enough ). Do you think 12% is a calibrations error ? seems to me a bit too much ( 5% sounds reasonable ). – Guy . D Jan 18 '20 at 19:52
  • try connecting the GPIO pin to the supply voltage of the ESP32 (3.3V) through a resistor and see if it becomes the maximum value. Maybe there is some flux left on the pin or something that affects it's response to voltage. – Coder_fox Jan 19 '20 at 12:02
  • @Coder_fox - Did already connect directly 3.3v pin directly to 36 pin (not using a resistor though ), and got 4095 as expected. – Guy . D Jan 19 '20 at 18:14
  • 2
    it looks like it should be about 2200 to hit 1.97v: i2.wp.com/randomnerdtutorials.com/wp-content/uploads/2018/08/… The absolute accuracy actually doesn't much matter, you just have to figure out what your MCU spits out. feed the ADC 1.00v and 2.50v, take the readings, then calc the code/volt slope. you can then read any battery voltages quite accurately between 2 and 5 volts: it's pretty linear in the middle range like that. – dandavis Jan 20 '20 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.