I have been using Arduino Nano Analog input to measure voltages, in the range between 22-30 Volts.
I want to measure it down to a tenth of a volt.
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. Clearly they are presuming an input of 0-5 Volts.
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.
(The biggest problem was the reference voltage and resolution were assumed to be 5/1024 which is completely incorrect. Unless, as someone pointed out, I fed the Nanos directly at the 5V pin - which is still on the table - then it would be 5/1023)
I changed my resistor network to be 147K/23.5K
(Parts list: 1x 100K, 3x 47K)
R1 = 47K / 47K = 23.5K
R2 = 100K + 47K = 147K
Also added a 100uF 50V electrolytic across the input from the battery bank I am monitoring.
I am feeding 5V to Vin. And have figured out that the reference is measurable on the 5V pin, after the regulator, as 4.6V.
Resolution/Vref = ADC/Vin
I turned that around to be
Vin = ADC * (Vref/Resolution)
Or, Vin = A2 * (4.6/1023)
Using a laboratory power supply I was able to confirm that with those settings 22 Volts came in at 3 Volts, and 29.98 came in right at 4 Volts.
Vbat = A2 * (4.6/1023) / (R2/(R1+R2))
Vbat = A2 * (4.6/1023.0)/(147000/170500)
It is now operating reasonably well.
(Yellow means the inverter is on)
--UPDATE-- Changed resistors to 100K/20K for the divider and brought power to the 5V pin, bypassing the regulator for a reference. It measures at 5.02 and is stable.
Charge controller meter says I am at 24.5 Volts.
Program says 24.64 (in the serial monitor).
--ADDENDUM: I want to pursue the Op-Amp setup. But if 741 won't work will need a suggestion.
Similarly, I went to 147K/47K for the 11-15 Volt circuit, and moved a higher-quality P/S over to the 5V pin, where it measures 5.03V; the results are better there, too. Just very slightly high.
So anyway, I'll put this to rest until I can figure out an op-amp circuit.
Thank you all for your help