I'm reading the voltage with voltage divider:
simulate this circuit – Schematic created using CircuitLab | Arduino is connected to both USB and a 9V adapter.
float r1 = 100000;
float r2 = 9980;
float adc, voltage, analog;
void setup() {
Serial.begin(9600);
analogReference(EXTERNAL);
pinMode(A0, INPUT);
}
void loop() {
for (int i = 0; i < 256; ++i) {
analog += analogRead(A0);
}
analog = analog / 256;
adc = analog * 1.228 / 1023;
voltage = adc / (r2 / (r1 + r2));
Serial.print(analogRead(A0));
Serial.print(", ");
Serial.print(analog);
Serial.print(", ");
Serial.print(adc);
Serial.print(", ");
Serial.print(voltage);
Serial.println(" ");
delay(100);
}
Here's the accuracy test result:
Input | Arduino | Difference
1V 0.89V -110mV
2V 1.90V -100mV
3V 2.92V -80mV
4V 3.93V -70mV
5V 4.95V -50mV
6V 5.97V -30mV
7V 6.98V -20mV
8V 8.00 0
9V 9.01 +10mV
10V 10.02 +20mV
11V 11.04 +40mV
12V 12.06 +60mV
The input voltages measured by DMM and was spot on voltage, like 1.000V.
I didn't test voltages above 12V but it seems the difference is gonna increase by 20mV with each 1V increase, that means at 50V voltage reading is gonna be off by ~1V!
Even with 10bit resolution of Arduino I should have had steady accuracy with some percentage of difference(right?), but as you can see the result shows that the reading is not steady at all!
Why the voltage difference decrease/increase when voltage changes at the input?