CCNAP

TPL-11Templates/Pushbutton Toggle LED

Pushbutton Toggle LED

Press the button to toggle a red LED on and off. Uses INPUT_PULLUP so no external resistor is needed on the button — reads HIGH when released, LOW when pressed.

Open in Playgroundbeginner · button · LED · digital input · INPUT_PULLUP · toggle
beginner

FIG. 1Pushbutton Toggle LED · circuitTPL-11

02Sketch

sketch.inoArduino C++
20 lines
// Pushbutton — toggle an LED on each press
const int BTN = 2;
const int LED = 13;
bool ledState = false;
bool lastBtn = HIGH;

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

void loop() {
  bool btn = digitalRead(BTN);
  if (btn == LOW && lastBtn == HIGH) {  // falling edge = press
    ledState = !ledState;
    digitalWrite(LED, ledState);
    delay(20);  // debounce
  }
  lastBtn = btn;
}

03Components

  • 01Arduino Uno
  • 02Pushbutton (wokwi-pushbutton)
  • 03LED (red)
  • 04Resistor 220Ω

04Wiring

Component pinArduino pin
Button pin 1.lPin 2
Button pin 2.lGND
Resistor Pin 1Pin 13
LED Anode (+)Resistor Pin 2
LED Cathode (−)GND

All templates

Ready when you are

Have a circuit in mind?

No hardware, no installs — a browser is enough.