I would like to read MUX 14 with the ADC. On page 263 of the Atmega328 data sheet it shows 1110b on the MUX corresponding with reading V_BG.
I need to read this so I can calibrate my sensor readings taken with the 5v reference to those taken with the 1.1v reference. Because the 5v power supply may vary and the 1.1 is more stable, I need to be able to translate the readings taken with a 5v reference based on how the internal 1.1v is read.
Based on the code for analogRead, I wrote this:
int readVBG(int _analogReference) {
uint8_t low, high;
ADMUX = (_analogReference << 6) | 14;
sbi(ADCSRA, ADSC);
while (bit_is_set(ADCSRA, ADSC));
low = ADCL;
high = ADCH;
return (high << 8) | low;
}
_analogReference is either 1 or 3 depending on the reference voltage desired.
I expect to read 1023 when _analogReference is 3 and about 225 when _analogReference is 1. Instead I get smaller values which I'm guessing are from a floating (unconnected) analog pin.
-
How are you initializing ADCSRA?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams05/20/2015 06:51:18Commented May 20, 2015 at 6:51
-
As I understand it, these are registers on the atmega328. If they were just variables, then the loop would never exit.MattD– MattD05/20/2015 07:37:03Commented May 20, 2015 at 7:37
-
Ignacio, are you suggesting that I need to do more than twiddle a single bit in ADCSRA?MattD– MattD05/20/2015 07:39:02Commented May 20, 2015 at 7:39
-
1You might find this of interest: hacking.majenko.co.uk/making-accurate-adc-readings-on-arduinoMajenko– Majenko05/20/2015 10:53:47Commented May 20, 2015 at 10:53
1 Answer 1
The problem is that a 2ms delay is required after setting the ADMUX bits before starting the conversion. Thank you Majenko for the link.
int readVBG(int _analogReference) {
uint8_t low, high;
ADMUX = (_analogReference << 6) | 14;
delay(2); // Wait for voltage to settle
sbi(ADCSRA, ADSC);
while (bit_is_set(ADCSRA, ADSC));
low = ADCL;
high = ADCH;
return (high << 8) | low;
}
The above code is specifically for the Arduino UNO. Majenko's link above has #ifdef statements which support other devices.
-
Can you accept this answer please so that StackExchange doesn't keep "bumping" it in the hope someone will give an accepted answer. :)08/21/2015 06:15:24Commented Aug 21, 2015 at 6:15