Controlling LED With Push Button Uisng Arduino
by Rachna Aggarwal in Circuits > Arduino
325 Views, 0 Favorites, 0 Comments
Controlling LED With Push Button Uisng Arduino
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
- Arduino UNO - https://amzn.to/32jAMUA
- LED - https://amzn.to/3kc0Iru
- Push-button - https://amzn.to/3m7yKit
- A resistor of 220 ohms with LED - https://amzn.to/2ZuPNRN
- A resistor of 1000 ohms with Push Button - https://amzn.to/2ZuPNRN
- Jumper wires - https://amzn.to/3iqdBxM
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
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;
}
}