0
\$\begingroup\$

I would need to calculate initial voltage from ADC reading.

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?

enter image description here

voltage ideal ADC reading
-12 V 0
0 V 2047
+12 V 4095
\$\endgroup\$
8
  • \$\begingroup\$ It won't be linear because of the resistor to the 3.3v. Take that out if you want to measure the 12v inputs. \$\endgroup\$ Commented Feb 23, 2023 at 13:52
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – Neil_UK
    Commented Feb 23, 2023 at 14:05
  • \$\begingroup\$ Sorry, I missed the -12v minimum and thought he was trying to measure only positive values. Ignore that remark. \$\endgroup\$ Commented Feb 23, 2023 at 14:07
  • 1
    \$\begingroup\$ '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' \$\endgroup\$
    – Neil_UK
    Commented Feb 23, 2023 at 14:09
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Mukira
    Commented Feb 23, 2023 at 15:32

2 Answers 2

0
\$\begingroup\$

The ESP32 has a free ADC, so it's good ADC value for money, but it is a markedly bad "12-bit" ADC.

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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!! \$\endgroup\$
    – mapomapo
    Commented Feb 27, 2023 at 9:17
0
\$\begingroup\$

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

schematic

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.