Arduino: how to turn on an LED with a button
Published on Oct 29, 2026
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 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.
| From | To | Purpose |
|---|---|---|
| Arduino 5V | One button terminal | Powers the button |
| The other button terminal | Pin D2 | Carries the signal to the Arduino |
| Same point as D2 | 10 kΩ resistor, and from it to GND | Pull-down: keeps D2 at 0 V while the button is released |
| Pin D13 | 330 Ω resistor | Limits the LED current |
| 330 Ω resistor | Long leg (anode) of the LED | Powers the LED |
| Short leg (cathode) of the LED | Arduino GND | Closes 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, andpinMode(LED_PIN, OUTPUT);sets D13 as an output.digitalRead(BUTTON_PIN)returnsHIGH(5 V) with the button pressed andLOW(0 V) when released.digitalWrite(LED_PIN, HIGH)turns the LED on anddigitalWrite(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
| Symptom | Likely cause |
|---|---|
| The LED never lights | LED backwards (long leg on GND) or a resistor not seated properly |
| The LED flickers on its own | Missing pull-down resistor (or INPUT_PULLUP): pin D2 is floating |
| The LED is always on | Button wired the wrong way (pins on the same side of a tactile switch are always connected) |
| The LED is dim | Resistor 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
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
ESP32 IoT Dashboard: Send Sensor Data to a Live Chart
Send a DHT22 reading from an ESP32 over the internet and see it on a chart, with a temperature alert. Step by step, with a ready sketch and simulator test.
Arduino simulator: sequential LEDs step by step
Simulate an Arduino circuit without building anything: open the sequential LEDs example, compile the code, see the breadboard and run it in 2D and 3D.