1

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.

asked May 20, 2015 at 4:39
4
  • How are you initializing ADCSRA? Commented 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. Commented May 20, 2015 at 7:37
  • Ignacio, are you suggesting that I need to do more than twiddle a single bit in ADCSRA? Commented May 20, 2015 at 7:39
  • 1
    You might find this of interest: hacking.majenko.co.uk/making-accurate-adc-readings-on-arduino Commented May 20, 2015 at 10:53

1 Answer 1

0

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.

answered May 23, 2015 at 0:20
1
  • Can you accept this answer please so that StackExchange doesn't keep "bumping" it in the hope someone will give an accepted answer. :) Commented Aug 21, 2015 at 6:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.