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.