Components and supplies
1 Breadboard (generic)
2 Resistor 1k ohm
5 Jumper wires (generic)
1 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);