0
0
AutocadHow-ToBeginner · 4 min read

How to Toggle LED with Button Press in Arduino

To toggle an LED with a button press in Arduino, connect the button to a digital input pin and the LED to a digital output pin. Use code to detect button presses and switch the LED state between ON and OFF each time the button is pressed.
📐

Syntax

The basic syntax involves setting up the button pin as an input and the LED pin as an output. You read the button state with digitalRead() and change the LED state with digitalWrite(). Use a variable to keep track of the LED's current state and toggle it when the button is pressed.

arduino
const int buttonPin = 2;  // Pin connected to button
const int ledPin = 13;    // Pin connected to LED

int ledState = LOW;       // Current state of LED
int buttonState;          // Current reading from button
int lastButtonState = LOW; // Previous reading from button

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH && lastButtonState == LOW) {
    ledState = !ledState;          // Toggle LED state
    digitalWrite(ledPin, ledState);
  }

  lastButtonState = buttonState;  // Save the current state
}
💻

Example

This example shows how to toggle the built-in LED on pin 13 each time you press a button connected to pin 2. It uses a simple debounce method by checking for a change from LOW to HIGH on the button pin.

arduino
const int buttonPin = 2;
const int ledPin = 13;

int ledState = LOW;
int buttonState;
int lastButtonState = LOW;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);  // Use internal pull-up resistor
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW && lastButtonState == HIGH) {
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
  }

  lastButtonState = buttonState;
  delay(50);  // Simple debounce delay
}
Output
LED toggles ON and OFF each time the button is pressed
⚠️

Common Pitfalls

  • Not using a pull-up or pull-down resistor: This causes the button input to float and read random values.
  • Not detecting button state changes: Checking only if the button is pressed can cause the LED to toggle rapidly.
  • Ignoring debounce: Mechanical buttons can cause multiple rapid presses; adding a small delay or debounce logic helps.
arduino
/* Wrong way: toggling LED without checking button state change */

const int buttonPin = 2;
const int ledPin = 13;

int ledState = LOW;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
  }
  delay(50);
}

/* Right way: toggle only on button press transition */

int buttonState;
int lastButtonState = HIGH;

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && lastButtonState == HIGH) {
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
  }
  lastButtonState = buttonState;
  delay(50);
}
📊

Quick Reference

Remember these tips when toggling an LED with a button:

  • Use INPUT_PULLUP mode for the button pin to avoid external resistors.
  • Detect button state changes (LOW to HIGH or HIGH to LOW) to toggle LED only once per press.
  • Add a small delay (e.g., 50 ms) to debounce the button.
  • Keep track of LED state in a variable to toggle correctly.

Key Takeaways

Use a variable to store the LED state and toggle it on button press.
Set the button pin to INPUT_PULLUP to use Arduino's internal resistor.
Detect the button press by checking for a change in button state, not just if it is pressed.
Add a small delay to debounce the button and avoid multiple toggles.
Always update the last button state to track changes correctly.