Multiple buttons interfering with each other.

Hey there,

I'm working on a project in which I use buttons too cycle through some menu options on an LCD screen. Now I found that my buttons were acting up. At first I though I had made a programming error but after further investigating I narrowed it down to the buttons themselves.

I made a test setup with just two buttons and let the arduino print something to the serial monitor when a HIGH signal was detected. However whenever I press one button it also triggers the other button.

I really don't know what is going on here.

int button1 = 10;
int button2 = 8;

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  Serial.begin(9600);
}

void loop() {
  if(digitalRead(button1) == HIGH){
    Serial.println("BUTTON 1");
  }

  if(digitalRead(button2) == HIGH){
    Serial.println("BUTTON 2");
  }  

}

Im using a 220 ohm resistor for the buttons.

If anyone can help me out, that would be greatly appreciated!

It sounds like the buttons are wired incorrectly, and the inputs are floating.

Can you please provide a sketch of how the buttons are wired to the Arduino and where the 220 ohm resistors are connected.

Of course:

Ohh and I tried switching the Power and Ground wires on the buttons and also tried it with two separate resistors instead of them both using the same one. No luck.

The most simple way to use these normally open tactile buttons is

  1. Set the pinMode on the Arduino input pins to INPUT_PULLUP. The will enable the internal pull up resistors, and the pin will read HIGH if the button is not pushed (or indeed if there is not even a button connected).

  2. Wire diagonally across the switch. One lead goes to ground, and the other lead goes to the input pin. The diagonal wiring is important as it ensures that the two leads will be connected by the switch. When the switch is pressed, the input will be connected to ground and read LOW. The internal pull up resistor limits the current.

With your diagram, remove the ground wires and resistors connected to the switches. The red wires currently running from the switches to 5v should be connected to ground instead.

The switches should be independent and read HIGH when not pressed, and LOW when pressed.

I think that your switches are rotated such that the inputs are not actually connected to ground or one is rotated with respect to the other. Figuring out he connected and switched legs on these switches are tricky, and that's why the diagonal wiring with INPUT_PULLUP mode is best.

I tried all of that and unfortuantely it didn't work.

I connected them like this:

However if I do it like this ^ the serial monitor just spams Button 1 and Button 2, which is odd since it should not be able to get power without the button being pressed right?

I also tried it like this:

But like this ^ it does the exact same thing as stated in my initial post.

In both cases I tried it with and without INPUT_PULLUP and rotated the buttons (first just the one button and then the other buttons aswell)

I tried all of that and unfortunately it didn't work.

Please show a diagram of the way I suggested, and show the coded used with that wiring. None of the diagrams you present show that. This should really be very simple. Two wires to each switch and no external resistors.

Have you tested the switches with a multimeter to confirm which legs are connected and which are switched? Are the switched legs normally open or normally closed?

You've wired the two switches together. You need separate pulldown resistors for each switch.
(As cattledog has pointed out) it would be less hardware if you initialized the buttons to INPUT_PULLUP and then just wired the buttons to +5V and Gnd. Then the sketch should look for a LOW instead of a HIGH.
But you also need to need to handle two other things. You need to detect when the switch changes state from HIGH to LOW - not when it is HIGH (or LOW). You also need to handle contact bounce.

Pete

@Catteldog
Do you mean without any resistor? And as for the code that would be exactly the same except instead of INPUT I would use INPUT_PULLUP right?

I have not tested them with a multimeter, since I don't own one (I only started working with arduino about a month ago). I know they're quite cheap but haven't had then need for on thus far. I may pick one up soon though.

I believe the switched legs are normally open since I have used them before without any problems. I don't know why they behave like this. I've never had any trouble with them before.

@el_supremo
Ok that makes sense to detect for a LOW value with the INPUT_PULLUP, And I know about the state change detection, but for now I just want my buttons to function properly and I don't need state change detection for that.

I'll going to sleep now, but I will try it without resistors tomorrow morning.

Do you mean without any resistor?

Yes, that is correct. You will be using the internal pullup resistor(approximately 30K ohm) of the Arduino processor to connect the input to 5v.

And as for the code that would be exactly the same except instead of INPUT I would use INPUT_PULLUP right?

For the pinMode of the input pin yes. For the logic of the code, you will see HIGH when the button is not pressed, and LOW when it is pressed.

Somehow today at school I manged to fix the problem. Turns out I had to use two resistors instead of one. Still not exactly sure why this works, but at least it works.

Thank you both for your replies!