Control The Led’s on the breadboard with a push button.

As of earlier I forgot to put this post up which shows how you can control any number of LED’s on your breadboard with a push button. My earlier post was also related to a push-button but that tutorial taught you only about the LED on the pin 13, not an external LED.

HARDWARE REQUIRED :-

Arduino.

Resistors 10k and 1k.

Jumper cables.

breadboard.

LED’s.

The CODE :-

int switchPin = 3;

int LEDpin = 12;

int switchState = 0;

void setup()
{

pinMode(switchPin, INPUT);
pinMode(LEDpin, OUTPUT);

}

void loop()
{

switchState = digitalRead(switchPin);

if(switchState == HIGH)

{

digitalWrite(LEDpin,HIGH);

}

else
{
digitalWrite(LEDpin,LOW);
}
}

The integer constants ‘switchPin’ and “LEDpin” are initialized with values 3 and 12. Therefore, the setup function declares the digital pins 3 and 12 as ‘INPUT’ and OUTPUT pins respectively. We need to digitally read pin 3 to check the current status of push button i.e whether it is pressed or not. A variable “switchState” is declared to read the current state of the switch. If the push button is pressed, variable ‘switchState’ becomes HIGH or in binary we say ‘1’. i.e ON on pin 12.

HOW TO ASSEMBLE :-

Very simple assemble setup. Connect the 10k ohm resistor to the switch and the 1k to the LED. Don’t forget to ground the circuit. You’re ready to go!