Skip to content Skip to sidebar Skip to footer

Turning LED ON and OFF using one button Arduino

One Button to Turn ON and OFF a LED

Turning LED ON and OFF using one button Arduino - in this article I will give you a tutorial on how to turn on and off the LED with just 1 button. Please connect the button to the Arduino board according to the following animation.

Turning LED ON and OFF using one button Arduino


Code Program:
int Led = 13;
int Button = 12;
int read_but, old_read_but, state = 0;

void setup() {
  pinMode (Led, OUTPUT);
  pinMode (Button, INPUT);
}

void loop() {
  read_but = digitalRead (Button);

  if ((read_but == HIGH) && (old_read_but == LOW)) {
    state = 1 - state;
    delay(10);
  }

  old_read_but = read_but;

  if (state == 1) {
    digitalWrite(Led, HIGH);
  }
  else
    digitalWrite(Led, LOW);
}


For more Arduino tutorials, please visit the Chip Piko website. May be useful.

Post a Comment for "Turning LED ON and OFF using one button Arduino"