Introduction: Controlling LED With Push Button Uisng Arduino

About: pursuing Bachelors in Electronics and Communication at Chandigarh University

In this project, I have used a push button to switch ON the LED if it is switched OFF at that point and vice verse. In other words, I will toggle the LED using a push button.

The Push-button completes the circuit and allows the flow of current inside the components attached to it in the circuit.

Components Required

  1. Arduino UNO - https://amzn.to/32jAMUA
  2. LED - https://amzn.to/3kc0Iru
  3. Push-button - https://amzn.to/3m7yKit
  4. A resistor of 220 ohms with LED - https://amzn.to/2ZuPNRN
  5. A resistor of 1000 ohms with Push Button - https://amzn.to/2ZuPNRN
  6. Jumper wires - https://amzn.to/3iqdBxM

Step 1: Circuit Schematic

Pin 13 - - > anode of LED

Pin 2 - - > terminal 1a of push button

5V - - > terminal 2a of push button

GND - - > cathode of LED, terminal 1b of push button

Note : - Here, resistance is required to pull down the voltage value supplied to push button that is why called pull down resistor . If you are new to this concept then try this project with and without 1000 ohms resistor . You will understand the necessity of using pull down resistor with push button

Step 2: Arduino Code

/*s will be a state of push button variable which will change to 1

till the button is not released*/

//ledtoggle will toggle between turning led on or off

int s= 0, ledtoggle=0;

void setup() {

pinMode(13, OUTPUT);

pinMode(2, INPUT);

}

void loop() {

if (s == 0 && digitalRead(2) == HIGH) {

s = 1;

ledtoggle=!ledtoggle;

digitalWrite(13, ledtoggle);

}

//when button is released we dont wan't led to get on or off //therefore , no ledtoggle required

if (s == 1 && digitalRead(2) == LOW) {

s = 0;

}

}