problem with multiple analog inputs! buggy arduinos? needs caps?

Hi,

I wired up two 10k linear pots in the usual way (5v on one side gnd on the other and the center pin going to an analog input for each pot). Next I get the values with analogRead and serial print them.

Here's the problem, the first pot reads fine, I get the values from 0 1023 but the second one always stays at 1023.

If I unplug it the input floats and gives random values so there's a connection to the pot. Also I've switched the pots around and same happens on the same line (not same pot). I've also tried with different pins, same story. I even tried it on the arduino uno, teensy 2, and teensy 2++.

This is getting very frustrating I've looked at a ton of similar projects measuring two pots like this and it seems to work for everyone straight of. Am I missing something here? Will putting caps somewhere help?
here's my code:

int pin1 = A0;
int pin2 = A1;
int val1 = 0;
int val2 = 0;

void setup(){
Serial.begin(9600);
pinMode(pin1, INPUT);
pinMode(pin2, INPUT);
}

void loop(){
val1 = analogRead(pin1);
val2 = analogRead(pin2);
Serial.print(val1);
Serial.print(" ");
Serial.println(val2);
}

Any help would be greatly appreciated!!

Here's the problem, the first pot reads fine, I get the values from 0 1023 but the second one always stays at 1023.

That means that the GND of the second pot is not connected well

try to use a new wire between the failing pot and the GND of the arduino.

Note it can be a bad connector in the breadboard!

please use the # button (just above the smileys) to post code with proper tags.

That's it! thanks I switched every thing up and it works, I think its the ground power line that might be cut off.
pheww, thanks

Some breadboards have the power and/or ground lines at the edges split into 2 equal parts. Maybe yours is like that.

This is wrong:-

int pin1 = A0;
int pin2 = A1;

Change it to:-

int pin1 = 0;
int pin2 = 1;

Grumpy_Mike:
This is wrong:-

int pin1 = A0;

int pin2 = A1;



Change it to:-


int pin1 = 0;
int pin2 = 1;

Why do you think the first example is wrong. I thought A0 was a predefined alias for analog pin 0, A1 for analog pin 1, etc. ?

Lefty

needs caps?

In general, capacitors (and inductors) are evil and you should try to use fewer of them if you can.

Nothing wrong with using the A0, A1 pin numbers for any of digitalRead/Write() and analogRead(). Using analog pin numbers 0, 1 etc in analogRead() is the only place they are valid, so should you be using some analog pins as digital as well as analog, the A0, A1 names are clearer.

Of course when iterating through analog pins with a for loop the 0, 1 numbering scheme is convenient:

  for (int channel = 0 ; channel < 6 ; channel++)
  {
    int val = analogRead (channel) ;
    ....
  }

this works too

for (int channel = A0 ; channel <= A5 ; channel++)  // even channel < A6 works !!
  {
    int val = analogRead (channel) ;
    ....
  }