# Converting from ADC to real voltage

I'm a bit confused from the 3.3 V after the voltage divider. Basically I have a voltage can vary from -12 V to +12 V on input and ADC is reading values from 0 to 4095.
This means theoretically that nearly 0 is -12 V, 2047 is point 0 V, 4095 is 12 V.

In reality I'm getting weird values as it seems the ADC is not linear. Maybe I'm using the wrong formula to convert? How should it be converted then?

-12 V 0
0 V 2047
+12 V 4095
• It won't be linear because of the resistor to the 3.3v. Take that out if you want to measure the 12v inputs. Commented Feb 23, 2023 at 13:52
• @user1850479 a resistor won't change linearity. The 56k + 66k provide a Thevenin voltage of about half rail and 30k to bias the potted down 12 into the 0-3 V range. Commented Feb 23, 2023 at 14:05
• Sorry, I missed the -12v minimum and thought he was trying to measure only positive values. Ignore that remark. Commented Feb 23, 2023 at 14:07
• 'I'm getting weird values' doesn't give us much to go on. Put a table in your question of voltages and readings, at least 5 of each, the more the better. Better still, edit the one I've started for you, changing the second column to 'actual ADC values' Commented Feb 23, 2023 at 14:09
• Like Neil_UK suggested, if you look at it with Thevenin's you can get the equation you're after: $V_{OUT} = \left ( 3.3+\left ( 56000*\left ( \frac{V_{1}-3.3}{256000} \right ) \right ) \right )*0.603175$ Where $V_{1}$ is your ADC input. So the readings you're getting are about right Commented Feb 23, 2023 at 15:32

I suggest you avoid the top and bottom regions of the range (100mV or so at the chip).

This is complicated by the fact that the actual Expressif ADC input works from a nominal 0 to 1.1V or so (but it could be 0 to 1.0V or 0 to 1.2V because the reference is not tightly specified). So you are using a module that has some resistors on it (another voltage divider), which will affect your voltage divider calculations.

Then there is reference tolerance (+/-10%), power supply tolerance (used for offset) of maybe +/-5%, resistor tolerances of a few percent in total. If you try to calibrate it by applying a voltage that causes the reading to saturate (0 or 4095) the calibration will be invalid. You might try +/-10V in for calibration.

• This was the problem!! I changed the 200k resistor with a 300k resistor and now I can correctly read the values. means the ESP32 is buggy!! Commented Feb 27, 2023 at 9:17

The easiest way to see if the ADC is producing the values you expect is to set up a test harness, like this:

simulate this circuit – Schematic created using CircuitLab

Here you may present the ADC with any potential from -12V to +12V, selected with a potentiometer. By measuring the actual ADC input potential with a voltmeter, VM1, and comparing that with the ADC conversion result, you may determine if the ADC is the problem.

In your case, the network of resistors is certainly not producing anything in the range -12V to +12V, and I am not sure what that network has to do with this problem. Just measure the ADC's actual input potential, and see what the ADC says is there.

With all that out of the way, I would point out that your understanding of what ADC result should correspond to -12V, 0V and +12V may be incorrect. If the ADC uses two's complement to represent the voltage, then what you should expect is:

000000000000: 0V

011111111111: +12V

100000000000: -12V

Maybe that's what's making your numbers look wrong, when actually they are correct.