Setting up multiple push-buttons to output one LED

Briefly- I’m very new to this so let me know if I should re-format the post, and sorry for my ignorance on the subject. The goal is to replicate a system with multiple on/off switch style sensors that will monitor liquid levels, and similar applications. I’m using push-buttons from the maker kit to replicate those switches so that I can write code, then test scenarios of a sensor giving a low oil reading for instance.

I’m trying to keep it to a system where all switches are normally open and when activated set off the LED as a warning light for attention.

To the code I have so far. I have largely based this off of the tutorial found here: http://diotlabs.daraghbyrne.me/5-getting-input/buttons/

I would like the LED to turn on when any of the buttons are pushed. In the code below I have only attempted two buttons. When I tested this only the fist button worked as planned. The LED was unresponsive to the second button. I’m sure there are numerous errors with my approach and I’ll happily receive some constructive criticism.

    // We will be using D0 to control our LED
    int ledPin = D0;
    
    // Our button wired to D1
    int buttonPin = D1;
    int buttonPin2 = D2;
    
    void setup()
    {
    
      // For input, we define the
      // pushbutton as an input-pullup
      // this uses an internal pullup resistor
      // to manage consistent reads from the device
    
      pinMode( buttonPin , INPUT_PULLUP);
      pinMode( buttonPin2 , INPUT_PULLUP);// sets pin as input
    
      // We also want to use the LED
    
      pinMode( ledPin , OUTPUT ); // sets pin as output
    
    }
    
    void loop()
    {
       // find out if the button is pushed
       // or not by reading from it.
       int buttonState = digitalRead( buttonPin || buttonPin2 );
    
      // remember that we have wired the pushbutton to
      // ground and are using a pulldown resistor
      // that means, when the button is pushed,
      // we will get a LOW signal
      // when the button is not pushed we'll get a HIGH
    
      // let's use that to set our LED on or off
    
      if( buttonState == LOW )
      {
        // turn the LED On
        digitalWrite( ledPin, HIGH);
      }else{
        // otherwise
        // turn the LED Off
        digitalWrite( ledPin, LOW);
    
      }
    }

Thank you all!

The parameter passed to digitalRead() is a pin number, not a list or bit mask of pins.
In order to read two pins you need to have one digitalRead() per pin and logical OR their results.

int buttonState = digitalRead(buttonPin) || digitalRead(buttonPin2);
1 Like

Makes sense. Although when I do this, it only turns on the LED when I depress both buttons at the same time. Seems like it is acting like a AND function for some reason.

@bvolzmfg, I noticed you use internal pull-ups for the switches meaning when you press a button it will go LOW, not HIGH. As such, you need to invert the logic. Another thing is that || is a logical OR whereas | is a binary OR. In this case, it may be best to stick to boolean values.

bool buttonState = !digitalRead(buttonPin) || !digitalRead(buttonPin2);

if (buttonState)  // If TRUE then a button was pressed

Test away! :smile:

1 Like

Yup, that’s due to the fact that using INPUT_PULLUP and closing to GND will invert the logic - which I missed here :blush:
And the invers function of OR (||) would be AND (&&)

Just change the boolean operator and check again :wink:


The use of binary or logic OR (respectively AND) is irrelevant in this case, since both versions will render the same logical result.

1 Like

Ahh yes! Works perfectly now, thank you all very much!!!

Here is the new code for anyone who may have a similar case later.

I have added a 3rd push-button switch and an additional SPDT switch. Pictures of my breadboard setup below.

// We will be using D0 to control our LED
int ledPin = D0;

// Our buttons wired to D1, D2, D3 & grounded other side
int water_in_oil = D1;
int low_oil = D2;
int motor = D3;
int lost_motion = D4;

void setup()
{
  // For input, we define the
  // pushbutton as an input-pullup
  // this uses an internal pullup resistor
  // to manage consistent reads from the device
  // using INPUT_PULLUP and closing to GND will invert the logic

  pinMode(water_in_oil, INPUT_PULLUP);
  pinMode(low_oil, INPUT_PULLUP);
  pinMode(motor, INPUT_PULLUP);   
  pinMode(lost_motion, INPUT_PULLUP);
  pinMode(ledPin , OUTPUT); // sets pin as output
}

void loop()
{
   // Using PULL-UP for switches, so button goes LOW
   // Invert logic by using boolean values & ! inversion
   // || is a logical OR whereas | is a binary OR

  bool buttonState = !digitalRead(water_in_oil) || !digitalRead(low_oil) || !digitalRead(motor) || !digitalRead(lost_motion);

  // remember that we have wired the pushbutton to
  // ground and are using a pulldown resistor
  // that means, when the button is pushed,
  // we will get a LOW signal
  // when the button is not pushed we'll get a HIGH
  // let's use that to set our LED on or off

  if (buttonState == HIGH)
  {
    // turn the LED On
    digitalWrite(ledPin, HIGH);
  }else{
    // otherwise
    // turn the LED Off
    digitalWrite(ledPin, LOW);

  }


}

1 Like