Projects / LED and button controller

LED and button controller

Beginner45 minGuided buildReviewed
Arduino UnoD9D2GND220 Ωtap the button
D2 = HIGH (released)LED current ≈ 0 mASerial: waiting for a press

Tap the parts to see what each one does. A teaching simulation, not a circuit simulator.

Build it step by step

0 of 4 done
  1. UNO

    Place the button

    Put the button across the centre gap so its two sides sit in different rows.

  2. UNO

    Wire the button

    One side to D2, the other side to GND. The code turns on the internal pull-up, so no resistor is needed.

  3. UNO9.5 mA

    Add the LED and resistor

    D9 to the 220 Ω resistor, resistor to the LED's long leg, short leg to GND.

  4. UNO

    Upload and test

    Upload, open the serial monitor at 9600 baud, and press the button a few times.

In short

How do you build an LED and button controller?

Press a button to toggle an LED on an Arduino Uno, with software debounce. You need Arduino Uno R3, LED 5 mm red, Resistor 220 Ω, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 4 steps and about 45 minutes.

Level
Beginner
Time
About 45 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Uno D9220 Ω resistor, then LED anode (long leg)LED output
LED cathode (short leg)Uno GNDReturn path
Uno D2One side of the buttonButton input, internal pull-up
Other side of the buttonUno GNDPressing pulls D2 low

Code

Arduino IDE, board "Arduino Uno". No extra libraries.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int LED_PIN = 9;
const int BUTTON_PIN = 2;

bool ledOn = false;
int lastReading = HIGH;
int stableState = HIGH;
unsigned long changedAt = 0;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int reading = digitalRead(BUTTON_PIN);
  if (reading != lastReading) {
    changedAt = millis();
    lastReading = reading;
  }
  if (millis() - changedAt > 30 && reading != stableState) {
    stableState = reading;
    if (stableState == LOW) {
      ledOn = !ledOn;
      digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
      Serial.println(ledOn ? "LED on" : "LED off");
    }
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Press and release onceLED changes state once, serial prints "LED on" or "LED off"
Measure voltage across the 220 Ω resistor with the LED onAbout 3 V, so about 13.6 mA
Measure D2 with the button released, then pressedAbout 5 V released, about 0 V pressed
Press quickly ten timesExactly ten toggles; extra toggles mean bounce, so raise 30 ms to 50 ms

If it does not work

LED never lights

Check the LED is the right way round: long leg to the resistor.

LED toggles by itself

D2 is floating: check INPUT_PULLUP is set and the button goes to GND.

One press toggles twice

Contact bounce: increase the debounce time.

Upload error

Select board "Arduino Uno" and the right port; try another USB cable.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the LED and button controller?

Arduino Uno R3, LED 5 mm red, Resistor 220 Ω, Tactile push button 6 mm and Breadboard 830 Tie Points.

How long does the LED and button controller take to build?

About 45 minutes for a beginner maker, in 4 steps.

How do I know the LED and button controller works?

Press and release once: LED changes state once, serial prints "LED on" or "LED off". Measure voltage across the 220 Ω resistor with the LED on: About 3 V, so about 13.6 mA. Measure D2 with the button released, then pressed: About 5 V released, about 0 V pressed.

LED never lights: what should I check?

Check the LED is the right way round: long leg to the resistor.

LED toggles by itself: what should I check?

D2 is floating: check INPUT_PULLUP is set and the button goes to GND.

One press toggles twice: what should I check?

Contact bounce: increase the debounce time.

Upload error: what should I check?

Select board "Arduino Uno" and the right port; try another USB cable.