LEDs and buttons: digital pins explained
Labs 1a and 1b use LEDs and push buttons. The lab sheets show the circuits; this page explains the ideas behind them and why a button sometimes acts "possessed".
Digital pins in one table
| Code | Does |
|---|---|
pinMode(pin, OUTPUT) | The pin drives something (LED) |
pinMode(pin, INPUT) | The pin reads something; needs an external pull-up or pull-down resistor |
pinMode(pin, INPUT_PULLUP) | Reads, using the Uno's built-in pull-up resistor |
digitalWrite(pin, HIGH) / LOW | Output 5 V / 0 V |
digitalRead(pin) | Returns HIGH or LOW |
pinMode goes in setup(). Forgetting it is a common reason an LED glows only faintly: the pin is still an input.
LEDs
An LED needs a resistor (220–330 Ω) in series, or it draws too much current and can damage itself and the pin. The long leg goes toward the Arduino pin, the short leg toward GND.
| LED problem | Check |
|---|---|
| Never lights | Backwards? Resistor in the same breadboard row as the LED's other leg (shorted)? Right pin number? pinMode set? |
| Very dim | pinMode(pin, OUTPUT) missing, or a huge resistor (e.g. 10 kΩ instead of 220 Ω) |
| Always on | Wired to 5V instead of the pin |
| Test the LED | Move its wire from the pin to 5V: if it still does not light, it is the LED or wiring, not the code |
Why a button needs a pull-up or pull-down
A button only connects or disconnects. When it is not pressed, the input pin is connected to nothing: it is floating, and picks up electrical noise from your hand and the wires. digitalRead then returns random HIGH/LOW. That is the "button presses itself" problem.
A resistor fixes it by holding the pin at a known level when the button is open:
| Wiring | Not pressed | Pressed |
|---|---|---|
| Pull-down: 10 kΩ from pin to GND, button from pin to 5V | LOW | HIGH |
| Pull-up: 10 kΩ from pin to 5V, button from pin to GND | HIGH | LOW |
| INPUT_PULLUP: no resistor, button from pin to GND | HIGH | LOW |
With INPUT_PULLUP the logic is inverted: pressed = LOW. That surprises everyone the first time:
pinMode(2, INPUT_PULLUP);
if (digitalRead(2) == LOW) {
// the button IS pressed
}
Bouncing: one press, many presses
A mechanical button does not close cleanly: the contacts bounce for a few milliseconds, so one press can be read as 5–20 presses. You see it when you count presses or toggle an LED and it sometimes skips.
Simple fix: after detecting a change, wait about 50 ms and only act once per change.
const int buttonPin = 2;
int lastState = HIGH;
unsigned long lastChange = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(buttonPin);
if (state != lastState && millis() - lastChange > 50) {
lastChange = millis();
if (state == LOW) {
Serial.println("pressed"); // runs once per press
}
lastState = state;
}
}
Pressed vs "while pressed"
| You want | Check |
|---|---|
| Something happens while the button is held (LED on while held) | digitalRead(pin) == LOW every loop |
| Something happens once per press (count, toggle) | The change from not-pressed to pressed (see the code above) |
Using the first when you meant the second is why a "toggle" flickers on and off while you hold the button.
delay() blocks buttons
During delay(1000) the Uno does nothing else. A button pressed during the delay is missed. For blinking while also reading a button, use millis():
unsigned long lastBlink = 0;
bool ledOn = false;
void loop() {
if (millis() - lastBlink >= 500) { // every 500 ms, without stopping
lastBlink = millis();
ledOn = !ledOn;
digitalWrite(13, ledOn);
}
// ...read buttons here; they are checked thousands of times a second
}
Useful debugging trick
Print what the Uno actually reads:
Serial.println(digitalRead(2));
Watch the Serial Monitor: it should change between 0 and 1 only when you press. Random changes = floating input (missing pull-up/down). Never changes = wrong pin or the button sits the wrong way round (legs on the same side are always connected).