Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← 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 R3

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 List

  • Arduino Uno R3 is a 5 V ATmega328P microcontroller board for direct control of sensors, switches and small driver circuits using a single uploaded program. UNO-format board with USB-B socket, barrel connector, ATmega328P and digital/analog header labels. The canonical Uno R3 is separate from the generic compatible-board record. Identify pins from the board labels/pinout rather than a diagram for a newer Uno generation.
  • An 830-contact solderless breadboard joins inserted leads through hidden spring strips so circuits can be rebuilt without solder. Full-size rectangular board, center channel, numbered five-hole terminal groups and side power rails. Counted contact holes are not component units.
  • An assortment of discrete indicator LEDs provides several visible colors for status and display experiments. Loose two-lead lens packages of different tints; transparent lenses can emit several different colors.
  • A mixed resistance assortment supplies values for current limiting, biasing and divider experiments. Groups of axial two-lead resistors; use labeled storage and measure uncertain parts out of circuit.
  • A small tactile pushbutton closes a momentary contact while pressed to request a digital action. Small square four-legged tactile switch. Confirm paired contacts by continuity; the switch may bridge two legs even before pressing.
  • Male-to-male jumper leads make removable point-to-point connections for low-current prototype signals. Pins at both ends; count individual leads rather than ribbon groups. Check intact insulation and snug contacts before use.
  • A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. Rectangular USB-A plug at one end and square USB-B plug at the other; standalone purchase length is recorded as ten feet. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.

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. Reuse the Lab 2 D8 LED circuit if it is still assembled, or build a new D8 → 220–330 Ω resistor → LED anode → LED cathode → GND branch.

  2. 02

    Examine the four-legged tactile button before wiring it. On a typical switch, two legs are already connected on each side, and pressing the button connects the two sides. Place it across the breadboard center gap and use a continuity meter or the button’s pinout to identify a pair that connects only when pressed.

  3. 03

    Connect Uno D2 to one side of that switched pair and Uno GND to the other. Do not connect the button to 5V: the core program uses INPUT_PULLUP, so an unpressed input reads HIGH and a pressed input reads LOW.

  4. 04

    Check the LED branch and the button independently before reconnecting USB. Select Hold button to light LED, then Save, Build and Upload; the LED should light only while the button is held.

  5. 05

    Optional check: select Observe button in PlatformIO Monitor, upload that example and view its readings at 9600 baud. Compare the released HIGH state with the pressed LOW state.

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 the button at least three times; confirm the D8 LED follows the press.
  • For the optional serial check, upload Observe button in PlatformIO Monitor and open PlatformIO Monitor at 9600 baud: released reads HIGH and pressed reads LOW.

04

Code

Development · VS Code + PlatformIO

Arduino Uno R3

Replace src/main.cpp with one complete example. Save → Build → Upload. These labs need no external libraries.

Copy the Uno platformio.ini
platformio.ini
[env:uno]
platform = atmelavr
board = uno
framework = arduino
monitor_speed = 9600

Getting Started: project and board setup →

Code experiments
01-button-controls-led.cpp
#include <Arduino.h>

// D2->button->GND; D8->resistor->LED->GND.
// Button input on D2 and LED output on D8 are two different jobs.
const int buttonPin = 2;
const int ledPin = 8;

void setup() {
  // INPUT_PULLUP activates an internal resistor. Released reads HIGH; pressed to GND reads LOW.
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // if runs its block only when the expression is true. == compares values; LOW means pressed
  // here.
  if (digitalRead(buttonPin) == LOW) {
    digitalWrite(ledPin, HIGH); // LOW means pressed.
  // else handles the other case: button released, so the LED should be off.
  } else {
    digitalWrite(ledPin, LOW);
  }
}

Experiments & challenges

  • Watch Serial values in the optional observation program 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

Choose Arrays of input/output pairs in the code selector above. Index matching button and LED arrays in a loop.

Choose Stable press toggle in the code selector above. Use a 30 ms stable transition before toggling, without blocking the loop.

What to remember

Why is pressed LOW here?
INPUT_PULLUP holds the released input HIGH through an internal resistor. Pressing the button connects that input to GND, so digitalRead() returns LOW.
What extra information must a program remember to react once per press?
Remember the previous debounced button state and compare it with the current state. Act only when it changes from released to pressed; while the button stays held, do not trigger again. Debouncing prevents contact bounce from counting as extra presses.

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 PlatformIO Uno environment and upload 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 program or increase the debounce interval.