Start here · 03 / 16

Button Input with digitalRead

Press a virtual button to control an LED. Follow the D2 input, see why the circuit uses a pull-down resistor, and read HIGH and LOW.

6 minBeginner
What you’ll learn

Read a digital input and use it to control an LED.

Understand it

A pull-down sets a known LOW state when the button is released. Pressing the button makes the input HIGH, and the LED mirrors that state. This is an idealized switch; contact bounce is not modeled.

Your experiment

Run the circuit, press the button, then release it. Watch D2 and the LED rather than judging the result from the button animation alone.

Connections

  • D2 — Button + 10 kΩ pull-down
  • D13 — Resistor → LED
  • 5V / GND — Input supply / return

Sketch

This sketch illustrates the hardware logic. The workbench runs its corresponding teaching model, not a C++ compiler.

void setup() {
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  int pressed = digitalRead(2);
  digitalWrite(13, pressed);
}

Quick check

What keeps D2 LOW when the button is released?

Show explanation

The pull-down resistor provides a defined resting state. Without a suitable pull-up or pull-down arrangement, an input can float.

Go beyond the experiment

Connect the experiment with the fundamentals · Help & FAQ