Button reading with pull-up resistor in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When reading a button with a pull-up resistor on Arduino, the program checks the button state repeatedly.
We want to know how the time to check the button grows as the program runs longer or with more checks.
Analyze the time complexity of the following code snippet.
const int buttonPin = 2;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
delay(100);
}
This code sets up a button with a pull-up resistor and repeatedly reads its state every 100 milliseconds, printing the result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
loop()function runs forever, reading the button state once each time. - How many times: It repeats indefinitely, once every 100 milliseconds.
Each loop cycle does a fixed amount of work: one button read and one print.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (loops) | 10 reads and prints |
| 100 (loops) | 100 reads and prints |
| 1000 (loops) | 1000 reads and prints |
Pattern observation: The number of operations grows directly with the number of loops. More loops mean more reads and prints.
Time Complexity: O(n)
This means the time to run the program grows linearly with how many times the button is read.
[X] Wrong: "Reading the button once takes longer if the program runs longer."
[OK] Correct: Each button read takes the same short time, no matter how many times it has run before. The total time grows because you do more reads, not because each read gets slower.
Understanding how repeated actions add up over time helps you explain how programs handle inputs and timing, a useful skill in many projects and interviews.
"What if we added another button and read both in each loop? How would the time complexity change?"
Practice
pinMode(pin, INPUT_PULLUP) do in Arduino when reading a button?Solution
Step 1: Understand pinMode with INPUT_PULLUP
UsingINPUT_PULLUPactivates the internal pull-up resistor on the pin, so it reads HIGH by default.Step 2: Effect on button reading
When the button is pressed, it connects the pin to ground, making the reading LOW. When not pressed, the pull-up resistor keeps it HIGH.Final Answer:
Enables the internal pull-up resistor to keep the pin HIGH when button is not pressed -> Option AQuick Check:
INPUT_PULLUP means pin reads HIGH unless grounded [OK]
- Thinking INPUT_PULLUP sets pin as output
- Assuming pin reads LOW when button is not pressed
- Confusing pull-up with pull-down resistor
Solution
Step 1: Recall correct pinMode usage
The correct way to enable internal pull-up resistor ispinMode(pin, INPUT_PULLUP);.Step 2: Check each option
pinMode(7, OUTPUT_PULLUP); uses OUTPUT_PULLUP which does not exist. pinMode(7, INPUT_PULLDOWN); uses INPUT_PULLDOWN which Arduino does not support internally. pinMode(7, INPUT); digitalWrite(7, LOW); sets pin as INPUT but digitalWrite LOW disables pull-up (equivalent to plain INPUT, floating pin), does not enable internal pull-up resistor.Final Answer:
pinMode(7, INPUT_PULLUP); -> Option CQuick Check:
Use INPUT_PULLUP to enable pull-up resistor [OK]
- Using OUTPUT_PULLUP which is invalid
- Trying INPUT_PULLDOWN which Arduino lacks
- digitalWrite(7, LOW) after INPUT (floating input, no pull-up)
void setup() {
pinMode(4, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(4);
Serial.println(state);
delay(500);
}Solution
Step 1: Understand INPUT_PULLUP behavior
With INPUT_PULLUP, the pin reads HIGH (1) when button is not pressed and LOW (0) when pressed because button connects pin to ground.Step 2: Analyze Serial output
The code prints the pin state every 500ms. When pressed, it prints 0; when released, it prints 1.Final Answer:
Prints 0 when button pressed, 1 when released -> Option DQuick Check:
Pressed = LOW (0), Released = HIGH (1) [OK]
- Assuming pressed reads HIGH (1)
- Confusing pull-up with pull-down logic
- Ignoring that button grounds the pin when pressed
void setup() {
pinMode(2, INPUT);
Serial.begin(9600);
}
void loop() {
int val = digitalRead(2);
Serial.println(val);
delay(200);
}Solution
Step 1: Check pinMode configuration
The code usespinMode(2, INPUT);which does not enable the internal pull-up resistor, so the pin may float and give unreliable readings.Step 2: Correct usage for button with pull-up
To use the internal pull-up resistor, the pinMode should beINPUT_PULLUP. This prevents floating and ensures stable readings.Final Answer:
Missing INPUT_PULLUP mode to enable pull-up resistor -> Option AQuick Check:
Use INPUT_PULLUP to avoid floating input [OK]
- Using INPUT without pull-up resistor
- Moving Serial.begin to loop unnecessarily
- Thinking delay affects button reading correctness
Solution
Step 1: Set pin modes correctly
Pin 8 must be input with internal pull-up resistor:INPUT_PULLUP. Pin 13 is output for LED.Step 2: Understand button logic with pull-up
Button press connects pin 8 to ground, sodigitalRead(8)returns LOW when pressed.Step 3: Write correct if condition
Turn LED on when button is pressed (pin reads LOW), so condition isif(digitalRead(8) == LOW).Final Answer:
pinMode(8, INPUT_PULLUP); pinMode(13, OUTPUT); if(digitalRead(8) == LOW) digitalWrite(13, HIGH); else digitalWrite(13, LOW); -> Option BQuick Check:
Pressed = LOW, LED ON when LOW [OK]
- Checking for HIGH instead of LOW on button press
- Setting button pin as OUTPUT
- Not enabling INPUT_PULLUP resistor
