Sub topic 8
Reading inverted logic: pressed is LOW
This chapter assumes you can write a basic sketch with setup() and loop(), wire a breadboard, and drive an LED with digitalWrite. You should also know why a floating input is a problem and how INPUT_PULLUP fixes it. Here the focus is on what changes in your code once a press reads as LOW.
What active-low means
A signal is active-high when the "on" or "asserted" condition is the HIGH level. It is active-low when the asserted condition is the LOW level.
With INPUT_PULLUP, the internal resistor (roughly 20 kΩ to 50 kΩ on an ATmega328P) connects the pin to 5V. The button connects the pin to GND. The pin therefore sees these levels:
| Button state | Path the pin sees | `digitalRead` result |
|---|---|---|
| Released | Pin pulled to 5V through the internal resistor, nothing pulling it down | `HIGH` |
| Pressed | Pin connected directly to GND, which overpowers the weak pull-up | `LOW` |
The button does not change the meaning of "pressed". It only changes which voltage the pin sees at that moment. The inversion is a property of the wiring, not a bug. The same inversion exists with an external pull-up resistor.
An external pull-down circuit is the opposite case. The pin rests at GND through the resistor, and the button connects it to 5V, so a press reads HIGH. The first code example in this tutorial uses that wiring.
The naive version and its confusion
Here is a sketch that reports the state of a button wired between pin 2 and GND, using the internal pull-up.
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);
}The comparison level == LOW is correct, but it reads awkwardly. A reader who sees LOW has to remember that it means "pressed". The same confusion appears when you write if (digitalRead(buttonPin)), which is true when the button is released. Beginners often write that and conclude that the button "works backwards".
Naming the inversion away
The most useful habit is to convert the raw level into a variable whose name says what it means. Do the conversion in exactly one place, and use the named value everywhere else.
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
bool isPressed = (digitalRead(buttonPin) == LOW);
digitalWrite(ledPin, isPressed ? HIGH : LOW);
}The LED output is active-high, because HIGH turns the LED on. The button input is active-low. The line isPressed = (digitalRead(buttonPin) == LOW) is the single place where the two conventions meet. After that line, the rest of the code reasons in terms of "pressed" and "not pressed".
You can also give the levels names with constants:
const int PRESSED = LOW;
const int RELEASED = HIGH;This works, but it ties the names to one wiring choice. If you later move to a pull-down circuit, you must change these two lines, and you must remember to. A function that returns a bool hides the wiring more cleanly:
const int buttonPin = 2;
bool buttonIsPressed() {
return digitalRead(buttonPin) == LOW;
}
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
if (buttonIsPressed()) {
Serial.println("pressed");
}
delay(200);
}If the wiring changes, only the body of buttonIsPressed() changes. Callers stay the same.
Where the inversion shows up in other code
Active-low logic affects more than a single if. Watch for it in these places.
Initial values for state variables
Any variable that stores the previous reading must start at the released level. For an INPUT_PULLUP button, that is HIGH. In the debounce example from this tutorial, you can see this in the declarations:
int lastReading = HIGH;
int stableState = HIGH;If you initialize these to LOW by habit, the sketch believes the button was pressed at power-up. On the first loop, it sees a change from LOW to HIGH and treats it as a release, or, depending on the logic, it fires a phantom event.
Detecting the press edge
A press is the transition from released to pressed. With INPUT_PULLUP, that is a HIGH to LOW transition, also called a falling edge. A release is a LOW to HIGH transition, or a rising edge. With a pull-down circuit, the edges swap names.
| Wiring | Press produces | Release produces |
|---|---|---|
| `INPUT_PULLUP` (button to GND) | Falling edge, `HIGH` to `LOW` | Rising edge, `LOW` to `HIGH` |
| External pull-down (button to 5V) | Rising edge, `LOW` to `HIGH` | Falling edge, `HIGH` to `LOW` |
This is why the debounce example toggles the LED when stableState == LOW. That check is true only on the press edge, because it runs just after stableState changes.
Counting presses
If you count how many times a button is pressed, count transitions, not levels. Counting the loop passes in which the pin reads LOW gives a number that depends on how long the user holds the button and on how fast loop() runs. The chapter on edge detection covers the transition approach in full.
Common mistakes
- Testing the wrong level. Writing
if (digitalRead(buttonPin) == HIGH)with anINPUT_PULLUPbutton runs the code while the button is released. The LED then appears to turn off when you press. Fix it by checking forLOW, or by using a helper such asbuttonIsPressed(). - Mixing
INPUTwith a button-to-GND wire. UsingpinMode(buttonPin, INPUT)and wiring the button to GND, with no external resistor, leaves the pin floating while the button is released. Readings are random. The button must be paired withINPUT_PULLUP, or with an external pull-up. - Mixing
INPUT_PULLUPwith a button-to-5V wire. The pull-up holds the pinHIGHall the time. Pressing the button connects 5V to a pin that is already at 5V, so nothing changes. The reading is stuck atHIGH. - Forgetting that the reading is inverted when reusing code. A sketch copied from a pull-down tutorial often compares to
HIGH. When you adapt it toINPUT_PULLUP, search the sketch for every comparison againstHIGHandLOWon that pin. - Applying the inversion twice. If a helper function already returns
truefor "pressed", do not compare its result toLOWagain.
A diagnostic habit
When a button seems to behave backwards, print the raw level before you interpret it.
const int buttonPin = 2;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
Serial.print("raw level: ");
Serial.println(digitalRead(buttonPin));
delay(200);
}digitalRead returns 1 for HIGH and 0 for LOW. With the button released you should see 1. While you hold it you should see 0. The three outcomes tell you what is wrong:
- Always
1: the button is not connecting the pin to GND, or it is wired to 5V instead of GND. - Always
0: the pin is shorted to GND, the button is wired across the wrong legs, or a pull-down is fighting the pull-up. - Random flipping while the button is released: the pin is floating. Check that
INPUT_PULLUPis set.
The inversion on the output side
The same idea applies to outputs. Some LED modules and relay boards are active-low: the pin must go LOW to turn them on. The Arduino's built-in LED on pin 13 is active-high. If you connect an LED between 5V and a pin through a resistor, the LED lights when the pin is LOW, because that creates a voltage drop across it.
The principle is the same as for inputs. Keep the physical level in one place and name the logical meaning.
const int relayPin = 7;
const int RELAY_ON = LOW;
const int RELAY_OFF = HIGH;
void setup() {
digitalWrite(relayPin, RELAY_OFF);
pinMode(relayPin, OUTPUT);
}
void loop() {
digitalWrite(relayPin, RELAY_ON);
delay(1000);
digitalWrite(relayPin, RELAY_OFF);
delay(1000);
}Writing the level before calling pinMode matters for active-low outputs. On AVR boards, writing HIGH to an input pin enables its pull-up. Doing the write first means the pin starts at the off level when it becomes an output, so the relay does not click for a moment at power-up.
Summary of the working model
- The pin reports voltage. Your code decides what the voltage means.
- With
INPUT_PULLUP, released isHIGHand pressed isLOW. - Convert once, in a variable or function named for the meaning, then reason in logical terms.
- Initialize "previous state" variables to the released level.
- Count and react to transitions, not to levels, when you care about presses.
Exercise
A button is wired between pin 4 and GND, and the sketch calls pinMode(4, INPUTPULLUP). A student writes `if (digitalRead(4) == HIGH) { digitalWrite(LEDBUILTIN, HIGH); }` and says the LED lights only when the button is not pressed. Explain why, and rewrite the condition so the LED lights while the button is held.
Show answer
With INPUTPULLUP, the pin reads HIGH while the button is released, because the internal pull-up holds it at 5V. Pressing the button ties the pin to GND, so it reads LOW. The student's condition is true when the button is released, so the LED lights at the wrong time. The fixed condition is `if (digitalRead(4) == LOW) { digitalWrite(LEDBUILTIN, HIGH); } else { digitalWrite(LEDBUILTIN, LOW); }. A cleaner form is bool isPressed = (digitalRead(4) == LOW); digitalWrite(LEDBUILTIN, isPressed ? HIGH : LOW);`.
Exercise
You print digitalRead(2) for a button on an INPUT_PULLUP pin and see 0 continuously, whether or not you press the button. Give two possible causes.
Show answer
The pin is permanently connected to GND, so the pull-up cannot lift it. Possible causes are a button wired with two legs on the same internal connection (so it is always closed), a jumper wire touching GND, or a button mounted rotated 90 degrees so its contacts are always joined. Another cause is a wire from the pin to GND left in the circuit from an earlier experiment.