How To Push Button With LED

Required Hardware

For users who do not have Hackerspace Karachi Arduino Kit v1.0

  • Arduino Board
  • Breadboard
  • Push-button
  • LED
  • Resistor 220 ohm
  • Hook-up Wires

Circuit

For users who do not have Hackerspace Karachi Arduino Kit v1.0

Schematic

Code

                    

1. /*
2. Push Button with LED
3.
4. This example shows how to use Push Button with LED
5.
6. The circuit:
7. – LED
8. Anode connected to digital pin 4.
9. Cathode connected to Ground.
10. – Push Button
11. Anode connected to VCC.
12. Cathode connected to digital pin 2.
13. Cathode connected to GND through 220 resistor.
14.
15.
16. http://hackerspacekarachi.org/
17. */
18.
19. void setup() {
20. // initialize digital pin as an output.
21. pinMode(4, OUTPUT);
22. pinMode(2, INPUT); // declaration of input
23. }
24.
25. // the loop function runs over and over again forever
26. void loop() {
27.
28. int d = digitalRead(2); // read and store input
29.
30. if (d == 1)
31. {
32. digitalWrite(4, HIGH); // turn the LED on (HIGH is the voltage level)
33. delay(1000); // wait for a second
34. digitalWrite(4, LOW); // turn the LED off by making the voltage LOW
35. delay(1000); // wait for a second
36. }
37. }

1. /*