4DVolt

Arduino: how to turn on an LED with a button

Published on Oct 29, 2026

Illustration: Arduino: how to turn on an LED with a button

To turn on an LED with a button on the Arduino, connect the button to pin D2 (with a 10 kΩ resistor to GND), the LED to pin D13 (with a 330 Ω resistor in series) and use digitalRead() and digitalWrite() in the code. It is the first project that uses an input and an output together, and it is the basis for almost everything that comes next: alarms, counters, control panels and automation.

What you will need

  • 1 Arduino Uno;
  • 1 push button (tactile switch with 4 pins or 2 pins);
  • 1 LED, 5 mm (any color);
  • 1 resistor of 330 Ω (orange, orange, brown) for the LED;
  • 1 resistor of 10 kΩ (brown, black, orange) for the button;
  • 1 breadboard and a few jumper wires.

Not sure how to read the color bands? See how to read the resistor color code.

How it works

The Arduino has digital pins that can work in two ways: as an output (it sets the pin voltage high or low, 5 V or 0 V) and as an input (it only reads whether the voltage is high or low). In this project:

  • the button is the input, on pin D2: pressed, the pin reads 5 V (high level); released, it reads 0 V (low level);
  • the LED is the output, on pin D13: the program puts 5 V on the pin to turn it on and 0 V to turn it off.
Wiring of the button and the LED on the Arduino Uno: button on D2 with a 10 kΩ resistor to GND and LED on D13 with a 330 Ω resistor

Wiring the components

With the Arduino unplugged from USB, make the connections in the table. The wires in the diagram are colored only to make it easier to read: red for 5 V, blue for the button signal, green for the LED signal and gray for GND.

FromToPurpose
Arduino 5VOne button terminalPowers the button
The other button terminalPin D2Carries the signal to the Arduino
Same point as D210 kΩ resistor, and from it to GNDPull-down: keeps D2 at 0 V while the button is released
Pin D13330 Ω resistorLimits the LED current
330 Ω resistorLong leg (anode) of the LEDPowers the LED
Short leg (cathode) of the LEDArduino GNDCloses the circuit

Why the 330 Ω resistor on the LED

An LED does not limit its own current: connected straight to the 5 V of the pin, it burns out and can even damage the Arduino pin. A series resistor solves it. With a red LED (a drop of about 2 V), the current is (5 − 2) ÷ 330 ≈ 9 mA, well below the 20 mA the pin handles comfortably. To work it out for another color, see how to calculate the resistor for an LED.

Why the 10 kΩ resistor on the button

With the button released, pin D2 would be connected to nothing, and an input pin left "floating" picks up noise and reads random values (the LED would switch on and off by itself). The 10 kΩ resistor to GND pulls the pin down to 0 V while the button is released: that is the pull-down. When you press the button, the 5 V reaches D2 and wins over the resistor, because only 0.5 mA flows through it.

Programming the Arduino

Every Arduino program (called a sketch) has two functions: setup(), which runs once at power-up, and loop(), which repeats forever.

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

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

void loop() {
  if (digitalRead(BUTTON_PIN) == HIGH) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

What each line does

  • const int BUTTON_PIN = 2; gives a name to the pin number. If you change the wiring, you edit only one line.
  • pinMode(BUTTON_PIN, INPUT); sets D2 as an input, and pinMode(LED_PIN, OUTPUT); sets D13 as an output.
  • digitalRead(BUTTON_PIN) returns HIGH (5 V) with the button pressed and LOW (0 V) when released.
  • digitalWrite(LED_PIN, HIGH) turns the LED on and digitalWrite(LED_PIN, LOW) turns it off.

Since the LED simply follows the button, you can shorten loop() to one line: digitalWrite(LED_PIN, digitalRead(BUTTON_PIN));. The result is the same.

Uploading to the board

In the Arduino IDE, choose the board (Arduino Uno) and the USB port under Tools, then click Upload. When it finishes, press the button: the LED should light while you hold it and turn off when you let go.

Variation: without the 10 kΩ resistor

The Arduino has an internal pull-up resistor of about 20 kΩ on each pin, which you can enable in code. That way the button goes straight between D2 and GND, with no external resistor and no 5 V connection. The logic is inverted: the pin reads HIGH with the button released and LOW when pressed.

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, !digitalRead(2));
}

The ! inverts the reading, so the LED lights when the button is pressed. This is the most common form in real projects, because it saves one component.

Common problems

SymptomLikely cause
The LED never lightsLED backwards (long leg on GND) or a resistor not seated properly
The LED flickers on its ownMissing pull-down resistor (or INPUT_PULLUP): pin D2 is floating
The LED is always onButton wired the wrong way (pins on the same side of a tactile switch are always connected)
The LED is dimResistor value too high (for example, 10 kΩ instead of 330 Ω)

A 4-pin button has its pins connected in pairs, and what changes is the moment you press it. If the LED stays on, rotate the button 90° on the breadboard.

Test it in the simulator before building

Loading the simulator…
Press the button: D2 reads 5 V and the program lights the LED on D13. Release it and the LED goes off. The Arduino here runs the same sketch as the article.

In the 4DVolt simulator the circuit comes ready. In the File menu, choose Examples and open Button lights the LED. Press play and close the switch: the LED on D13 lights up. To change the program, open the Code panel, edit the sketch and click Compile and run. Change the LED pin, for instance, or use INPUT_PULLUP, and see the result without moving a single wire.

Next steps

  • Make the button toggle the LED (press once to turn on, press again to turn off), keeping the state in a variable.
  • Handle debounce: the mechanical contact of the button "bounces" for a few milliseconds and can count one press as several.
  • Swap the LED for a buzzer or a relay, using the same code.

Try it yourself

Next step: send ESP32 sensor data to a live chart on the IoT Dashboard.

Create your free account: open this example, change the code and build your own circuits. See also the electronics design feature of 4DVolt.

Before wiring, it helps to know the schematic symbols: electronic component symbols.

This text is informational and simplified. Check the current and voltage limits of your board and components in the datasheet before powering up.

Read also

Questions and answers No questions yet

You need to be logged in to take part. Log in or Create free account