LED with Pushbutton

Dina Chehab


Components and supplies

Breadboard (generic)

Resistor 1k ohm

Jumper wires (generic)

Arduino UNO

1 LED (generic)

1 pushbutton 



LED ON when button is pressed

arduino

LED is set to ON when the button is pressed.

1const int BUTTON = 2;
2const int LED = 3;
3int BUTTONstate = 0;
4
5void  setup()
6{
7  pinMode(BUTTON, INPUT);
8  pinMode(LED, OUTPUT);
9}
10
11void  loop()
12{
13  BUTTONstate = digitalRead(BUTTON);
14  if (BUTTONstate == HIGH)
15  {
16    digitalWrite(LED, HIGH);
17  } 
18  else{
19    digitalWrite(LED,  LOW);
20  }
21}

LED is OFF when button is pressed (Opposite effect)

arduino

LED is OFF when button is pressed (Opposite effect)

1const int BUTTON = 2;
2const int LED = 3;
3int BUTTONstate = 0;
4
5void  setup()
6{
7  pinMode(BUTTON, INPUT);
8  pinMode(LED, OUTPUT);
9}
10
11void  loop()
12{
13  BUTTONstate = digitalRead(BUTTON);
14  if (BUTTONstate == HIGH)
15  {
16    digitalWrite(LED, LOW);