Sub topic 2
Wiring with INPUT_PULLUP and inverted logic
This chapter assumes you can write a basic sketch with setup() and loop(), and that you know pinMode(), digitalRead() and digitalWrite(). It also assumes you have wired a pushbutton and an LED to a board before. Here you will learn the standard way to wire a button with no external parts, and why the readings come out inverted.
The problem: a pin that is connected to nothing
A digital input pin reads either HIGH or LOW. A pushbutton is only a switch. When pressed, it joins two points. When released, it joins nothing.
Suppose you wire one side of a button to pin 2 and the other side to 5 V. Pressing the button puts 5 V on the pin, so you read HIGH. Releasing it leaves the pin connected to nothing. The pin is then floating: its voltage is set by stray charge, nearby wires, and your hand. The reading flips between HIGH and LOW at random.
A floating pin is the most common cause of a button that "triggers by itself". The fix is to make sure the pin is always connected to a defined voltage, through a resistor, when the button is released.
Pull-up and pull-down resistors
A pull resistor is a resistor that gives the pin a default level. It is large enough that the button can override it.
- A pull-down resistor connects the pin to GND. The idle reading is
LOW. The button connects the pin to 5 V, so a press readsHIGH. - A pull-up resistor connects the pin to the supply voltage. The idle reading is
HIGH. The button connects the pin to GND, so a press readsLOW.
Both work. Pull-up is the usual choice on Arduino because the microcontroller has pull-up resistors built in.
| Scheme | Resistor goes to | Button goes to | Released reads | Pressed reads | External part needed |
|---|---|---|---|---|---|
| External pull-down | GND | 5 V | `LOW` | `HIGH` | Yes, about 10 kΩ |
| External pull-up | 5 V | GND | `HIGH` | `LOW` | Yes, about 10 kΩ |
| Internal pull-up | 5 V, inside the chip | GND | `HIGH` | `LOW` | No |
Using INPUT_PULLUP
Calling pinMode(pin, INPUT_PULLUP) switches on the pull-up resistor inside the microcontroller for that pin. On classic AVR boards such as the Uno, this resistor is roughly 20 kΩ to 50 kΩ. Other boards differ, so check the datasheet if the exact value matters.
The wiring becomes very small:
- Connect one leg of the button to the input pin, for example pin 2.
- Connect the opposite leg of the button to GND.
- Add no resistor.
When the button is open, no current flows through the resistor. The pin sits at the supply voltage and reads HIGH. When the button closes, the pin is joined directly to GND. A small current flows through the resistor to GND, and the pin reads LOW. Because the resistor is large, that current is tiny: about 0.1 to 0.25 mA at 5 V.
Inverted logic
With a pull-up, the electrical level and the meaning are opposite:
| Button state | Pin voltage | `digitalRead()` result |
|---|---|---|
| Released | Supply (high) | `HIGH` |
| Pressed | GND (low) | `LOW` |
This is called inverted logic, or active-low: the "active" state (pressed) is the low level. Beginners often write if (digitalRead(pin) == HIGH) expecting it to mean "pressed". With INPUT_PULLUP that test is true when the button is released.
Here is a complete sketch that lights the built-in LED while the button is held:
const uint8_t BUTTON_PIN = 2;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
bool pressed = (digitalRead(BUTTON_PIN) == LOW);
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
}The variable pressed is where the inversion gets handled. Everything after that line can think in terms of "pressed" and "released", not voltage levels.
Keep the inversion in one place
Inverted logic spreads confusion if you scatter == LOW through a sketch. Two habits help.
First, name the levels:
const uint8_t BUTTON_PIN = 2;
const int PRESSED = LOW;
const int RELEASED = HIGH;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if (digitalRead(BUTTON_PIN) == PRESSED) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
}Second, if you later switch to a pull-down wiring, you change two constants instead of hunting through the code. The later chapters in this tutorial use the same idea: the debounce code compares readings to each other, and only the final "was it a press?" test cares which level is active.
Setting the mode before reading
On AVR boards, the pull-up is controlled by the same register bit that holds the output level. This has a consequence you can see in older code:
// Older style, still found in many sketches:
pinMode(2, INPUT);
digitalWrite(2, HIGH); // enables the pull-up on an input pinThis works on AVR, but it is unclear and does not work the same on every board. Use INPUT_PULLUP. It states the intent and is portable across the Arduino core.
Another detail: switching a pin from OUTPUT to INPUT_PULLUP mid-sketch is fine, but the pull-up needs a short moment to charge the pin and any wire attached to it. If you read the pin immediately after pinMode() with a long wire or a capacitive sensor attached, the first reading may be wrong. For an ordinary button on a short wire this is not an issue.
Choosing between internal and external pull-ups
The internal pull-up is the right default. Consider an external resistor when:
- The wire is long. A long cable picks up noise, and a weak 20 kΩ to 50 kΩ pull-up gives it little to resist with. A stronger external pull-up, such as 4.7 kΩ, holds the line more firmly.
- The board has no internal pull-up on that pin. Some pins on some boards lack one. For example, on ESP8266 boards GPIO16 has no pull-up, and on ESP32 the input-only pins GPIO34 to GPIO39 have none. Check your board's documentation.
- The input is shared with other circuitry that expects a specific voltage level or resistance.
A stronger pull-up is not free. When the button closes, more current flows through the resistor. A 4.7 kΩ resistor at 5 V draws about 1 mA per pressed button, which still matters little on mains-powered projects but adds up on battery-powered ones.
What the button looks like when you press it
With the wiring correct, a press should produce a clean change from HIGH to LOW. In practice you may see several changes. A mechanical contact does not close once. It touches, springs apart, and touches again for a few milliseconds. INPUT_PULLUP does not fix this. It only gives the pin a defined resting level.
You can see the problem by counting transitions:
const uint8_t BUTTON_PIN = 2;
int lastReading = HIGH;
unsigned int changes = 0;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastReading) {
lastReading = reading;
changes++;
Serial.print("Change #");
Serial.print(changes);
Serial.print(" to ");
Serial.println(reading == LOW ? "LOW (pressed)" : "HIGH (released)");
}
}Open the Serial Monitor at 9600 baud and press the button once. A clean button prints two lines: one for the press and one for the release. Many buttons print three, four or more lines for a single press. The extra lines are contact bounce, the subject of the next chapter.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Reads `HIGH` and `LOW` randomly with the button untouched | Floating pin: no pull resistor, or `pinMode(pin, INPUT)` only | Use `INPUT_PULLUP` and wire the button to GND |
| Always reads `HIGH`, even when pressed | Button wired to the supply voltage instead of GND, or the button legs are on the same internal contact | Wire to GND. Rotate a four-pin button by 90 degrees |
| Always reads `LOW` | Button permanently shorted, or pin wired to GND without the button | Check the wiring with a multimeter in continuity mode |
| The LED turns on when the button is released | Inverted logic not accounted for | Test for `LOW` to mean pressed |
| The pin reads `HIGH` after `INPUT_PULLUP` on pin 13 | The LED circuit on pin 13 loads the pin | Use a different pin |
| One press prints several lines | Contact bounce | Debounce in software, covered in later chapters |
Summary of the model
Think of the pin as a voltage you read, not a switch you ask about. INPUT_PULLUP guarantees a HIGH when nothing else drives the pin. The button overrides that by connecting to GND. So the reading is HIGH at rest and LOW when pressed. Your code translates that once into "pressed" and "released", and everything else works with those words.
Exercise
A button is wired between pin 4 and GND, and the sketch calls pinMode(4, INPUT_PULLUP). What does digitalRead(4) return while the button is released, and while it is held down? Then explain what you would see on a floating pin if you used pinMode(4, INPUT) instead.
Show answer
It returns HIGH while the button is released, because the internal pull-up holds the pin at the supply voltage, and LOW while the button is held, because the button connects the pin to GND. With pinMode(4, INPUT) and no external resistor, the released button leaves the pin floating. The reading would change unpredictably between HIGH and LOW, influenced by noise and nearby objects, so the sketch would appear to detect presses that never happened.
Exercise
Rewrite the test that decides whether the button on pin 2 is pressed so that a reader of the code does not need to know about inverted logic. Use a named constant.
Show answer
Define const int PRESSED = LOW; and write if (digitalRead(2) == PRESSED). Alternatively, store the result in a variable: bool pressed = (digitalRead(2) == LOW); and use pressed in later conditions. In both cases the inversion appears in exactly one place.