ADC problem, where is the voltage come form?

Hello all:

I didn't some test with my Duemilanove today for ADC function,

I don't understand why after I change A6 pin to input mode, change the value to LOW,
and then I call analogRead function, it show me the input voltage is abouyt 1v? (I didn't plug any external Vcc to it)

But if I change the pind mode to "OUTPUT" mode, then every thing become normal (0v), even the ADC function is still work!

This is so strange!

Did I do something wrong? Could anybody give me show hint?

Thanks.

my test code shawn as below

int sensorPin = A5; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT); // << INTPUT or OUTPUT
digitalWrite(sensorPin, LOW);

Serial.begin(9600);
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for milliseconds:
delay(sensorValue); // turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for milliseconds:
delay(sensorValue);

Serial.println(sensorValue);
}

  pinMode(sensorPin, INPUT);  // << INTPUT or OUTPUT

This is NOT affecting the analog pin at all. It ONLY affects digital pins.

I didn't plug any external Vcc to it

Then you should not be reading it, or should have NO expectation about what the function will return.

But don't you think it should always return 0 if there is no any input voltage to ADC port?

So if I connect wire with very low voltage (say 0.1), can I get the ideal input value? or it may still high than 1v?

Thanks for your replying first!

But don't you think it should always return 0 if there is no any input voltage to ADC port?

No, I do not. Nor does ATMEL.

I think you are right, after I connect the ADC pin to GND, the measurement value become zero.
Please forget me if this is stupid question.
Thanks

If you set the pin to a digital input and nothing is connected to it because you turned off the internal pullup resisitor with these lines

pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, LOW);

then the pin is "floating" and the value you get back will be random.

I don't know if the arduino software does some smart stuff so you get valid analog results back from a digital input pin.

rollingbug:
But don't you think it should always return 0 if there is no any input voltage to ADC port?

No, a pin without anything wired to it is said to be a floating input and will read random electrical]
noise values.

So if I connect wire with very low voltage (say 0.1), can I get the ideal input value? or it may still high than 1v?

Wire a jumper from a ground pin to the analog input pin, the value returned by analogRead() should be 0 counts. Then move the jumper to the shield 5V pin and the analogRead() should return 1023 counts, if so you have no problems reading analog input voltages.
Lefty

Thanks for your replying first!

rollingbug:
But don't you think it should always return 0 if there is no any input voltage to ADC port?

No, a pin without anything wired to it is said to be a floating input and will read random electrical
noise values.

So if I connect wire with very low voltage (say 0.1), can I get the ideal input value? or it may still high than 1v?

Wire a jumper from a ground pin to the analog input pin, the value returned by analogRead() should be 0 counts. Then move the jumper to the shield 5V pin and the analogRead() should return 1023 counts, if so you have no problems reading analog input voltages.
Lefty

Thanks for your replying first!

PaulS:

  pinMode(sensorPin, INPUT);  // << INTPUT or OUTPUT

This is NOT affecting the analog pin at all. It ONLY affects digital pins.

I think what you are trying to get at is that when using pinMode() or digitalWrite () on an analog pin you
must use its proper full pin number (the #defines A0, A1, A2 etc are valid, as are 14..19) - you mustn't
use the bare analog pin numbers (0..5) for such calls.

I didn't plug any external Vcc to it

Then you should not be reading it, or should have NO expectation about what the function will return.

Indeed a floating input is exactly that, floating around picking up any nearby signals via capacitive coupling,
or even static electricity from nearby insulating objects.

I think what you are trying to get at is that when using pinMode() or digitalWrite () on an analog pin you
must use its proper full pin number (the #defines A0, A1, A2 etc are valid, as are 14..19) - you mustn't
use the bare analog pin numbers (0..5) for such calls.

No.

Some pins can serve double duty. The same physical pin can be used as a digital pin OR an analog pin.

When pinMode() is called, it hasn't a clue that there are even such things as analog pins. Analog pins are input only, so there is no need to call a function to tell the analog pin to be an input pin.

The digitalWrite() function doesn't make an analog pin output a voltage. It might make the digital pin that shares the space do something. What it does depends on the mode of the digital pin that shares the space.

Is his 1st line ok?

can I refer to my 1st analog input pin as A0 as opposed to 0?

PaulS:

I think what you are trying to get at is that when using pinMode() or digitalWrite () on an analog pin you
must use its proper full pin number (the #defines A0, A1, A2 etc are valid, as are 14..19) - you mustn't
use the bare analog pin numbers (0..5) for such calls.

No.

Some pins can serve double duty. The same physical pin can be used as a digital pin OR an analog pin.

When pinMode() is called, it hasn't a clue that there are even such things as analog pins. Analog pins are input only, so there is no need to call a function to tell the analog pin to be an input pin.

The digitalWrite() function doesn't make an analog pin output a voltage. It might make the digital pin that shares the space do something. What it does depends on the mode of the digital pin that shares the space.

Strange semantics you have, the pin is the piece of metal sticking out of the IC package, there is only one pin per physical piece of metal, and the "analog" ones happen to be connected to the ADC multiplexor.