Button and LED on an Arduino Uno
Hold the pushbutton on pin 2 and the LED on pin 13 lights. The pin uses the Uno’s internal pull-up, so it reads LOW while the button is pressed. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $32. On Mokxi they're free.
What you'll learn
- Reading a button with INPUT_PULLUP
- Why a pull-up button reads LOW when pressed
- Turning a digital input into a digital output
- Wiring an LED with a 220 ohm resistor
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
The pushbutton connects pin 2 to ground. The sketch turns on the Uno's built-in pull-up on that pin, so pin 2 reads HIGH when nothing is pressed. Pressing the button pulls it to ground and it reads LOW.
The loop checks the button, then sets the LED to match. Pressed means LED on, released means LED off. It repeats every 5 milliseconds, which feels instant.
Parts list
5 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, greenIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
Kits that can build it
These 12 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- Adafruit MetroX Classic Kit
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| LED, green pin C | Arduino Uno R3 pin GND |
| Pushbutton pin 2a | Arduino Uno R3 pin GND |
| Resistor, 220 Ω pin 1 | Arduino Uno R3 pin 13 |
| Pushbutton pin 1a | Arduino Uno R3 pin 2 |
| LED, green pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Button: a pushbutton on pin 2 lights the LED on pin 13.
//
// INPUT_PULLUP switches on the ATmega328P's 35 k pull-up, so the pin sits at
// 5 V until the button pulls it to GND. That is why the button reads LOW when
// it is pressed: wire one side to pin 2 and the other to GND, no resistor.
const int BUTTON_PIN = 2;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
bool pressed = digitalRead(BUTTON_PIN) == LOW;
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
delay(5);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once at power-up. pinMode(BUTTON_PIN, INPUT_PULLUP) makes pin 2 an input with the internal pull-up switched on, so you need no extra resistor. pinMode(LED_BUILTIN, OUTPUT) makes the LED pin an output. On the Uno, LED_BUILTIN is pin 13.
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}loop sketch.ino
Reads the button with digitalRead and sets pressed to true when the pin is LOW. Then digitalWrite sends HIGH to the LED if pressed, LOW if not. The short delay(5) keeps the loop from spinning needlessly fast.
void loop() {
bool pressed = digitalRead(BUTTON_PIN) == LOW;
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
delay(5);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED is on when the button is not pressed This project
The logic is flipped. With INPUT_PULLUP the pin is LOW when pressed, so the sketch compares against LOW. - The LED never lights This project
Check that the button's other side goes to ground, and that the LED is on pin 13 through the resistor with its anode toward the resistor. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.