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.
Beginner Runs in your browser. Free, and no account needed.
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);
}
Parts list
5 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 1 × Resistor, 220 Ω
- 1 × LED, green
- 1 × Pushbutton
How it is wired
4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; LED, green pin C; Pushbutton pin 2a
- Arduino Uno R3 pin 13; Resistor, 220 Ω pin 1
- Arduino Uno R3 pin 2; Pushbutton pin 1a
- Resistor, 220 Ω pin 2; LED, green pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.
Free forever. Every board and part. Saves your projects. Works on Chromebooks.