I have been using Arduino Nano analog input to measure voltages, in the range between 22-30 Volts. The Nano's ADC expects a 0-5 volt range and has 0-1023 as its output.
Following the instructions found here:
Read Analog Voltage (www.arduino.cc)
Which basically states that all I need is this code:
int sensorValue = analogRead(A0);
float voltage= sensorValue * (5.0 / 1023.0);
5.0/1023 = 0.004888 volts per increment in analog readings. Turning that around, voltage input divided by this value should give me the sensor value.
My voltage divider is 1 Mohm (R1) between the voltage input and A2, then 200K (R2) from A2 to ground
30 Volts measures as 4.2 Volts. 22 Volts measures as 3.1 Volts.
I think the range between 22 volts and 30 Volts doesn't present enough variation to the ADC - every single-digit change in the analogRead really make a big difference.
The result is that it is giving me some crazy readings. It is definitely nonlinear, so I am having real trouble trying to get it to be accurate at both ends of my measurement range. Each slight ADC increment makes too much of a difference in the measured voltage because I am not using the full 0-5V input range.
So I have been taking 10 readings in a row, at 500 ms intervals, then averaging.
And yet it is not working well enough.
One thing that seems to really matter is the power supply. When I put 4.97 into the Vin it gets different readings than if I rely on getting power from the USB connection to the Raspberry Pi 3B.
Just yesterday I put it on the power supply for good - had been relying on the Pi all along. But it still is not giving accurate readings.
--EDIT from Answers (Thank you for showing me where to look)
Input impedance: I'm trying 147/23.5 for the next attempt. Getting 3.0-4.1 for the 22-30 range. Same as the 1M/100K, basically, but lower resistances.
The reference voltage issue: It measures 4.36 Volts on the 5V output from the Nano. Vin is 5V exactly.
Here is an article I found that says the 5/1023 formula should use that actual value:
Measuring DC Voltage using Arduino
And another that confirms it:
SO NOW the immediate problem is how can I map 22-30 volts to 0-4 or so, thus allowing the utilization of more than just a few % of the allowable values?
Cross-posted at How can I seriously calibrate ADC voltage readings with Arduino Nano?