How to Toggle an LED with Push Button and Arduino Uno

 Toggling of an LED with Push Button

 

Welcome to my  Tutorial 2, In this we will do Toggling of an LED with push button. 

The very first Tutorial on this Arduino Platform is "How to Turn On and Off an LED using 2 Push Buttons with Arduino Uno

In this we Turn On and Off an LED with a single Push Button.
 
 
 

 

This Tutorial I have shown is using Proteus v8.11. I will upload a video using components later on my YT channel.


If you don't know How to Install Proteus v8.11, read my blog from below link👇👇

https://www.electronicsisfun08.in/2022/12/how-to-download-and-install-proteus.html

 Since YouTube has removed my video of How to download and install Proteus, So you can download it from above link.

 

If you don't have the Arduino Libraries, then watch that separate video from below link👇👇

https://www.electronicsisfun08.in/2022/12/how-to-add-free-libraries-in-proteus.html 

 For more help on this How to add libraries of Proteus v8.11/v8.12 You can watch the video also.

 


 

 

Using 1 push button to Turn On and Off LED


Using 1 push button to Turn ON and OFF the same LED is a logic game. Like there is 1 push button, When you press Push Button first time, the LED Turns On and when you press again, the same LED Turns OFF.

 

Watch the video:-

 


 

 

WIRING DIAGRAM FOR Push BUTTON AND LED

 

Let’s start with a wiring diagram for this project.

In this Wiring Diagram, First connect an LED with Pin no. 7 of Arduino with a 330 ohm resistance to ground.

Now, connect push button with Pin no. 3 of Arduino to GND.

Connect 1K resistance from push button to Vcc.

 


 

Parts List:- 

 

1. Arduino Uno :- https://amzn.to/3VSv8l6

2. Push Buttons :- https://amzn.to/3XhvvGG

3. LED :- https://amzn.to/3jO6559

4. Resistance Pack :- https://amzn.to/3XbimPy

5. Jumper Wires :- https://amzn.to/3vPi9WO

 

CODE FOR THE BUTTON AND LED

 

const int BUTTON_PIN = 3;  // Connect the Button to pin 3 or change here
const int LED_PIN    = 7;       // Connect the LED to pin 7 or change here
 
// variables will change:
int ledState = LOW;        // tracks the current state of LED
int lastButtonState;        // the previous state of button
int currentButtonState; // the current state of button
 
void setup() {
  Serial.begin(9600);                         // initialize serial
  pinMode(BUTTON_PIN, INPUT);  // set arduino pin to input mode
  pinMode(LED_PIN, OUTPUT);      // set arduino pin to output mode
 
  currentButtonState = digitalRead(BUTTON_PIN);
}
 
void loop() {
  lastButtonState    = currentButtonState;                // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state
 
  if(lastButtonState == HIGH && currentButtonState == LOW) {
    Serial.print("The button is pressed: ");
 
    // toggle state of LED
    if(ledState == LOW) {
       ledState = HIGH;  
       Serial.println("Turning LED on");
    }
    else {
      ledState = LOW;
      Serial.println("Turning LED off");
    }
 
    // control LED arccoding to the toggled state
    digitalWrite(LED_PIN, ledState);  //turns the LED on or off based on the variable
  }
}



If you have any query regarding this project, Contact me from below :

Telegram +919557024177
Instagram id : eif.08
Facebook page  : EIF08

Post a Comment

1 Comments