Menu
CoddyTech

The Internal Pull-Up

Part of the Inputs section of Coddy's Arduino journey. Lesson 3 of 22.

You have used INPUT_PULLUP since Read a Button. It switches on a resistor inside the board, 20k to 50k ohm, from the pin to 5V: the pull-down of the last lesson turned the other way, with no extra part. The button only joins the pin to GND.

The catch is that the logic is upside down: up reads HIGH, pressed reads LOW, and == LOW all over a sketch is easy to get backwards. A comparison such as digitalRead(2) == LOW is itself true or false, and a bool is a variable that holds true or false. Store it under a name that says what it means:

bool pressed = digitalRead(2) == LOW;

if (pressed) {
  // the button is down
}

From there on the code reads the right way round. Printing a bool shows 1 for true and 0 for false.

challenge icon

Challenge

Easy

The button joins pin 2 to GND, and the LED is on pin 13:

  • set pin 2 to INPUT_PULLUP
  • store digitalRead(buttonPin) == LOW in a bool called pressed
  • print pressed, light the LED while it is true, then wait 100 ms

Check expects 1 while the button is held and 0 while it is up. Press Run, hold the button, then Check.

Try it yourself

const int buttonPin = 2;
const int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  // switch on the pull-up of the button pin
}

void loop() {
  // bool pressed = ...
  // print it, light the LED while it is true, wait 100 ms
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Inputs

Practice on your own: Arduino playground