typestar

INPUT_PULLUP in Arduino

The internal resistor holds the pin HIGH; a press pulls it LOW.

// INPUT_PULLUP holds the pin HIGH; pressing pulls it LOW.
const int DOOR_PIN = 2;
const int LAMP_PIN = 13;

void setup() {
  pinMode(DOOR_PIN, INPUT_PULLUP);
  pinMode(LAMP_PIN, OUTPUT);
}

void loop() {
  // Closed switch reads LOW, so the logic reads inverted.
  if (digitalRead(DOOR_PIN) == LOW) {
    digitalWrite(LAMP_PIN, HIGH);
  } else {
    digitalWrite(LAMP_PIN, LOW);
  }
}

How it works

  1. pinMode(DOOR_PIN, INPUT_PULLUP) enables the built-in resistor.
  2. Closed switch reads LOW — the logic runs inverted.
  3. No external resistor needed: the chip provides it.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
365
Tokens
69
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 59 seconds.

Type this snippet

Step 1 of 3 in Digital I/O, step 4 of 27 in Language basics.

← Previous Next →