Duplicating the "Button" tutorial for multiple pushbuttons

Hi.

I want to be able to control 4 LEDs individually using 4 push buttons.

I thought I could simply replicate the “Button” tutorial a number of times. So I set about creating my circuit with the 4 LEDs (A through D) and my separate control push buttons (A through D). I found to my surprise that pressing any button would cause button A to light.

To debug, I stripped out all the code to everything except for button B and led B.

Still, pressing button A, B or D will turn on led B. Weirdly, button C does not cause the LED to turn on.

I’ve tried debugging it by pulling everything out and using a bare minimum of code, so now I’m thinking its a hardware issue not a coding one.

const int butB = 3;     
const int ledB =  9;      


int stateB = 0;         

void setup() {
  
  pinMode(ledB, OUTPUT);
  
  pinMode(butB, INPUT_PULLUP);
}

void loop() {
  
  stateB = digitalRead(butB);

  if (stateB == HIGH) {
    
    digitalWrite(ledB, HIGH);
  }
  else {
    
    digitalWrite(ledB, LOW);
  }
}

Any suggestion is much appreciated.

056369f4ccdde0e8088e43ee5fa1e7b1fe49bada.png

Hi Jef, looks like your wiring needs some attention.

  • Because you have used INPUT_PULLUP, you don’t need to wire your switches to 5V, only to GND
  • Wire the switches using diagonally opposite pins, with one connected to GND and the other connected to the Arduino, does not matter which pair you use. Leave the other diagonal pair unconnected.
  • Remove that 10K between the Arduino GND and the breadboard’s “-” rail
  • You should have one series resistor per led (unless you can guarantee only one led will be lit at once)
  • Because you are using INPUT_PULLUP and your switches are connected only to GND, when your code uses digtalRead(), the result will be LOW when a button is pressed and HIGH when it is not.

Well done for using code tags. 9 out of 10 new members have to be told about those, they don’t read the “read me first” post.

Paul