Read a button safely
Avoid a floating input by using INPUT_PULLUP and understand why a pressed button reads LOW.
What you'll learn
- Connect a normally open pushbutton without a floating input.
- Enable the Uno’s internal pull-up resistor.
- Interpret an active-low digital input correctly.
- Recognize why mechanical buttons may need debouncing.
Components needed
- Arduino UnoDigital pin 2 and onboard LED available
- Normally open pushbuttonMomentary contact switch
- Two jumper wiresPin 2 and ground connections
- USB cablePower and sketch upload
Circuit diagram
Circuit diagram placeholder
Digital pin 2 → one side of a normally open button; other button side → GND. INPUT_PULLUP supplies the pull-up, so no external resistor is required.
Diagram coming soonCode
Study the highlighted firmware, then copy it into the matching development environment. Adapt board pins and initialization before compiling.
const byte buttonPin = 2;
void setup() { pinMode(buttonPin, INPUT_PULLUP); pinMode(LED_BUILTIN, OUTPUT);}
void loop() { bool pressed = digitalRead(buttonPin) == LOW; digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);}INPUT_PULLUP enables the internal pull-up resistor. The released input is HIGH. Closing the button to ground makes the input LOW, so this circuit is active-low. This example does not debounce rapid mechanical transitions.
Simulate It
Run the circuit in your browser when a Wokwi simulation is connected to this lesson.
Wokwi simulation placeholder
Add the public Wokwi embed URL for this lab to launch the interactive simulator here.
wokwiEmbedUrl: "https://wokwi.com/projects/..."Expected Output
The onboard LED is off while the button is released and on while it is pressed. The input reads HIGH when released and LOW when pressed.
Expected output image placeholder
Expected Arduino button output placeholder showing LED on while pressed
Diagram coming soon07 · Challenge Task
Take this lab further
Modify the sketch so each valid press toggles the LED instead of holding it on. Add debounce logic and explain why a simple delay is not always ideal.