strange changing analog Reference behaviour


I have a program on the Arduino UNO Board that reads one analog pin with analogRefernce(DEFAULT), if this value ist below 200 it changes the analog reference to INTERNAL and reads the value again, to spread the accuracy. But this does not work as expected I always get nearly the same value as with the DEFAULT-Reference and not as expected an approx. four times value.

After experimenting for a while and finding something related here in the discussion boards, I did a for loop for the second reading that reads the value with the INTERNAL-Reference twenty times and printing it to serial console. The measured values are starting with an value like the reading with the DEFAUL-Reference and then increasing with each reading. Reaching a stable value as expected about 4 times the first reading after 7 to 10 readings. So I use this for loop as an workaround only using the last value I got after changing the analogReference.

Can someone explain me why this happens? Is it the same for other arduino boards?

If this is by hardware design and not a bug in the firmware. It should be mentioned with an example on the documentation page for analogReference();. I had some very hard hours before finding my solution, thinking i did something wrong.

What if you put a big delay (1-2 seconds) between changing the reference and the first reading? Same behavior?

I tried it with a delay of 500ms that was not enough. I will give it tomorrow another try with some increasing delays to see what will happen. Anyway either behaviour and workaround should be mentioned in the reference.

I've done a lot of testing with this... I was trying to make an autoranging ammeter.

It only takes about 5.5 ms for the reference voltage to switch from DEFAULT to INTERNAL, this delay is because of the 100nF cap on the AREF pin. In your loop you will actually have to throwaway the first 50 readings or so before you get a stable reading.

Also keep in mind the stabilization time does not start from when you change analogreference, it starts from the first analogread after you change the reference.

I added a note about this to the analogReference() page: If you can suggest more precise wording based on the datasheet or observed behavior, I can update it.

Hello thank you for adding it to the reference page. I suggest to add some more words. I am not a native speaker an expert. But from what I got in the mentioned threads. Something like your sentence plus added:

A safe way after changing the reference ist to do an analogRead wait for 6ms with delay and then the next reading should be fine.

would be great.