Zach Christensen
arduino journey
P03First LightSession 3✓ Working

Button LED

First input. Floating pins, pull-ups, and why pressed reads LOW.

Carried forward fromP02 External LED
Running
The build

The sketch

03_Button_LED.inocomments stripped
const uint8_t PIN_LED = 9;const uint8_t PIN_BTN = 2;void setup() {  pinMode(PIN_BTN, INPUT_PULLUP);  pinMode(PIN_LED, OUTPUT);  Serial.begin(9600);}void loop() {  bool pressed = (digitalRead(PIN_BTN) == LOW);  digitalWrite(PIN_LED, pressed ? HIGH : LOW);  Serial.println(pressed ? "PRESSED" : "released");  delay(50);}

Same LED on D9, plus a button on D2. Hold it down and the LED is on, let go and it is off. Up to now every pin was an output and I decided what happened. This is the first one where the world decides and I react.

Why INPUT_PULLUP works

The internal pull-up is a roughly 30 kΩ resistor on the silicon, between 5 V and the pin. What changes is whether current flows through it.

SwitchCurrentDrop across the pull-upPin sits at
Opennone0 × 30000 = 0 V5 V, HIGH
Closed5 / 30000 ≈ 0.17 mA0.00017 × 30000 = 5 V0 V, LOW

So open is HIGH and closed is LOW. Active-low falls out of the circuit, it is not a property of buttons. A pull-down with the switch going to 5 V gives the opposite convention.

What I learned