Skip to Content

Arduino Button Control: How to Turn an LED On/Off with a Pushbutton

Learn to read a button press and control an LED using an Arduino Uno in Velxio's interactive simulator.
May 12, 2026 by
Arduino Button Control: How to Turn an LED On/Off with a Pushbutton
Velxio

Welcome! In this project, we'll build a classic beginner circuit: a pushbutton that controls an LED. Using Velxio's in-browser simulator, you can follow along without any hardware. By the end, you'll understand how to read digital inputs and write outputs on an Arduino Uno.

Circuit Walkthrough

Our circuit uses three components: an Arduino Uno, a pushbutton, and a red LED. Let's look at each wire:

  • Wire button (blue): Connects Arduino pin 2 to pushbutton pin 1.l. This reads the button state.
  • Wire wire-led (red): Connects Arduino pin 13 to LED pin A (anode). This supplies current to the LED.
  • Wire wire-led-gnd (black): Connects LED pin C (cathode) to Arduino GND. Completes the LED circuit.
  • Wire wire-button-gnd (black): Connects pushbutton pin 2.l to Arduino GND. This grounds the other side of the button.

The pushbutton has four pins, but we only use two: 1.l and 2.l. When pressed, these pins are shorted together, connecting pin 2 to GND (LOW).

ComponentQuantityNotes
Arduino Uno1
Pushbutton1Normally open, momentary
Red LED15mm, any color
Connecting wires4Assorted colors

Code Walkthrough

Here's the full Arduino sketch:

// Button LED Control
// Press button to turn LED on

const int BUTTON_PIN = 2;
const int LED_PIN = 13;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN);

  if (buttonState == LOW) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

Let's break it down block by block:

Constants: We define BUTTON_PIN = 2 and LED_PIN = 13. These match our wiring.

setup(): The button pin is configured as INPUT_PULLUP. This enables the internal pull-up resistor (about 20-50 kΩ), so the pin reads HIGH when the button is not pressed, and LOW when pressed (since pressing shorts it to GND). The LED pin is set to OUTPUT.

loop(): We read the button state with digitalRead(BUTTON_PIN). If it's LOW (button pressed), we turn the LED on (HIGH). Otherwise, we turn it off (LOW).

Key Concepts

  • Digital Input/Output: Arduino pins can read or write digital signals (0V or 5V).
  • Pull-up Resistor: INPUT_PULLUP uses an internal resistor to keep the pin HIGH until the button pulls it LOW. This avoids a floating pin.
  • Debouncing: In real circuits, switches bounce, causing multiple readings. Here we ignore debouncing for simplicity, but in production you'd add a small delay or use a debounce library.

Common Pitfalls & Debugging Tips

  • Wiring mistakes: Double-check that the button's other pin is connected to GND. If not, the button won't work.
  • LED polarity: The anode (long leg) goes to pin 13, cathode to GND. Reversing won't damage it, but it won't light up.
  • Using the wrong pin: On Arduino Uno, pin 13 has a built-in LED. That's fine, but you can use any digital pin.
  • No pull-up: If you forget INPUT_PULLUP, the pin floats and readings will be random.

Suggested Extensions & Variations

  • Toggle mode: Modify the code so each press toggles the LED on/off instead of holding.
  • Multiple buttons: Add more buttons to control different LEDs.
  • Analog input: Replace the button with a potentiometer to control LED brightness with PWM.
  • Add serial output: Use Serial.println() to see button state in the serial monitor.

Try It Yourself!

Now you're ready to build your own button-controlled LED. Head over to the live example on Velxio.dev to simulate the circuit instantly. Open the code, press the button, and watch the LED light up. Happy hacking!

Blink an LED with Arduino – Your First Step into Embedded Programming
Learn how to upload the classic Blink sketch to an Arduino Uno and make an LED toggle on and off.