Why does ADC keep printing values on serial monitor without any input?

Hey guys,

Can someone explain to me why the ADC keeps printing values on the serial monitor without anything connected to the analog input pin (arduino is basically connected to laptop USB only). Attached is the code (found in arduino website) and the output from serial monitor. Is arduino damaged :S ?

Thanks.

Read up on 'floating pins' its perfectly normal.

How do I eliminate it? And can the pin work even if it is floating? Is it necessary for the pin to output 0 when nothing is connected?

How do I eliminate it?

Stop reading the pin.

And can the pin work even if it is floating?

As long as it doesn't float away...

How would you determine if a pin was "working" when there is nothing connected to it? How would even define "working" for a pin with nothing connected to it?

Is it necessary for the pin to output 0 when nothing is connected?

No, it isn't.

If you want the pin to go to a particular value when it is not connected, you need to connect an external pull-up or pull-down resister to stop it floating. Or, you can enable the internal pull-up resister.

The internal pull up resistor can only be enabled to digital pins right? I am using the analog pin, if so can I still declare the analog pin as an input?

The internal pull up resistor can only be enabled to digital pins right?

Wrong.
It can be applied to the analogue pin as well.

I am using the analog pin, if so can I still declare the analog pin as an input?

There is no need (or ability) to. The analog pins are, by definition, input pins.

When I put the analog pin to HIGH or LOW, the analog pin A0 outputs 14 on the serial monitor. Is it normal?

int analogPin = A0;     // potentiometer wiper (middle terminal) connected to analog pin 3                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read

void setup()
{
  Serial.begin(9600);          //  setup serial
  pinMode(analogPin, INPUT);
}

void loop()
{
  //val = analogRead(analogPin);    // read the input pin
  analogWrite(analogPin, HIGH);
  Serial.println(analogPin);             // debug value
  delay(300);
}

Yes that is normal, you are printing the value of a variable you created called analogPin which is set to A0 which is an alias of the number 14. This has nothing to do with what voltage is at pin 14 either reading or writing.

By the way the statement:

analogWrite(analogPin, HIGH);

Is bogus. You can only perform analogWrites to digital output pins that are associated with PWM capabilities. To use a analog pin as a digital output pin you can use only digitalWrite commands.

Lefty

When I put the analog pin to HIGH or LOW, the analog pin A0 outputs 14 on the serial monitor. Is it normal?

Analog pins are input only. You can't set them to HIGH or LOW. Analog pins as digital pins can be set HIGH or LOW, if you have declared them to be output pins, and you reference them using the correct numbers (or aliases).

Serial.println(analogPin);

A0/14 is a constant - did you mean to put some sort of read in there?