Sub topic 7
Using the Internal Pull-Up with `INPUT_PULLUP`
Assume you know the basic Arduino sketch structure, voltage and resistance, breadboard wiring, and digitalWrite. This chapter uses an Arduino Uno-style board. The exact pull-up resistance and available pins can vary between boards.
Why an input needs a defined level
A digital input should read one of two logic levels:
HIGH, which means the pin voltage is near the board's logic supply voltage.LOW, which means the pin voltage is near ground, or 0 volts.
A button does not create a reliable voltage by itself. It only connects or disconnects two points. If the button is open and the input has no electrical connection to either voltage or ground, the pin is floating. A floating input can pick up electrical noise and appear to change randomly.
A pull-up resistor gives the input a default HIGH level. Pressing the button then connects the input to ground and changes it to LOW.
Wiring the button to ground
Use a button connected between digital pin 2 and GND.
When the button is released, the internal resistor gently pulls pin 2 toward HIGH. When the button is pressed, the button creates a much lower-resistance path to ground, so the pin reads LOW.
For a four-legged tactile button, place the button across the center gap of the breadboard. Legs on the same side are already connected inside the button. Use legs from opposite sides so pressing the button changes the connection.
Configuring `INPUT_PULLUP`
Set the pin mode like this:
pinMode(buttonPin, INPUT_PULLUP);This does two things:
- It configures the pin as an input.
- It connects a built-in pull-up resistor to the pin.
The resistor is not an exact precision resistor. Its value depends on the board and microcontroller, but it is suitable for a short button wire in ordinary projects.
Because the button connects to ground, the logic is inverted:
- Released button:
HIGH - Pressed button:
LOW
This pattern is called active-low logic. A signal is active when it is LOW, rather than when it is HIGH.
const int buttonPin = 2;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int level = digitalRead(buttonPin);
if (level == LOW) {
Serial.println("pressed");
} else {
Serial.println("released");
}
delay(200);
}Upload the sketch and open the Serial Monitor at 9600 baud. The monitor should report released until you press the button. While you hold it down, it should report pressed.
The delay(200) limits the number of messages, but it does not remove the brief electrical bouncing that happens when mechanical contacts open or close. A later chapter will handle that with debouncing.
When to use an external resistor
The internal pull-up is convenient for ordinary buttons:
- It needs no external resistor.
- It reduces wiring.
- It gives the input a stable default state.
Use an external pull-up when you need a known resistance, a stronger pull-up, a different voltage, or special electrical timing. An external pull-down is another valid design, but it uses HIGH for a press and requires the resistor to connect the input to ground.
Exercise
A button is wired between digital pin 2 and GND, and the sketch uses pinMode(2, INPUT_PULLUP). What should digitalRead(2) return while the button is pressed?
Show answer
It should return LOW because pressing the button connects the pin to ground.