Sub topic 8
Reading Inverted Logic: Pressed Is LOW
At this point, you should know the basic Arduino sketch structure, voltage and resistance, breadboard wiring, and how digitalWrite() controls an LED. This chapter assumes that background.
A digital input reports one of two logic levels:
HIGH: the pin voltage is near the board's supply voltage.LOW: the pin voltage is near 0 V, or ground.
The meaning of HIGH and LOW depends on how the circuit is wired. A button press does not inherently mean HIGH. With a pull-up resistor, pressing the button connects the input to ground, so the press produces LOW.
Active-low input
A signal is called active-low when its important or active state is represented by LOW. The name often appears with an overline or a suffix such as _N, but in Arduino code you usually see a comparison with LOW.
With the internal pull-up enabled, the input behaves like this:
| Button condition | Pin connection | Reading |
|---|---|---|
| Released | Pulled toward 5 V internally | `HIGH` |
| Pressed | Connected to GND through the button | `LOW` |
The internal pull-up resistor keeps the pin at a defined voltage when the button is released. When the button is pressed, the button provides a low-resistance path to ground. The pin voltage falls, overriding the pull-up.
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}The LED is on while the button is held. Notice that the condition is buttonState == LOW. Reversing this comparison would make the LED turn on while the button is released.
Reading active-low signals clearly
You can make the meaning more obvious by naming the condition instead of repeating the electrical detail throughout the program.
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
bool pressed = digitalRead(buttonPin) == LOW;
digitalWrite(ledPin, pressed ? HIGH : LOW);
}The expression digitalRead(buttonPin) == LOW produces a Boolean value. A Boolean value is either true or false. Here, pressed is true only when the button is physically pressed.
This naming also helps when you later add debounce or detect a transition. You can reason about pressed and released rather than mentally translating every condition from voltage level to button action.
Active-low logic in larger circuits
Active-low logic is common because a pull-up gives the input a safe default state and a switch can simply connect the signal to ground. Many modules also use active-low outputs, including some reset pins, chip-select pins, and status signals.
When documenting or naming such a signal, state its active level near the code. For example:
const int buttonPin = 2;
const bool BUTTON_PRESSED_LEVEL = LOW;This avoids treating HIGH as universally meaning “on” or “pressed.” The correct interpretation comes from the circuit's electrical design.
Exercise
A button uses INPUT_PULLUP. What should the program test to detect a press, and why?
Show answer
It should test digitalRead(buttonPin) == LOW, because pressing the button connects the input to ground and pulls its voltage below the logic-high level.