Lab 06
Intermediate
Arduino

Read a button safely

Avoid a floating input by using INPUT_PULLUP and understand why a pressed button reads LOW.

MCU:Arduino Uno · Digital input
Official reference documentation
01

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.
02

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
03

Circuit diagram

04

Code

Study the highlighted firmware, then copy it into the matching development environment. Adapt board pins and initialization before compiling.

main.c
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.

05

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/..."
06

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.

07 · 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.

Up next · Lab 01

Blink an LED on STM32

Next Lab
Back to all labs