Zed-King Institute

    Arduino Push Button to Control an LED

    mediumDigital inputArduino (C++)

    Reading an input and reacting to it — the basis of every interactive Arduino project.

    The question

    A push button is on pin 11 (use INPUT_PULLUP) and an LED on pin 3. The LED must be ON while the button is pressed and OFF otherwise.

    The sketch

    sketch.ino

    void setup() {
      pinMode(11, INPUT_PULLUP);
      pinMode(3, OUTPUT);
    }
    
    void loop() {
      if (digitalRead(11) == LOW) {
        digitalWrite(3, HIGH);
      } else {
        digitalWrite(3, LOW);
      }
    }

    Wiring

    Button: one side → pin 11, other side → GND. LED → 220Ω → pin 3, cathode → GND.

    How it works

    1. 1INPUT_PULLUP turns on the Arduino's internal resistor, so no external resistor is needed.
    2. 2With a pull-up, the pin reads HIGH when the button is released and LOW when it is pressed.
    3. 3digitalRead() checks the button inside loop(), and an if decides the LED state.

    Common mistakes

    • Checking == HIGH for "pressed" while using INPUT_PULLUP — the logic is reversed.
    • Using plain INPUT without a resistor, so the pin reads random values.

    Now solve a similar question yourself

    A new Arduino question every time, checked instantly.

    Practice questions