Push Button & LED Control
Start with the “hello world” of electronics — a blinking LED — then wire up a push button so your Arduino can read your finger and switch the light on and off on command.
What you'll learn
- How the Arduino sends power out of a pin (a digital output) to light an LED — and why an LED needs a resistor and must face the right way
- What a digital input is, and how
digitalRead()checks whether a pin is HIGH or LOW - Why a button needs a pull-down resistor, and how to make the LED react to a press the instant it happens using
if/else
What you'll build: first an LED that blinks all on its own, then the same LED wired to a push button — it lights the instant you press and goes dark the moment you let go. Your Arduino's first taste of both talking and listening.
Components Needed
- 1× Arduino Uno R3
- 1× USB programming cable
- 1× Breadboard (MB-102)
- 1× LED (red, yellow, or blue)
- 1× 220Ω resistor red–red–brown · protects the LED
- 1× Tactile push button (the 12×12 clicky square, 4 legs)
- 1× 10kΩ resistor brown–black–orange · the button's pull-down
- 4–6× Male-to-male jumper wires
Connections
This is the very first circuit of the course, so take it one line at a time. Build the LED half first and get it blinking, then add the button — its four legs are the part that trips people up, so save them for last.
| From | To |
|---|---|
| Arduino pin 12 | one end of the 220Ω resistor (red–red–brown) |
| other end of the 220Ω resistor | the LED's long leg (the + side, the anode) |
| the LED's short leg (the − side, the cathode) | the breadboard − (GND) rail |
| Arduino 5V | one button leg — call it leg A |
| the button leg diagonally opposite (leg B) | Arduino pin 2 |
| that same leg B row | one end of the 10kΩ resistor (brown–black–orange) |
| other end of the 10kΩ resistor | the breadboard − (GND) rail |
| Arduino GND pin | the breadboard − (GND) rail |
Tutorial
Do the LED half first — it's your warm-up. Push the LED into the breadboard so its two legs land in different rows, run pin 12 through the 220Ω resistor to the LED's long leg, and the short leg down to the − (GND) rail. Add a wire from the Arduino's GND pin to that rail. Plug in USB only after you've checked the LED's long leg goes to the resistor side. Once it's blinking, add the button: straddle it across the centre gap, feed 5V into leg A, and run the diagonal leg B to both pin 2 and the 10kΩ resistor down to GND.
Open the Arduino IDE, type this in, and upload it:
// Blink warm-up: flash the LED on and off #define LED_PIN 12 // the LED is wired to pin 12 void setup() { pinMode(LED_PIN, OUTPUT); // pin 12 is an OUTPUT (sends power out) } void loop() { digitalWrite(LED_PIN, HIGH); // LED ON (HIGH = 5 volts) delay(500); // wait half a second digitalWrite(LED_PIN, LOW); // LED OFF (LOW = 0 volts) delay(500); // wait, then repeat }
If it blinks on for half a second, off for half a second, your output half is perfect. Now teach the board to listen.
Add the button wiring from the table, then upload this:
// --- Pin setup --- #define BUTTON_PIN 2 // pin 2 "listens" to the button (digital INPUT) #define LED_PIN 12 // pin 12 drives the LED (digital OUTPUT) void setup() { pinMode(BUTTON_PIN, INPUT); // pin 2 will READ voltage: is it pushed? pinMode(LED_PIN, OUTPUT); // pin 12 will SEND voltage to the LED } void loop() { int buttonState = digitalRead(BUTTON_PIN); // HIGH (pushed) or LOW (released) if (buttonState == HIGH) { // pushed -> 5V reaches pin 2 digitalWrite(LED_PIN, HIGH); // ...turn the LED ON } else { // released -> pull-down holds pin 2 at 0V digitalWrite(LED_PIN, LOW); // ...turn the LED OFF } }
Press and hold — the LED should light the instant you press and go dark the instant you let go. Tap it fast and watch the light keep up: your Arduino is now reading your finger thousands of times a second and reacting every time.
Try for Yourself
Now make it yours. Try these before peeking at the answers:
- Flip the logic. LED normally ON, goes dark only while you hold the button — a “hold to turn off” night light.
- Make it latch. One press turns it on and it stays on; press again to turn it off — a proper on/off switch.
- Press to blink. Bring back the Step-1 blink, but only while the button is held. Output and input working together.
💡 Answers ›
1. Flip the logic — swap the HIGH/LOW inside the if/else: pushed → digitalWrite(LED_PIN, LOW), released → digitalWrite(LED_PIN, HIGH).
2. Make it latch — remember the state in a variable and act only on the press edge (was LOW last loop, is HIGH now): flip ledOn = !ledOn, then a small delay(50) to debounce.
3. Press to blink — put the Step-1 blink (HIGH · delay · LOW · delay) inside the “pressed” branch, and keep the LED off in the else.
What Actually Just Happened?
In Step 1 you made the Arduino talk. digitalWrite(LED_PIN, HIGH) told pin 12 to push out 5 volts; that shoves a tiny current through the resistor, through the LED, and out to GND — a glowing LED is just what an LED does when current flows the right way. LOW sets the pin back to 0 volts and the light goes out. That's a digital output — the Arduino flipping a pin on and off like a switch it controls with code.
In Step 2 you taught it to listen. digitalRead(BUTTON_PIN) asks one question: is there voltage on pin 2 right now? “Yes” = HIGH, “no” = LOW. That single yes/no is a digital input — the simplest way it senses the world outside itself.
Here's the surprise: an input pin is nosy. If it isn't firmly wired to something, it floats — picking up stray voltage from your hand, nearby wires, even the air — and flips between HIGH and LOW on its own, making the LED flicker for no reason.
That's the whole job of the 10kΩ resistor. Button not pressed → it quietly ties pin 2 to GND, so the pin reads a rock-solid LOW. Press → 5V rushes in through leg A and overpowers that gentle pull, so the pin jumps to HIGH. We call it a pull-down resistor because it pulls the pin down to ground.
Troubleshooting
Nine times out of ten it's backwards — the LED's long leg should be on the resistor (+) side. Flip it around. Also make sure the two LED legs sit in different rows; both in one row shorts past the LED.
The code didn't reach the board. Check Tools → Board = Arduino Uno and Tools → Port = the port that appears when you plug in. If the board's power light is dark, suspect the USB cable or port first.
Start at the button. Make sure your 5V wire and pin-2 wire go to legs that are diagonally opposite — those connect only on a press. Same-side legs do nothing.
Either (1) your 5V and pin-2 wires are on two legs already joined inside the button — move one to the diagonal leg; or (2) the pull-down isn't working — the 10kΩ must run from the pin-2 row to the − (GND) rail, and that rail must reach the Arduino's GND.
That's a floating pin — the pull-down is missing, in the wrong row, or not reaching ground. Trace the 10kΩ: one end shares the pin-2 row, the other lands in the − (GND) rail.
