Advertisement
arduino turn on and off led with button

Turn ON an LED with a Button and Arduino – Tutorial #4

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.

Related products: Electromechanical Switches | Switch Indicators Switch Rocker 

The schematic

arduino turn on and off led with button

/* sketch 1 
turn on a LED when the button is pressed
turn it off when the button is not pressed (or released)
*/
int pinButton = 8; //the pin where we connect the button
int LED = 2; //the pin we connect the LED

void setup() {
  pinMode(pinButton, INPUT); //set the button pin as INPUT
  pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
}

void loop() {
  int stateButton = digitalRead(pinButton); //read the state of the button
  if(stateButton == 1) { //if is pressed
     digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
  } else { //if not pressed
     digitalWrite(LED, LOW);  //write 0 or low to led pin
  }
}

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.

Related Products: Switch Slide Switch Snap Action Switch Tactile

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.

/* sketch 2 
turn on a LED when the button is pressed and let it on when the button is released
*/
int pinButton = 8; //the pin where we connect the button
int LED = 2; //the pin we connect the LED

void setup() {
  pinMode(pinButton, INPUT); //set the button pin as INPUT
  pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
}

void loop() {
  int stateButton = digitalRead(pinButton); //read the state of the button
  if(stateButton == 1) { //if is pressed
     digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
  }
}

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;

/* sketch 3 
turn on a LED when the button is pressed and let it on 
until the button is pressed again
*/
int pinButton = 8;
int LED = 2;
int stateLED = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 200;

void setup() {
  pinMode(pinButton, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
  stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateLED == HIGH){
      stateLED = LOW; 
    } else {
       stateLED = HIGH; 
    }
    time = millis();
  }
  digitalWrite(LED, stateLED);
  previous == stateButton;
}

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.

7 Comments

Join the conversation!

Error! Please fill all fields.
  • Jim

    Thank you for this article, I am new to microprocessors. I am attempting to create a quiz game and all the other tutorials I have seen, on led with a button, have not been this simple.

  • Chris Harris

    Bloody good stuff but if I may point a tiny little grammatical inexactitude:

    One does not turn an LED on, one turns a led on.

  • The same with 4 led's or relay's

    /*
    Greatings J.Hermans
    */

    int pinButton1 = 2;
    int pinButton2 = 3;
    int pinButton3 = 4;
    int pinButton4 = 5;

    int LED1 = 10;
    int LED2 = 11;
    int LED3 = 12;
    int LED4 = 13;

    int stateLED1 = LOW;
    int stateButton1;
    int stateLED2 = LOW;
    int stateButton2;
    int stateLED3 = LOW;
    int stateButton3;
    int stateLED4 = LOW;
    int stateButton4;
    int previous = LOW;

    long time = 0;
    long debounce = 200;

    void setup() {
    pinMode(pinButton1, INPUT);
    pinMode(LED1, OUTPUT);
    pinMode(pinButton2, INPUT);
    pinMode(LED2, OUTPUT);
    pinMode(pinButton3, INPUT);
    pinMode(LED3, OUTPUT);
    pinMode(pinButton4, INPUT);
    pinMode(LED4, OUTPUT);
    }

    void loop() {

    stateButton1 = digitalRead(pinButton1);
    if (stateButton1 == HIGH && previous == LOW && millis() – time > debounce) {
    if (stateLED1 == HIGH) {
    stateLED1 = LOW;
    } else {
    stateLED1 = HIGH;
    }
    time = millis();
    }
    digitalWrite(LED1, stateLED1);
    previous == stateButton1;

    stateButton2 = digitalRead(pinButton2);
    if (stateButton2 == HIGH && previous == LOW && millis() – time > debounce) {
    if (stateLED2 == HIGH) {
    stateLED2 = LOW;
    } else {
    stateLED2 = HIGH;
    }
    time = millis();
    }
    digitalWrite(LED2, stateLED2);
    previous == stateButton2;

    stateButton3 = digitalRead(pinButton3);
    if (stateButton3 == HIGH && previous == LOW && millis() – time > debounce) {
    if (stateLED3 == HIGH) {
    stateLED3 = LOW;
    } else {
    stateLED3 = HIGH;
    }
    time = millis();
    }
    digitalWrite(LED3, stateLED3);
    previous == stateButton3;

    stateButton4 = digitalRead(pinButton4);
    if (stateButton4 == HIGH && previous == LOW && millis() – time > debounce) {
    if (stateLED4 == HIGH) {
    stateLED4 = LOW;
    } else {
    stateLED4 = HIGH;
    }
    time = millis();
    }
    digitalWrite(LED4, stateLED4);
    previous == stateButton4;
    }

    • nhari

      i want a program related to turn off led when the tactile switch is pressed for 5 seconds

  • Haydn

    Hi, I have been looking for information for how to wire up 7 LEDs. I would like to have a power switch which turns on the LEDs. I would like the LEDs to light up only one at a time. I would like to be able to press a button and make each LED light up in turn, in other words pressing the button changes which LED is powered.

    I have only just started with Arduino so not sure how to go about something like that yet. This tutorial is good as it shows how to press a button and get a single LED to turn on and stay on.

    Have you ever come across something like that?

  • Kinda

    Hello Marian,

    Thank you for this nice debounced push-button tutorial. I was looking for something similar like in the arduino “Debounce” example but dind’t understood how I could move a servo in that way.
    Thanks of your last example here, I was able to do it. Thanks!

Looking for the latest from TI?