Arduino pushbutton switch

Multiple LED with Multiple pushbutton (only one Turn on at a time)

Share this

Multi led turn on alternate with multi pushbutton

Multi-LED controlling using Multi pushbuttons using Arduino (Only One Turn on at a time)

        In this post we see how we can control more than one led using multi pushbuttons, all LEDs Turn on One by one. When pressing the one-push button switch.  Releted only One LED starts to glow or turn on. This LED continuously remains on until another pushbutton switch is pressed. When pressed the other push button switch, the previous LEDs will turn off and other LEDs start glowing which is related to this switch.

For example, if we connect 4 LEDs with 5 pushbutton switches. when pressing the first switch for a moment then only  1st LEDs start glowing and all other remains in off condition. Again when pressing the 2nd pushbutton switch then the only 2nd LED will glow and all others will be off. just like this when pressing the 3rd push button only the 3rd LED will glow and the others are in off condition and when pressing the 4th pushbutton switch only the 4th LED will turn on and the others are in OFF. One extra Pushbutton switch can be used to turn off all the LEDs or turn ON all LEDs.

Schematic diagram

Here is the connection schematic diagram

Arduino pushbutton switch

Arduino code


// assign pin number for button1
const int buttonPin2 = 3; // assign pin number for button2
const int buttonPin3 = 4; // assign pin number for button3
const int buttonPin4 = 5; // assign pin number for button4
const int buttonPin5 = 6; // assign pin number for button5
const int buttonPin6 = 7; // assign pin number for button6
const int ledPin1 = 8; // assign pin number for LED1
const int ledPin2 = 9; // assign pin number for LED2
const int ledPin3 = 10; // assign pin number for LED3
const int ledPin4 = 11; // assign pin number for LED4

int buttonState1 = 0; // variable to store the state of button1
int buttonState2 = 0; // variable to store the state of button2
int buttonState3 = 0; // variable to store the state of button3
int buttonState4 = 0; // variable to store the state of button4
int buttonState5 = 0; // variable to store the state of button5
int buttonState6 = 0; // variable to store the state of button6

void setup() {
pinMode(buttonPin1, INPUT_PULLUP); // set button1 as input with pull-up resistor
pinMode(buttonPin2, INPUT_PULLUP); // set button2 as input with pull-up resistor
pinMode(buttonPin3, INPUT_PULLUP); // set button3 as input with pull-up resistor
pinMode(buttonPin4, INPUT_PULLUP); // set button4 as input with pull-up resistor
pinMode(buttonPin5, INPUT_PULLUP); // set button5 as input with pull-up resistor
pinMode(buttonPin6, INPUT_PULLUP); // set button6 as input with pull-up resistor
pinMode(ledPin1, OUTPUT); // set LED1 as output
pinMode(ledPin2, OUTPUT); // set LED2 as output
pinMode(ledPin3, OUTPUT); // set LED3 as output
pinMode(ledPin4, OUTPUT); // set LED4 as output

}

void loop() {
buttonState1 = digitalRead(buttonPin1); // read the state of button1
buttonState2 = digitalRead(buttonPin2); // read the state of button2
buttonState3 = digitalRead(buttonPin3); // read the state of button3
buttonState4 = digitalRead(buttonPin4); // read the state of button4
buttonState5 = digitalRead(buttonPin5); // read the state of button5
buttonState6 = digitalRead(buttonPin6); // read the state of button6

if (buttonState1 == HIGH) {
digitalWrite(ledPin1, HIGH); // turn on LED1
digitalWrite(ledPin2, LOW); // turn off LED2
digitalWrite(ledPin3, LOW); // turn off LED3
digitalWrite(ledPin4, LOW); // turn off LED4

}
else if (buttonState2 == HIGH) {
digitalWrite(ledPin1, LOW); // turn off LED1
digitalWrite(ledPin2, HIGH); // turn on LED2
digitalWrite(ledPin3, LOW); // turn off LED3
digitalWrite(ledPin4, LOW); // turn off LED4

}
else if (buttonState3 == HIGH) {
digitalWrite(ledPin1, LOW); // turn off LED1
digitalWrite(ledPin2, LOW); // turn off LED2
digitalWrite(ledPin3, HIGH); // turn on LED3
digitalWrite(ledPin4, LOW); // turn off LED4

}
else if (buttonState4 == HIGH) {
digitalWrite(ledPin1, LOW); // turn off LED1
digitalWrite(ledPin2, LOW); // turn off LED2
digitalWrite(ledPin3, LOW); // turn off LED3
digitalWrite(ledPin4, HIGH); // turn on LED4

}
else if (buttonState5 == HIGH) {
digitalWrite(ledPin1, HIGH); // turn off LED1
digitalWrite(ledPin2, HIGH); // turn off LED2
digitalWrite(ledPin3, HIGH); // turn off LED3
digitalWrite(ledPin4, HIGH); // turn on LED4

}

else if (buttonState6 == HIGH) {
digitalWrite(ledPin1, LOW); // turn off LED1
digitalWrite(ledPin2, LOW); // turn off LED2
digitalWrite(ledPin3, LOW); // turn off LED3
digitalWrite(ledPin4, LOW); // turn on LED4

}
}

Simulation Video

Share this

2 thoughts on “Multiple LED with Multiple pushbutton (only one Turn on at a time)”

Leave a Comment

Your email address will not be published. Required fields are marked *