← All labs

LAB 004

Pushbutton: Input Changes Output

Press a button to light an external LED and read an active-low input.

Intro labArduino Uno R360 minTeams of 2

01

What you will build

A button between D2 and GND turns a current-limited D8 LED on while pressed.

Why it matters

Your program now reads the world, decides what to do, then drives an output.

What you will learn

  • digitalRead and conditional decisions
  • INPUT_PULLUP: unpressed HIGH, pressed LOW
  • state and event detection as optional depth

02

Parts per station

  • 1 each An independently programmable Uno R3 per simultaneous station.
  • 1 each A small breadboard is a possible substitute after inspection.
  • 1 each Ordinary discrete LEDs; colors are optional.
  • 1 each One verified 220–330 Ω current-limiting resistor per LED; count and values before class.
  • 1 each Four-terminal tactile button; check which legs are internally common.
  • several assorted Count leads usable for the chosen circuit before class.
  • 1 each Availability unconfirmed. One working USB-A to USB-B DATA cable for upload; cable length need not be 10 feet. This canonical item is procurement-only, so existing cable stock must be verified, not assumed.
Station planning and software

Three separately counted Uno R3 boards and four counted 830-point breadboards; fifth complete independent station is unconfirmed. Kit boards and small breadboards may add capacity only after identification and testing. Count usable USB data cables, LEDs, resistors, jumpers and any inputs before meeting. For optional five-pair variants count five buttons, LEDs and resistors per station.

  • Arduino IDE or Arduino CLI with Arduino AVR Boards core; select Arduino Uno and the connected serial port.

Arduino IDE is the only required development environment. Arduino CLI is optional.

03

Assembly

Disconnect USB before changing breadboard wiring; check connections before reconnecting.

Use one 220–330 Ω resistor in series with each ordinary LED. Never connect 5V directly to GND.

Internal pull-up button goes between input pin and GND; never bridge 5V to GND with the switch.

  1. 01

    Disconnect USB; keep the D8 LED and its series resistor from Lab 2.

  2. 02

    Place the four-leg tactile button across the breadboard center gap; check opposing sides with a meter if unsure.

  3. 03

    Connect D2 to one switch side and GND to the other; do not wire this button to 5V.

  4. 04

    Inspect both circuits, reconnect, verify/upload, and press/release the switch.

FromToPurpose / notes
Uno D8LED 1 series resistor 220–330 ΩDrive output with current limitingPut the resistor in series, never across 5V and GND.
LED 1 series resistorLED 1 anode (long leg)Feed LED in forward directionResistor may instead go on the cathode side if it stays in series.
LED 1 cathode (short leg/flat side)Uno GNDComplete the circuitLED must not be connected directly across 5V and GND.
Uno D2button 1 first sideRead active-low buttonUse INPUT_PULLUP; choose legs across the switch, not the two permanently connected legs.
button 1 opposite sideUno GNDPressed reads LOWDo not connect this button to 5V in this configuration.

Predict before you run

  • Why might an unpressed button read HIGH with INPUT_PULLUP?
  • Would an if statement using HIGH for pressed work with this circuit?
Expected result

LED on D8 lights only while the button on D2 is held; release extinguishes it.

  • Press and release at least three times; verify LED follows the button.
  • Open the observation variant and set Serial Monitor to 9600 to see HIGH unpressed and LOW pressed.

04

Code

The internal pull-up holds the input HIGH at rest; a press connects it to ground and reads LOW.

Code experiments
01-button-controls-led.inoDownload .ino
// D2->button->GND; D8->resistor->LED->GND.
const int buttonPin = 2;
const int ledPin = 8;

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

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    digitalWrite(ledPin, HIGH); // LOW means pressed.
  } else {
    digitalWrite(ledPin, LOW);
  }
}

About this version

core · Arduino Uno R3 · beginner

Read D2 with internal pull-up and drive D8.

Same core wiring.

Hardware and wiring for this sketch
  • arduino uno r3 · 1 eachAn independently programmable Uno R3 per simultaneous station.
  • 830 point solderless breadboard · 1 eachA small breadboard is a possible substitute after inspection.
  • assorted leds · 1 eachOrdinary discrete LEDs; colors are optional.
  • resistor assortment · 1 eachOne verified 220–330 Ω current-limiting resistor per LED; count and values before class.
  • tactile pushbutton assortment · 1 eachFour-terminal tactile button; check which legs are internally common.
  • jumper wire assortment · several assortedCount leads usable for the chosen circuit before class.
  • 10 foot usb a to usb b cable · 1 eachOne working USB-A to USB-B DATA cable for upload; cable length need not be 10 feet. This canonical item is procurement-only, so existing cable stock must be verified, not assumed.
FromToPurpose / notes
Uno D8LED 1 series resistor 220–330 ΩDrive output with current limitingPut the resistor in series, never across 5V and GND.
LED 1 series resistorLED 1 anode (long leg)Feed LED in forward directionResistor may instead go on the cathode side if it stays in series.
LED 1 cathode (short leg/flat side)Uno GNDComplete the circuitLED must not be connected directly across 5V and GND.
Uno D2button 1 first sideRead active-low buttonUse INPUT_PULLUP; choose legs across the switch, not the two permanently connected legs.
button 1 opposite sideUno GNDPressed reads LOWDo not connect this button to 5V in this configuration.

How the code works

Lines 11–11

The internal pull-up holds the input HIGH at rest; a press connects it to ground and reads LOW.

Read D2 with internal pull-up and drive D8.

How it works

Your program now reads the world, decides what to do, then drives an output.

Digital input plus an if/else decision changes an output; a stored state enables event-driven behavior.

Experiments & challenges

  • Watch Serial values in the optional observation sketch at 9600 baud.
  • Press and release slowly, then compare press-to-hold with press-once-to-toggle.
Scale it up

Optionally build two inputs/outputs on D2/D3 and D8/D9, then five pairs on D2–D6 and D8–D12, then an array implementation.

Challenge

Create a toggle: each new press flips LED state once, even if the button stays held.

Advanced implementation

Open an advanced sketch in the code selector above. Compare its structure, hardware, and annotated lines with the core sketch.

  • Arrays of input/output pairs: Index matching button and LED arrays in a loop.
  • Stable press toggle: Use a 30 ms stable transition before toggling, without blocking the loop.

Check your understanding

Why is pressed LOW here?

What extra information must a program remember to react once per press?

Learning summary

Digital input plus an if/else decision changes an output; a stored state enables event-driven behavior.

Lab 5 replaces the button’s two states with a range of measured analog values.

Troubleshooting

No upload or no power

Check USB data cable, selected Uno board and serial port; verify the power LED.

Unexpected external LED behavior

Disconnect USB, check the breadboard row, LED polarity, series resistor, GND, then compare the code pin with the wire.

Button appears stuck

Check opposite switch sides, center gap, GND, D2, and INPUT_PULLUP; unpressed should be HIGH.

Toggle flickers or changes twice

A switch can bounce; use the optional debounced sketch or increase the debounce interval.