ADC library problem.

A warning when using the ADC library:

Every time you change the ADC MUX or especially the reference, you need about a 5 msec delay for the analog levels to settle. Refer to the source code for analogRead() and you can see that the author has fiddled with delays and experienced problems.

If you do this:

someVoltage1 = analogRead(SOMEVOLTAGE_1_SENSE_PIN); someVoltage2 = analogRead(SOMEVOLTAGE_2_SENSE_PIN);

someVoltage 2 is likely to read incorrectly because it will change the ADC mux and then start reading the ADC with no delay. This error manifests itself as showing the value from the previous channel or an incorrect value. Presumably there are some high impedance nodes still charged up somewhere. You need to do this:

[code]someVoltage1 = analogRead(SOMEVOLTAGE_1_SENSE_PIN);

// this changes the mux and loads the reference setting into the actual ADC register
// junkReading may hold invalid values
long junkReading = analogRead(SOMEVOLTAGE_2_SENSE_PIN);

// let the MUX and reference settle
delay (5);

// someVoltage2 will now contain the correct value
someVoltage2 = analogRead(SOMEVOLTAGE_2_SENSE_PIN);[/code]

Likewise at start up - if you do this:

analogReference(INTERNAL); // does not actually change the ADC register someVoltage1 = analogRead(SOMEVOLTAGE_1_SENSE_PIN); // the ADC register is changed here

someVoltage1 value may be incorrect as the reference isn’t actually changed until you do the analogRead() call. The following will not solve the problem:

analogReference(INTERNAL); // does not actually change the ADC register delay(5); // this does not work someVoltage1 = analogRead(SOMEVOLTAGE_1_SENSE_PIN); // the ADC register is changed here

Once again you need to do a dummy read, then delay, then do the actual read.

Thanks,

I sent you an email…

Cheers
Henrik