Turn ON OFF an LED with a Push Button and Arduino

Did you know that you can use Arduino to turn on an LED when you press a button?
Well, it is true, you can do this! Leaving the joke aside, let me show how you can achieve this. You will need the Arduino Board, a 560Ω resistor, and LED and the code example below.

The schematic
arduino turn on and off led with button
  1. /* sketch 1
  2. turn on a LED when the button is pressed
  3. turn it off when the button is not pressed (or released)
  4. */
  5. int pinButton = 8; //the pin where we connect the button
  6. int LED = 2; //the pin we connect the LED
  7.  
  8. void setup() {
  9. pinMode(pinButton, INPUT); //set the button pin as INPUT
  10. pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
  11. }
  12.  
  13. void loop() {
  14. int stateButton = digitalRead(pinButton); //read the state of the button
  15. if(stateButton == 1) { //if is pressed
  16. digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
  17. } else { //if not pressed
  18. digitalWrite(LED, LOW); //write 0 or low to led pin
  19. }
  20. }

What does the code do?
We set the pinButton variable as integer 8 and we connect the button at pin 8 on the Board. Then the LED is connected to pin 2 using the resistor in series with it.
In the setup() function we set the pin 8 as INPUT and pin 2 as OUTPUT. In the loop() function we read the value of the pin 8 and store it in the variable stateButton. Using a if() function the Arduino makes some decisions: if the button is pressed (stateButton == 1) then give voltage to pin 2 (HIGH), else, if stateButton is not 1 (not pressed) do not output voltage on pin 2.
If you want to turn on the LED then let it remain ON at the button release you just need to get rid of the else statement (as you can see in sketch 2, but you won’t be able to turn it OFF without using the third sketch. Actually you can disconnect the board from the power supply or USB.
  1. /* sketch 2
  2. turn on a LED when the button is pressed and let it on when the button is released
  3. */
  4. int pinButton = 8; //the pin where we connect the button
  5. int LED = 2; //the pin we connect the LED
  6.  
  7. void setup() {
  8. pinMode(pinButton, INPUT); //set the button pin as INPUT
  9. pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
  10. }
  11.  
  12. void loop() {
  13. int stateButton = digitalRead(pinButton); //read the state of the button
  14. if(stateButton == 1) { //if is pressed
  15. digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
  16. }
  17. }
In this code I will show how you can turn on the LED when you press the button then turn it off when you press the button again. The initial state of the LED is off but if you want to be on you have to change int stateLED = HIGH;
  1. /* sketch 3
  2. turn on a LED when the button is pressed and let it on
  3. until the button is pressed again
  4. */
  5. int pinButton = 8;
  6. int LED = 2;
  7. int stateLED = LOW;
  8. int stateButton;
  9. int previous = LOW;
  10. long time = 0;
  11. long debounce = 200;
  12.  
  13. void setup() {
  14. pinMode(pinButton, INPUT);
  15. pinMode(LED, OUTPUT);
  16. }
  17.  
  18. void loop() {
  19. stateButton = digitalRead(pinButton);
  20. if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
  21. if(stateLED == HIGH){
  22. stateLED = LOW;
  23. } else {
  24. stateLED = HIGH;
  25. }
  26. time = millis();
  27. }
  28. digitalWrite(LED, stateLED);
  29. previous == stateButton;
  30. }

I had some problems initially with the code because the LED was not turning ON or OFF as I desired and discovered that there was a problem with debounce.

Comments