Why are all of these buttons acting like they are the same?

Hey guys; I've been a long time lurker, but this is my first question I couldn't find an answer too myself.

I'm working on a project where I'm pairing a Teensy 2.0 with a Raspberry Pi over UART to make an interactive raspberry pi enclosure. I've got seven 2-pole buttons mounted in the front of the enclosure, each going to a different Teensy input but receiving power from the same source.

I just wrote a quite sketch to test the validity of my setup and that the buttons were working, but I realized that no matter which button I pressed, the pin I was reading (I was only reading the first buttons pin) would go up to 1.

I am relatively new to this; but I have had buttons that have had common power sources (and by that I mean that each buttons input wire is connected to the same "rail" which I made by myself out of perfboard) and they have never done anything like this in the past. However, in recent weeks, I've heard alot about "Button Bouncing" and think that it may be my issue. However, I'm not sure. Is this behaviour something that could be brought about by a lack of debouncing in my hardware?

Thanks, and if more details are needed feel free to ask.

Michael

Did you accidentally leave out the pull-down resistors?

I've only ever used resistors with 4-Pole buttons before, I wasn't aware as to how to employ them with the two pole buttons. In fact I didn't even know that they needed them, I thought that it had something to do with the four pole buttons being able to switch either from LOW to HIGH or HIGH to LOW.

Will I need to add the resistors? And which pole, the source (of which I assume so) or the gate?

Thanks for your help!

Michael

You will need to post your code!

Mark

... and a diagram of how the buttons are wired up.
It sounds to me like they are all wired to one pin or perhaps to all of the pins.

Pete

Here is a diagram of the way that these buttons are wired. The 'Power' board is just a piece of perfboard with every pad connected to the same rail.

The code is inconsequential in this case, I'm just reading pin1 and echoing it to serial in a loop. But here you go anyways

/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor 
 
 This example code is in the public domain.
 */

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 1;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
}

If you have any questions then feel free. But I would like to hear thoughts towards needing resistors on the buttons as was discussed earlier.

You are fighting a case of "floating input pin condition". When the buttons are not being pushed you are assuming the input pin condition will return a LOW, which is not the case as there is no electrical LOW being presented to the pins, just a no connection which means random noise will cause a random value to be read back.

You need to either wire external pull-down resistors (say 10k ohms) from each input pin to ground. Or more simply, instead of wiring one side of all the button to Vcc power, instead wire them all to ground and then in software for each input pin enable the internal pull-up resistor for that pin, then in your sketch reading a LOW means the button is being pressed and reading a HIGH means the button is not being pressed.

That make sense?

Lefty

No, it really doesn't. My issue isn't that I'm getting an input that is randomly floating between LOW and HIGH while I'm not pressing a button, it's that when I press any button, even the ones that I'm not reading the lines of; I get a HIGH. So basically all of my buttons are acting like they are wired to the same pin, but really the only common thing they share is a power source.

As well, I'm NOT using the standard four post buttons of which I am used to, I am using panel mounted two post buttons, that don't have positive/negative/signal posts; rather a source and a gate. So in that case; where am I putting the resistor? And is that even my problem at hand here?

centizen:
No, it really doesn't. My issue isn't that I'm getting an input that is randomly floating between LOW and HIGH while I'm not pressing a button, it's that when I press any button, even the ones that I'm not reading the lines of; I get a HIGH.
How do you know this without reading the other pins?
So basically all of my buttons are acting like they are wired to the same pin, but really the only common thing they share is a power source.

As well, I'm NOT using the standard four post buttons of which I am used to, I am using panel mounted two post buttons, that don't have positive/negative/signal posts; rather a source and a gate. So in that case; where am I putting the resistor? And is that even my problem at hand here?

I really think you are overthinking the symptom. The circuit you have drawn suffers from 'floating inputs' when switches are not being pressed and pressing one switch will certainly effect the noise level being seen by the other unpressed button input pins. So not to try and lay the experience card down on you, but we have have been helping beginners with their simple button wiring problems for several years now, and not having proper pull-down resistors on your inputs pins is the cause of your problem. Pull-down resistors are wired between the input pins and ground, one for each input pin.

Lefty

Ok, I didn't think about it that way. So really this is as much a floating input issue as it is a button debouncing problem, because the bounce is returning down to the power board causing noise. That makes a lot more sense now. I'll add resistors to the gate pins on each of my buttons and hopefully that will get it to work.

centizen:
Ok, I didn't think about it that way. So really this is as much a floating input issue as it is a button debouncing problem, because the bounce is returning down to the power board causing noise. That makes a lot more sense now. I'll add resistors to the gate pins on each of my buttons and hopefully that will get it to work.

Cool, let us now how it works out. I don't quite understand your use of the name 'gate pins' for a switch contact pin, but that's not important as there are many things I don't understand in this world. :wink:
Again the pull-down resistors should wire from the microcontroller's input pins side of the switches and ground.

Lefty

By gate pin I just meant the pin that is not connected to power.

So I implemented this solution with 10k resistors and I'm still having the same problem. To try to isolate the problem I made a breadboard circuit using similar buttons in the manner you described. Here is a picture of it.

Anyways, the issue is still happening. I will press a button that is not connected to the line that I am reading and get a HIGH. Using the digitalReadSerial example function I read pin 3 and can get HIGH's from most, if not all of the buttons. I am not sure why this is happening but I don't think it is a floating input error unless I've put my resistors on the wrong side.

Anyways, thanks for the help so far. Hopefully we can figure this out

Michael

centizen:
So I implemented this solution with 10k resistors and I'm still having the same problem. To try to isolate the problem I made a breadboard circuit using similar buttons in the manner you described. Here is a picture of it.

It's hard to be sure from the picture, but it looks to me as if you have the resistors wired in series in the line between the switch and the Arduino input. That is not the correct way to wire up pull-up (or pull-down) resistors.

Yes you have improperly followed my instructions (trice times!). The resistors need to wire from the input pins of the controller to ground, not is series with the switch contacts as you have built. Here is a linked document that will possible better explain the problem and solution to your dilemma. The fourth drawing down in the doc is how you need to wire your resistors. http://www.seattlerobotics.org/encoder/mar97/basics.html

Lefty

retrolefty:
Yes you have improperly followed my instructions (trice times!).

Yeah; great. Thanks. Sorry if my small brain had a hard time comprehending.

PeterH:
It's hard to be sure from the picture, but it looks to me as if you have the resistors wired in series in the line between the switch and the Arduino input. That is not the correct way to wire up pull-up (or pull-down) resistors.

Thanks, I'll rewire and try tomorrow.