Try Embedded

Breadboard: button and LED

Build a circuit on a breadboard: a pushbutton that lights an LED, and how the breadboard joins them.

Basic · Beginner

In a real product: Product prototype (Consumer electronics): Every product starts as a breadboard circuit like this one before a board is designed.

A breadboard lets you build a circuit without soldering: parts push into its holes, and metal strips under the holes join them. This project builds a pushbutton and an LED on a half-size breadboard, and the button lights the LED.

What you'll learn

  • Which holes a breadboard joins: each half-column of five (a–e, f–j) is one strip
  • Why a pushbutton straddles the centre channel
  • Reading a button with the internal pull-up (INPUT_PULLUP)

How it works

Every column has two strips of five holes, rows a–e and rows f–j, with the channel down the middle between them. Parts in the same strip are connected, and nothing else is.

  • LED and resistor: pin 12 goes to column 3. A wire carries it across the channel to the resistor, which runs from column 3 to column 9. The LED's anode is in column 9 too, and its cathode in column 8, which goes to GND.
  • Button: the 6 mm button straddles the channel in columns 15 and 17. Pressing it joins those two columns. Column 17 goes to pin 2 and column 15 to GND.

pinMode(2, INPUT_PULLUP) turns on a resistor inside the chip that holds pin 2 HIGH. Pressing the button connects it to GND, so it reads LOW. That is why the code checks for LOW to mean "pressed".

Try this

  • Press the button (click it) and watch the LED. Then move the LED's cathode wire and see the circuit break.
  • Add a second LED that is on whenever the first is off.
  • Make each press toggle the LED instead of lighting it only while held (see the pushbutton toggle project).

sketch.ino

// A pushbutton on pin 2 (to GND, with the internal pull-up) lights an LED on pin 12 while it is held.
const int BUTTON = 2;
const int LED = 12;

void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
}

void loop() {
  bool pressed = digitalRead(BUTTON) == LOW;  // pressed connects the pin to GND
  digitalWrite(LED, pressed ? HIGH : LOW);
}

Lessons behind it