OBJECTIVE
Toggle the LED ON/OFF using Push button..
MODULES REQUIRED
- Arduino Board
- 1 x Push button
- 10k Pull down resistor
- Connecting wires
- 1 LED
SCHEMATIC DIAGRAM
- Connect LED’s pin (anode) to D5 (output)
- Connect Push button pin to D7 (Input)
Following connection are made in inbuilt circuit, so there no need made these connection:
- LED’s anode connected to 5V using a 1K pull-up resistor
- Push button pulled down to GND using 10K pull-down resistor
ARDUINO CODE
JavaScript
int led=5;
int button=7;
int val=0;
int old_val=0;
int state=0;
void setup (){
pinMode(led,OUTPUT);
pinMode(button,INPUT);
}
void loop()
{ val=digitalRead(button);
if( (val==HIGH) && (old_val==LOW)) {
state=1-state;}
old_val=val;
if (state==1) { digitalWrite(led, HIGH);}
else {digitalWrite (led,LOW);}
}}
INSTRUCTIONS
- Connect the modules & components as per schematic diagram.
- Upload the Arduino code into Arduino board.
- Verify the LED toggles on and off with push button presses.
WORKING
- When the button is pressed, pin 12 is 5V (HIGH).
- When the button is pressed again, pin 12 is 0V (LOW).
- So we will see the LED light up and go out alternately as the button is pressed and pressed again.