2

I have an Arduino device and I'm trying to read two voltages. The first one is the Arduino Vcc by using the bandgap value 1.1V (this requires raw access to registers since it's not implemented in analogRead). The other one is checking the battery voltage using the badgap as a reference. I want both of them because I'm doing a study on the battery life when using a step-up converter.

I'm using these pieces of code:

// Read bandgap with Vcc as reference - this will give use the Vcc
ADMUX = DEFAULT << 6 | 14;
delayMicroseconds(250);
bitSet(ADCSRA, ADSC);
while(bit_is_set(ADCSRA, ADSC))
    ;
word x = ADC;
Serial.println(x);


// Read A1 with 1.1V bandgap as reference - this will give battery voltage
ADMUX = INTERNAL << 6 | 1;
delayMicroseconds(250);
bitSet(ADCSRA, ADSC);
while(bit_is_set(ADCSRA, ADSC))
    ;
word x = ADC;
Serial.println(x);

The problem that I have is that separately they seem to work well, however when using them together the first piece of code ruins the reading for the second one. The culprit is the line ADMUX = DEFAULT << 6 | 14. This sets up the default reference (chip voltage) and reads channel 14 (which is the 1.1V). I can't figure out why after using this line changing back with ADMUX = INTERNAL << 6 | 1 doesn't work.

3
  • 1
    Did you try taking the reading twice? The datasheet says: “The first ADC conversion result after switching reference voltage source may be inaccurate, and the user is advised to discard this result.” Mar 5, 2015 at 15:48
  • Yes, I ran them in a loop and get consistent results. The first one read correctly, but then the second one won't work. If the first one is commented, the second one works OK.
    – rslite
    Mar 5, 2015 at 16:03
  • Actually you are right, I didn't understand your comment clearly the first time. I loop both reads one after another so it doesn't help. Looping the second one gets to the right value after a while (~5 ms) so I need to read once then wait 5 ms and read again. Please put your comment in an answer so I can accept it.
    – rslite
    Mar 5, 2015 at 17:19

1 Answer 1

3

The datasheet says:

The first ADC conversion result after switching reference voltage source may be inaccurate, and the user is advised to discard this result.

Then you could try to take each reading twice, looping over four ADC readings:

  1. Read bandgap with Vcc as reference, discard the value.
  2. Re-take the same, keep the value.
  3. Read A1 with 1.1V bandgap as reference, discard the value.
  4. Re-take the same, keep the value.
1
  • 1
    I also want to add this as reference - I found some more info in this thread: forum.arduino.cc/index.php?topic=22922.0 which confirmed my suspicion that the Aref capacitor discharge time might be messing up with the reads. They got values very close to my 5 ms wait time.
    – rslite
    Mar 5, 2015 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.