Want to make an adaptive voltmeter with the Arduino based upon the following idea;
I start with measuring with an analogReference of 5.0 volt and if the voltage is less than approx 1 volt (raw 200) , I switch the analog reference to INTERNAL and make a new measurement which should be ~5x more precise, => to get that 3rd digit . In short the analogRead would adapt its range automatically to the voltage supplied.
Written a sketch and it sort of works. The problem I encounter is that it takes quite a while (many reads) before the new analog reference "stabilizes". In the sketch below I use 64 reads to be sure it stabilizes. I have not tried yet to find the optimum nr of reads yet but 8 reads was too few. IN my next try I will make that adaptive too.
Q: is there a method known to get the analogRead() stable much quicker after a switch of the analogReference()?
//
// FILE: volts.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE:
// DATE:
// URL:
//
// Released to the public domain
//
void setup()
{
Serial.begin(115200);
Serial.println("Start ");
Serial.println(voltage(A0), 4);
}
void loop()
{
Serial.print(millis());
Serial.print("\t");
Serial.println(volts(A0), 4);
delay(500);
}
float voltage(uint8_t pin)
{
int raw;
analogReference(DEFAULT); // 5.0
for (int i = 0 ; i < 64; i++) raw = analogRead(pin);
Serial.print(raw);
Serial.print("\t");
Serial.print(raw * 5.0 / 1.1); // expected raw for 1.1V
Serial.print("\t");
if (raw < 200)
{
analogReference(INTERNAL); // 1.1
for (int i = 0 ; i < 64; i++) raw = analogRead(pin);
Serial.print(raw);
Serial.print("\t");
return raw * 1.1 / 1024;
}
Serial.print(raw);
Serial.print("\t");
return raw * 5.0 / 1024;
}
I am also thinking about connecting the externalReference pin to a PWM output of the Arduino with a low pass filter so I can set the analogReference(EXTERNAL) to 0 ... 5.0 Volts in 255 steps. Probably just use 5 or 10 levels in practice. But that is another experiment.
But if the above does not work well, this PWM experiment makes even less sense