LED ON/OFF Using Push Button

PUSH BUTTON:
  • The push button is a component and connects the two points.
  • The first pin the push button is connected to 5 volt supply.
  • The second pin the push button is connected to gnd.

                 Figure1: PushButton 
 
Required Materials                                          
  • Arduino Uno board
  • Push button
  • Led
  • Bread board
  • Hook up wires
  • USB cable
Step 1: Push button connection,                                 
  1. The first pin the push button is connected to 5 volt supply.
  2. The second pin the push button is connected to gnd and to digital pin 03.
  3. The LED has a positive and a negative point.
  4. The positive point of LED is connected to a digital pin 13.
  5. The negative points connected to a gnd
Step 2:
                         
Connect the arduino board to your computer with the USB cable and start sketching the following program, 
 
Step 3: Programming
  1. int led = 13;  
  2. int PinButton = 4;  
  3. void setup()  
  4. {  
  5.     pinMode(PinButton, INPUT);  
  6.     pinMode(led, OUTPUT);  
  7. }  
  8. void loop()  
  9. {  
  10.     int stateButton = digitalRead(PinButton);  
  11.     if (stateButton == 1)  
  12.     {  
  13.         digitalWrite(led, HIGH); // Turn on led  
  14.     }  
  15.     else  
  16.     {  
  17.         digitalWrite(led, LOW); //Turn off led  
  18.     }  
  19.     delay(20);  
  20. }  
Explanation
 
Declare the variables: led, PinButtons and assign the value to that and in the setup() function set the button as the INPUT and led as the OUTPUT
and in the loop() function stateButton=digitalRead(PinButton). It reads the state of the push button value and if (stateButton==1) it means check if the push button is pressed and if the button state will be HIGH, then the led will glow and if it is not that means led will not glow.