We have moved at community.getvera.com

Author Topic: ADC library problem.  (Read 1248 times)

Offline a-lurker

  • Hero Member
  • *****
  • Posts: 872
  • Karma: +66/-8
ADC library problem.
« on: March 20, 2014, 10:55:55 pm »
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:
Code: [Select]
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: [Select]
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);

Likewise at start up - if you do this:

Code: [Select]
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:

Code: [Select]
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.

« Last Edit: March 21, 2014, 04:21:22 am by a-lurker »

Offline hek

  • Moderator
  • Hero Member
  • *****
  • Posts: 714
  • Karma: +60/-1
Re: ADC library problem.
« Reply #1 on: March 21, 2014, 04:06:45 am »
Thanks,

I sent you an email...

Cheers
Henrik