analogReference(EXTERNAL)

hi all,
i m trying to read to the input from analog i/o and show on serial.
when i m using analogReference(EXTERNAL) function then it gives me only highest values never change that values, can anyone debug my code.

i think may be ADCMUX will be reset that would be possibility but i ll try on that way also but didn't get anything. anyone also suggest me actually what happens?? in this code.

int a = A1;
float b = 0;

void setup()
{
  Serial.begin(9600);
  analogReference(EXTERNAL);
}

void loop()
{
  b = analogRead(a);
  Serial.print("PRESSURE = ");
  Serial.println(b);
  delay(1000);
}

A1's pinMode = INPUT ?

yesh that is mistake but also same output.......

int a = A1;
float b = 0;

void setup()
{
  Serial.begin(9600);
  analogReference(EXTERNAL);
  pinMode(a, INPUT);
}

void loop()
{
  
  b = analogRead(a);
  b = map (b,0,1024,0,5);
  Serial.print("PRESSURE = ");
  Serial.println(b);
  delay(1000);
}

erm, i may be wrong here, being newbie and all

but referring to the analog pin as A1... doest that make it behave as a digital pin ?

shouldn't it be # int a = 1; #

yesh that is mistake

No, it isn't. There is no way to set the mode for analog pins, because they are input only, by design.

When you use the analogReference() function, with EXTERNAL as the argument, the ADC compares the measured voltage on the analog pin to the voltage on the VRef pin. What do you have supply voltage to the VRef pin? What voltage is it?

PaulS:

yesh that is mistake

No, it isn't. There is no way to set the mode for analog pins, because they are input only, by design.

When you use the analogReference() function, with EXTERNAL as the argument, the ADC compares the measured voltage on the analog pin to the voltage on the VRef pin. What do you have supply voltage to the VRef pin? What voltage is it?

The "analog" pins 0 to 5 are also general purpose digital I/O pins (numbered 14..19) so can be set as outputs. A6 and A7 (only on surface mount 328's) are input only.

You are right that the original problem is a lack of an external reference (BTW it has to be at least 1.1V or something like that for the ADC to behave in spec).

There is a issue here - when you press reset the Aref goes back to being internally connected to Vcc so that when you supply an external voltage to it it must be via a current-limiting resistor. On the Arduino there is already a decoupling capacitor on Aref.

@tomperdarwin---- i tried your idea but didn't get work...any other idea???

@PaulS-- i have "ARDUINO MEGA ADK" for that i m applying 5V current with 1k variable register. so i can get ADC signal.

@MarkT--- i get your point but i dont get any idea how could i get over from this problem.

when i remove analogReference(EXTERNAL) this function or set default this code work properly so anyone tell me why this is happening????

int a = A1;
float b = 0;

void setup()
{
  Serial.begin(9600);
  //analogReference(EXTERNAL);(Remove this function)
  pinMode(a, INPUT);
}

void loop()
{
  
  b = analogRead(a);
  b = map (b,0,1024,0,5);
  Serial.print("PRESSURE = ");
  Serial.println(b);
  delay(1000);
}[code/]

If you remove the analogReference(EXTERNAL), the Arduino uses it's internal 5V reference.

There is a issue here - when you press reset the Aref goes back to being internally connected to Vcc

Actually the default state of the register should be set to EXTERNAL after a reset.

wayneft:

There is a issue here - when you press reset the Aref goes back to being internally connected to Vcc

Actually the default state of the register should be set to EXTERNAL after a reset.

No, that is not correct:

retrolefty:

wayneft:

There is a issue here - when you press reset the Aref goes back to being internally connected to Vcc

Actually the default state of the register should be set to EXTERNAL after a reset.

No, that is not correct:

analogReference() - Arduino Reference

What they are saying is that by default if you perform an analogRead without specifying a reference it will default to DEFAULT. The analogReference variable is set to DEFAULT by default but does not actually change the ADMUX register until the first analogRead takes place (probably for safety reasons just in case someone does have an external voltage applied to the AREF pin).

We can find out for sure by simply looking at the ADMUX register after a reset and see how REFS1 and REFS0 are set (maybe I'll do this a little later if I get a chance).

wayneft:

retrolefty:

wayneft:

There is a issue here - when you press reset the Aref goes back to being internally connected to Vcc

Actually the default state of the register should be set to EXTERNAL after a reset.

No, that is not correct:

analogReference() - Arduino Reference

What they are saying is that by default if you perform an analogRead without specifying a reference it will default to DEFAULT. The analogReference variable is set to DEFAULT by default but does not actually change the ADMUX register until the first analogRead takes place (probably for safety reasons just in case someone does have an external voltage applied to the AREF pin).

We can find out for sure by simply looking at the ADMUX register after a reset and see how REFS1 and REFS0 are set (maybe I'll do this a little later if I get a chance).

I went ahead and wrote a quick sketch and the ADMUX register defaults to a value of zero after a POR which is a setting of EXTERNAL. I also changed the values to both INTERNAL and DEFAULT and hit the reset button and it resets it back to zero again.

I would think that more important is what is the value of REFS1 and REFS0 if one performs a analogRead() statement without having performed any prior analogReference(type) command? It's the default of the analogRead() function/library that is more important rather then the hardware reset default condition and the reason it's so important to perform the analogReference(type) command before the very first usage of analogRead() command if it is to be other then using the default Avcc pin's input voltage as the reference.

Lefty

thanks to all to give your valuable suggestions

i think when i m calling analogReference(EXTERNAL) function before analogRead() then they would not config with other. i mean to say may be when we call analogRead() function then REFS1 & REFS0 must be reset that's why it's give us maximum values.
apart this if we didn't call ADC library and just make myself then it's work, so i think may be any other way to work with this library or have some bug in that.