Arduino Push Button LED: Wiring, Code and Simulator
To build this Arduino Push Button LED project you need an Arduino Uno and a 220 Ω resistor, a green LED and a push button. A push-button on pin 2 with the internal pull-up, lighting an LED on pin 13. Make the 5 connections in the wiring table, upload the Arduino C++ code, and click and hold the button. The LED lights while it is held, and the serial monitor says pressed / released.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | green LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
| 1 | push button | Connects 1 to 2 while held. Pair with INPUT_PULLUP and wire the other side to GND. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno pin 13 | 220 Ω resistor leg 1 |
| 2 | 220 Ω resistor leg 2 | green LED anode (A, long leg) |
| 3 | Arduino Uno GND | green LED cathode (C, short leg) |
| 4 | Arduino Uno pin 2 | push button 2.l |
| 5 | Arduino Uno GND | push button 1.r |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a 220 Ω resistor, a green LED and a push button from the parts palette.
- Wire 1. Connect Arduino Uno pin 13 to 220 Ω resistor leg 1.
- Wire 2. Connect 220 Ω resistor leg 2 to green LED anode (A, long leg).
- Wire 3. Connect Arduino Uno GND to green LED cathode (C, short leg).
- Wire 4. Connect Arduino Uno pin 2 to push button 2.l.
- Wire 5. Connect Arduino Uno GND to push button 1.r.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Click and hold the button. The LED lights while it is held, and the serial monitor says pressed / released.
Arduino C++ code
// Button: read a push-button and light an LED while it is held.
// INPUT_PULLUP means the pin reads HIGH until the button connects it to GND.
const int BUTTON = 2;
const int LED = 13;
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("Press the button.");
}
void loop() {
static bool last = HIGH;
bool now = digitalRead(BUTTON);
digitalWrite(LED, now == LOW ? HIGH : LOW);
if (now != last) {
Serial.println(now == LOW ? "pressed" : "released");
last = now;
}
delay(10); // a crude debounce
}
What to try
Click and hold the button. The LED lights while it is held, and the serial monitor says pressed / released.
Frequently asked questions
How do I wire the Push Button LED project on an Arduino Uno?
Make 5 connections: Arduino Uno pin 13 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to green LED anode (A, long leg); Arduino Uno GND to green LED cathode (C, short leg); Arduino Uno pin 2 to push button 2.l; Arduino Uno GND to push button 1.r.
What parts do I need for this Arduino project?
An Arduino Uno and a 220 Ω resistor, a green LED and a push button.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
A push-button on pin 2 with the internal pull-up, lighting an LED on pin 13.
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator