Sub topic 2
Wiring with `INPUT_PULLUP` and Inverted Logic
This chapter assumes you know the basic Arduino sketch structure, setup() and loop(), and have used pinMode(), digitalRead(), and digitalWrite(). It also assumes you can identify the pins on your board and wire a pushbutton on a breadboard.
A digital input can read one of two logical states:
HIGH, usually close to the board's supply voltageLOW, usually close to 0 volts, or ground
A button connects an input pin to one of these voltage levels when pressed. The input must also have a known level when the button is not pressed. Otherwise, the pin can be electrically disconnected and pick up noise. This condition is called a floating input.
Using the internal pull-up resistor
A pull-up resistor connects an input to the positive supply through a high resistance. It gives the input a stable HIGH reading when the button is open. Pressing the button connects the input directly to ground, changing the reading to LOW.
Arduino boards provide an internal pull-up resistor. Enable it by passing INPUT_PULLUP to pinMode():
const uint8_t BUTTON_PIN = 2;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
int state = digitalRead(BUTTON_PIN);
if (state == LOW) {
Serial.println("Button is pressed");
} else {
Serial.println("Button is released");
}
delay(200);
}With this wiring, connect one side of the button to digital pin 2 and the opposite side to GND. Do not connect the button to 5 V for this arrangement.
| Button condition | Pin connection | `digitalRead()` result |
|---|---|---|
| Released | Internal pull-up holds the pin near supply voltage | `HIGH` |
| Pressed | Button connects the pin to ground | `LOW` |
This is called inverted logic because the electrical reading is the opposite of the event meaning. LOW means “pressed,” while HIGH means “released.” Name variables and conditions clearly so this inversion remains visible in your code.
Understanding breadboard button orientation
Many four-legged tactile buttons contain two pairs of internally connected legs. The two legs on one side are connected to each other, and the two legs on the opposite side form the other pair. Pressing the button connects the two sides.
Place a four-legged button across the center gap of the breadboard. This normally puts each connected pair on a different half of the board. If you place the button incorrectly, the pin may always read LOW or the button may have no effect.
Test the circuit before adding debounce logic:
- Disconnect power while changing the wiring.
- Connect one button side to the input pin.
- Connect the opposite button side to
GND. - Set the pin mode to
INPUT_PULLUP. - Print the reading or use the Serial Monitor.
- Confirm that an untouched button reads
HIGHand a pressed button readsLOW.
Handling a pressed state
A simple condition can respond while the button remains pressed:
if (digitalRead(BUTTON_PIN) == LOW) {
// The button is currently pressed.
}This condition runs on every pass through loop(). Later, you will compare the current reading with the previous reading so one physical press creates one event instead of repeated actions. Debouncing is also needed because the contacts can change state several times during one press.
Exercise
With pinMode(BUTTON_PIN, INPUT_PULLUP), what should the sketch read when the button is released, and why?
Show answer
It should read HIGH because the internal pull-up holds the input near the positive supply while the open button does not connect it to ground.