Arduino digital pins - incorrect values

I've been trying to get my digital pins to work on my Uno, but whenever I try to measure a voltage from a digital pin such as A0 using AnalogRead, it won't give me a correct value, even when connecting it directly to ground or 5V using a jumper cable. Whatever pin I connect it to, it'll output 2.11 V and gradually decrease down to 1.33 V. Is my board busted? How can I fix this?

int analogPin = A0;
int val = 0;           // variable to store the value read

void setup() {
    Serial.begin(9600);          //  setup serial
}

void loop() {
  val = analogRead(analogPin);    // read the input pin
  Serial.println(val*(5.0/1023));             // debug value
  delay(1000);
}

When you use analogRead, your pin (A0 in your example) is treated as an analog pin.

Is anything connected to A0? If not, you're reading garbage as your pin is floating.

UnoWatt:
OP said s/he has tried with it wired to 0 and 5V.

OOPS, missed that :frowning:

Is it being powered from USB? Are any other pins connected to anything? Do you have a meter so you can check the voltage between Ground and +5V?

Ignore this post, as AWOL pointed out, I was incorrect about the substitutions.
Marsha

as an experiment try not using a variable for analogPin, that is change it to

val = analogRead(A0);

I can't remember where the IDE does the pin coding substitutions, but you may be trying to do an analogRead on pin 0xA0, or 160 decimal?

Marsha

MarshaJ847:
I can't remember where the IDE does the pin coding substitutions, but you may be trying to do an analogRead on pin 0xA0, or 160 decimal?

No.

@OP: Are you absolutely sure you're wired to the correct pin? Check the pin labels.

Here is something I learned way back in the dark ages.

Most analog input systems use a single A to D converter. To get multiple channels, they use a MUX chip to allow the system to use one A/D to scan a lot of channels.

MUX chips generally use a series of FET transistors to control which channel they are looking at. If you leave all of the unused channels on a given MUX chip un-terminated or floating, the voltage on them fill floats all over. If the voltage on a given channel exceeds the bias voltage on the FET, the voltage will go through. As a result, the A/D converter will read the result of more than one channel at a time. The fix is easy. Ground all unused analog inputs.