Button on pin 12 lights the LED on pin 13, on an Arduino Uno
A pushbutton from 5 V to pin 12, held low by a 10k pull-down, lights the Uno’s own LED on pin 13 while it is pressed. The whole build, an Arduino Uno and 2 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 $30. On Mokxi they're free.
What you'll learn
- Pull-down resistors
- Why an input pin must not float
- Reading a button with
digitalRead - Reacting only when the state changes
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
One side of the pushbutton goes to 5V. The other side goes to pin 12, and a 10k resistor runs from that same point to ground. With the button up, the resistor holds pin 12 at 0 V, so it reads LOW. This is called a pull-down.
When you press the button, it joins pin 12 to 5V and the pin reads HIGH. The sketch copies that reading to pin 13, the Uno's own LED, so the LED is on while you hold the button. It also prints a message each time the state changes.
Parts list
4 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop 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 |
|---|---|
| Resistor, 10k Ω pin 1 | Arduino Uno R3 pin GND |
| Pushbutton pin 2a | Arduino Uno R3 pin 12 |
| Pushbutton pin 1a | Arduino Uno R3 pin 5V |
| Resistor, 10k Ω pin 2 | Pushbutton pin 2b |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A button on pin 12 lights the LED on pin 13.
//
// one side of the button -> 5V
// the other side -> pin 12, and a 10k resistor from pin 12 to GND
// the LED is the Uno's own, on pin 13
//
// The 10k is a pull-down: with the button up it holds pin 12 at 0 V, so the
// pin reads LOW instead of floating. Pressed, the button joins pin 12 to 5 V
// and it reads HIGH. (On the bench a 0.1 uF capacitor across the resistor
// smooths out the contact's bounce.)
//
// Hold the button on the canvas and watch the L LED on the board.
const int BUTTON = 12;
const int LED = 13;
int last = LOW;
void setup() {
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: hold the button on pin 12");
}
void loop() {
int now = digitalRead(BUTTON);
digitalWrite(LED, now);
if (now != last) {
last = now;
Serial.println(now == HIGH ? "pressed: LED on" : "released: LED off");
}
delay(5);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets BUTTON (pin 12) as an input and LED (pin 13) as an output, then starts serial at 9600 and prints a hint.
void setup() {
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: hold the button on pin 12");
}loop sketch.ino
Reads the button into now and writes it straight to the LED. If now differs from last, it saves the new state and prints "pressed: LED on" or "released: LED off". A 5 ms delay ends each pass.
void loop() {
int now = digitalRead(BUTTON);
digitalWrite(LED, now);
if (now != last) {
last = now;
Serial.println(now == HIGH ? "pressed: LED on" : "released: LED off");
}
delay(5);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED flickers or comes on by itself This project
The pull-down resistor is missing or loose. Pin 12 needs the 10k to ground to hold it LOW. - The LED stays off when pressed This project
Check that the other side of the button goes to 5V, not to GND. - 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. - 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. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.