Learn Arduino UNO · Lesson 4

Give the Arduino a real-world input.

Until now the UNO has simply followed a repeating program. A tactile pushbutton lets the circuit react to you. We will use Arduino’s internal pull-up resistor so the wiring stays simple and reliable.

30 minutesDigital inputINPUT_PULLUP

Keep your LED circuit

Leave the LED from Lesson 1 on pin 8. Add one tactile button across the breadboard centre trench.

  • One side of button → digital pin 2
  • Opposite side → GND rail
  • No external pull-down resistor required
▶ YOUTUBE DEMO PLACEHOLDERSuggested title: “Arduino Pushbutton Explained — INPUT_PULLUP Made Simple”

Wire the button

UNO digital pin 2
→
Tactile button → GND

Place the common four-leg tactile switch across the breadboard centre gap. Two legs on one side are already connected internally, and the other pair are connected internally. Pressing the button joins the two sides.

Important: with INPUT_PULLUP, the logic is intentionally reversed. Not pressed reads HIGH. Pressed connects the pin to ground and reads LOW.

Button controls LED

const int ledPin = 8;
const int buttonPin = 2;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

The logic table

ButtonPin 2 readsLED
Not pressedHIGHOFF
PressedLOWON

Reading this table before reading the code is a useful habit. It separates what you want the hardware to do from the syntax used to make it happen.

Experiments

  1. Reverse the LED behaviour so it is normally on and turns off while the button is pressed.
  2. Add a 200 ms delay inside the pressed condition. What changes?
  3. Move the button from pin 2 to pin 4 and update only the pin variable.

Challenge

Make the LED blink only while the button is being held. You already know every instruction needed: digitalRead(), if, digitalWrite() and delay().