Switch an LED with a push button

Read a push button with a digital input, switch an LED on and off with each press, and learn why buttons need debouncing.
  • Level:Beginner
  • Duration: 30 min
  • Ages 10+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Switch an LED with a push button – EEMENTOR STEM™ lesson

So far your programs have only sent signals out. Now the UNO listens. A push button is the simplest input there is: pressed or not pressed. In this lesson each press switches an LED on or off, like a light switch, and you find out why a button that looks simple can fool a fast computer.

What you'll learn

  • How to read a digital input with digitalRead()
  • What a pull-up resistor does and how to use the built-in one
  • How to react to a press only once (edge detection)
  • Why buttons bounce and how to ignore it

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • 1 push button (UNO R3 Starter Kit)
  • 1 LED and 1 resistor, 220 Ω (UNO R3 Starter Kit)
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Wiring

Place the button across the gap in the middle of the breadboard so that each pair of legs is on a different side.

Breadboard wiring diagram: Switch an LED with a push button
Breadboard view. Check each connection against the wiring table below.
From To
Button, one leg UNO pin 2
Button, the diagonally opposite leg GND
UNO pin 8 220 Ω resistor, then the LED's long leg
LED short leg GND

The sketch

const int BUTTON_PIN = 2;
const int LED_PIN = 8;

bool ledOn = false;
int lastReading = HIGH;          // not pressed
unsigned long lastChange = 0;
int presses = 0;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);   // built-in resistor keeps the pin HIGH
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  int reading = digitalRead(BUTTON_PIN);   // LOW = pressed

  // React only when the reading changes, and ignore changes within 50 ms (bounce)
  if (reading != lastReading && millis() - lastChange > 50) {
    lastChange = millis();
    if (reading == LOW) {                  // the button has just been pressed
      ledOn = !ledOn;
      digitalWrite(LED_PIN, ledOn);
      presses++;
      Serial.print("Presses: ");
      Serial.println(presses);
    }
    lastReading = reading;
  }
}

Upload the sketch and open the Serial Monitor at 9600 baud. Each press switches the LED and the counter goes up by exactly one.

How it works

When the button isn't pressed, nothing is connected to pin 2, and a pin that isn't connected to anything picks up electrical noise and reads random values. INPUT_PULLUP switches on a resistor inside the UNO that gently pulls the pin up to 5 V, so it reads HIGH. Pressing the button connects the pin straight to GND, which wins, so it reads LOW.

If the LED simply followed the button, it would only be on while you held it. To make a switch, the sketch remembers the last reading and acts only at the moment it changes from HIGH to LOW. That's called edge detection.

Inside the button, two metal contacts snap together and bounce a few times before they settle, all within a few thousandths of a second. The UNO is fast enough to see every bounce as a separate press. Ignoring any change that comes less than 50 ms after the last one is called debouncing.

Try this next

  • Remove the 50 ms check and watch the counter jump by several numbers on one press.
  • Make a long press (over one second) switch the LED off, and a short press switch it on.
  • Add a second LED and a second button for a two-player reaction game.

Troubleshooting

  • The LED switches by itself: pin 2 isn't really connected to the button. Check that the two legs you use are on opposite sides of the button.
  • The LED is always on or always off: the button may be turned so that both wires go to legs that are always connected. Turn it a quarter turn.
  • The LED doesn't light at all: check that its long leg goes to the resistor.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit