Skip to content Skip to sidebar Skip to footer

How To Turn On /Off a LED With One Button

Turn On /Off a LED With One Button

How To Turn On /Off an LED With One Button - In this tutorial I will share how to use a button to turn an LED on and off.

The program code that I created is a very simple program. but with this simple program, you can use it in larger applications, such as controlling a lamp using a relay.

For an example of the simulation, I created it using the SimulIDE application as shown in the following image.

How To Turn On /Off a LED With One Button

Progarm Code:
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;

  old_read_but = read_but;

  if (state == 1) {
    digitalWrite(Led, HIGH);
    digitalWrite(Led, LOW);
For LEDs I use pin 13 and the button on pin 12. If you want to turn on the AC light, then you connect pin 13 to the relay module.

I hope this article can help you.

Post a Comment for "How To Turn On /Off a LED With One Button"