DIY ELECTRONICSTR

← All projects

Toggle an LED with a Button

Beginner20 min

Turn a red LED on and off with a button: every press flips the LED, and the press count and the LED's state are written to the Serial Monitor. It teaches reading a button with digitalRead, the Arduino's built-in pull-up resistor (INPUT_PULLUP), catching the moment a button is pressed and waiting out contact bounce (debounce). The button needs no extra resistor, and no extra library is needed.

Build it step by step in the app

What you need

Wiring steps

  1. 1

    Connect the common ground (GND)

    Connect the common ground (GND)

    Plug a black jumper wire into the GND pin next to pin 13 on the Arduino's digital header. Connect its other end to the breadboard's top blue (−) rail. Both the LED's resistor and the button's ground wire will go into this rail.

    • Arduino GND → the top blue (−) rail (black wire)
  2. 2

    Add the LED and its resistor

    Add the LED and its resistor

    Plug the red LED's long leg (+) into column 10 in the top half of the breadboard and its short leg (−) into column 11. Put one end of the 220 Ω resistor in column 11 and the other end in the top (−) rail. Then connect the Arduino's pin 8 to column 10 with an orange jumper wire (LED_PIN 8). The resistor limits the current through the LED; without it both the LED and the Arduino's pin can be damaged.

    • red LED long leg (+) → column 10, top half
    • red LED short leg (−) → column 11, top half
    • 220 Ω resistor (leg 1) → column 11, top half
    • 220 Ω resistor (leg 2) → the top blue (−) rail
    • Arduino pin 8 → column 10, top half (orange wire)
  3. 3

    Add and wire the button

    Add and wire the button

    Seat the button over the middle gap of the breadboard: two legs in the top half and two in the bottom half, in columns 5 and 7. Connect the Arduino's pin 2 to column 5 in the bottom half with a green jumper wire (BUTTON_PIN 2). Then connect column 7 in the top half to the top (−) rail with a short black wire. Because you use two diagonal legs of the button, it works whichever way round it sits. The button needs no resistor of its own: the Arduino's built-in one (INPUT_PULLUP) keeps it HIGH when released, and pressing it reads LOW.

    • push button → across the middle gap, columns 5 and 7
    • Arduino pin 2 → column 5, bottom half (green wire)
    • column 7, top half → the top blue (−) rail (black wire)

Arduino sketch

// The button's and the LED's pins
const int BUTTON_PIN = 2;
const int LED_PIN = 8;

// Time to let the contacts settle (ms)
const unsigned long BOUNCE_TIME = 50;

bool ledOn = false;
int lastReading = HIGH;
int presses = 0;

void setup() {
  // Pull-up: a released button reads HIGH
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Press the button to turn the LED on or off");
}

void loop() {
  int reading = digitalRead(BUTTON_PIN);

  // Act only when the button changes
  if (reading != lastReading) {
    // LOW: the button was just pressed
    if (reading == LOW) {
      ledOn = !ledOn;
      digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
      presses++;
      Serial.print("Press ");
      Serial.print(presses);
      Serial.println(ledOn ? ": LED on" : ": LED off");
    }
    lastReading = reading;
    // Wait while the contacts bounce
    delay(BOUNCE_TIME);
  }
}

What the Serial Monitor shows

SERIAL MONITOR · 9600 BAUD
Press the button to turn the LED on or off
Press 1: LED on
Press 2: LED off
Press 3: LED on

What you will see

Every press flips the LED: it lights on the first press, goes out on the second and lights again on the third. Holding the button down changes nothing; what counts is the moment it is pressed. If nothing happens when you press the button, check that it sits over the middle gap, the green wire is in column 5 and the black wire in column 7. If the LED never lights, check that its long leg is in column 10 (the same column as pin 8's wire).

In the app, tick off every wire, match the parts to your inventory and pick up where you left off.

Build it step by step in the app