Weird "buffer" value

Hi,

I am using an Arduino Mega; on which I connected a LED on pin 8, a photodiode on A13 and a couple of photodiodes plugged in series on A15.
I am running this simple code in the Arduino IDE 2.0 :

void setup() {  
  Serial.begin(115200);
  pinMode(8,OUTPUT);
  pinMode(13,INPUT); pinMode(15,INPUT);
}

void loop() { 
  int x=0; 
  digitalWrite(8, HIGH); delay(100); 
  x=analogRead(A13); Serial.println(x);  
  //delay(1000);
  x=analogRead(A15); Serial.println(x); // changing pin number
  x=analogRead(A15); Serial.println(x);
  x=analogRead(A15); Serial.println(x);
  delay(10000000);
}

And what I get :

60
337
481
481

There is always this "buffer" value (337 here) before analogRead(A15); gives the right one (481).
The problem doesn't appear if I skip analogRead(A13) and start right away with A15.
The presence of the delay() in between the 2 analogRead() from pin A13 and A15 doesn't change anything (even with a long time like 10 seconds).
I tried a few things, but no result so far :
Instead of an int, I tried with float and double.
I used 2 different variables (int x for A13 and int y for A15) in case it would be a memory problem.
I unpluged and repluged my leds/sensors on an Arduino Mega clone.

I could simply write a blank analogRead(A15) to discard the wrong value before reading the actual one, but it wastes a -tiny- bit of time, and I would prefer to understand what is wrong.

Is there something obvious that I missed here? If you have any suggestion...

Thanks

When you analog read from a different pin, it is suggested to do so twice and discard the first value.

Evidently no matter how long between the switch.

I am sure you could get to the very bottom of this, but it is a common problem and this is the solution.

a7

If the input impedance is too high the sample/hold capacitor will not fully charge or discharge to the new input voltage before the sample is held. The delay doesn't make a difference because the input to the sample/hold capacitor isn't changed until you switch to the new pin. Doing a dummy read will help if the impedance is marginal. Using an op-amp as a voltage follower to drive the analog input should also work.

Okay! I didn't know how it works, but it makes sense.

I will go with the easiest solution, just adding another analogRead() in between.
Thanks for your answers

What is wrong is that the source impedance is too high for the ADC. It should have impedance less than 10K.

Tossing one analog read read will not give you the correct answer if the source impedance is too high. One solution is to use a unity gain buffer amplifier (a very simple op amp circuit).

Or, if you don't expect light levels to change rapidly, add a 1 nF capacitor between the analog input and GND, and give it plenty of time to charge between readings.

All right, I will give it a try. Thank you

See if this mod works better.