What is input_pullup in Arduino: Explanation and Example
INPUT_PULLUP is a mode for Arduino pins that activates an internal pull-up resistor. It makes the pin read HIGH by default and LOW when connected to ground, helping to avoid floating inputs without needing an external resistor.How It Works
Imagine a door that is usually closed but can be opened by pressing a button. The internal pull-up resistor in Arduino acts like a spring that keeps the door closed (pin reading HIGH) unless you press the button to open it (connect the pin to ground, making it LOW).
Without this pull-up resistor, the pin would be like a door left open, letting in random noise and causing the input to 'float' between HIGH and LOW unpredictably. Using INPUT_PULLUP connects a small resistor inside the Arduino to the pin, pulling it up to a HIGH state by default.
This means you don't need to add an external resistor to keep the input stable. When you connect the pin to ground (for example, by pressing a button), the pin reads LOW, signaling an input event.
Example
This example shows how to use INPUT_PULLUP to read a button press without an external resistor. The built-in LED on pin 13 lights up when the button is pressed.
const int buttonPin = 2; const int ledPin = 13; void setup() { pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor pinMode(ledPin, OUTPUT); } void loop() { int buttonState = digitalRead(buttonPin); if (buttonState == LOW) { // Button pressed (connected to ground) digitalWrite(ledPin, HIGH); // Turn LED on } else { digitalWrite(ledPin, LOW); // Turn LED off } }
When to Use
Use INPUT_PULLUP when you want to read a button or switch input without adding an external resistor. It simplifies wiring and reduces parts.
This mode is ideal for simple user inputs like buttons, switches, or sensors that connect to ground when activated.
It helps prevent unreliable readings caused by floating pins, which can happen if the input pin is left disconnected or not properly biased.
Key Points
INPUT_PULLUPactivates Arduino's internal pull-up resistor on a pin.- The pin reads HIGH by default and LOW when connected to ground.
- No external resistor is needed for stable button or switch inputs.
- Prevents floating input values and unreliable readings.
- Commonly used for simple user input devices.
Key Takeaways
INPUT_PULLUP enables an internal resistor that keeps input pins HIGH by default.