digitalRead() for input reading in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using digitalRead() in Arduino, it's important to know how the time to read inputs changes as we read more pins.
We want to understand how the number of input reads affects the total time the program takes.
Analyze the time complexity of the following code snippet.
for (int pin = 2; pin <= 10; pin++) {
int state = digitalRead(pin);
// do something with state
}
This code reads the state of pins 2 through 10 one by one and processes each reading.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: digitalRead() called inside a for loop.
- How many times: Once for each pin from 2 to 10, so 9 times.
Each additional pin adds one more digitalRead() call, so the total work grows directly with the number of pins.
| Input Size (n) | Approx. Operations |
|---|---|
| 9 | 9 digitalRead() calls |
| 100 | 100 digitalRead() calls |
| 1000 | 1000 digitalRead() calls |
Pattern observation: The number of operations grows evenly as the number of pins increases.
Time Complexity: O(n)
This means the time to read inputs grows in direct proportion to how many pins you read.
[X] Wrong: "digitalRead() takes the same total time no matter how many pins I read."
[OK] Correct: Each call to digitalRead() takes time, so reading more pins adds more calls and more time.
Understanding how input reading scales helps you write efficient Arduino code and shows you can think about how programs grow with input size.
"What if we read only every other pin instead of all pins? How would the time complexity change?"
Practice
digitalRead() function do in Arduino?Solution
Step 1: Understand the purpose of digitalRead()
The function digitalRead() checks the voltage level on a digital pin and returns HIGH or LOW.Step 2: Differentiate from other functions
Unlike digitalWrite() which sets pin output, digitalRead() only reads input state.Final Answer:
Reads the voltage level (HIGH or LOW) from a digital input pin -> Option BQuick Check:
digitalRead() reads pin state [OK]
- Confusing digitalRead() with digitalWrite()
- Thinking digitalRead() sets pin output
- Mixing digitalRead() with analogRead()
Solution
Step 1: Recall the correct function syntax
The correct function call is digitalRead(pinNumber); where pinNumber is the pin to read.Step 2: Check each option
Only digitalRead(7); matches the correct syntax digitalRead(7); others are invalid function calls or assignments.Final Answer:
digitalRead(7); -> Option CQuick Check:
Correct function call [OK]
- Using assignment instead of function call
- Wrong function name like readDigital()
- Adding extra words like digitalReadPin()
void setup() {
pinMode(2, INPUT);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(2);
Serial.println(buttonState);
delay(500);
}Solution
Step 1: Analyze pinMode and digitalRead usage
Pin 2 is set as INPUT, so digitalRead(2) reads the button state correctly.Step 2: Understand button press state
When the button is pressed, the pin reads HIGH which is 1, so Serial.println prints 1 repeatedly.Final Answer:
1 -> Option AQuick Check:
Pressed button = HIGH = 1 [OK]
- Assuming pressed button reads 0 instead of 1
- Forgetting to set pinMode to INPUT
- Confusing analogRead with digitalRead
void setup() {
Serial.begin(9600);
}
void loop() {
int state = digitalRead(4);
Serial.println(state);
delay(1000);
}Solution
Step 1: Check pin configuration
The code does not set pin 4 as INPUT using pinMode(), which is required before reading.Step 2: Verify other parts
digitalRead() can be used in loop(), Serial.begin() must be in setup(), and delay() is allowed.Final Answer:
Missing pinMode(4, INPUT) in setup() -> Option DQuick Check:
Always set pinMode before digitalRead() [OK]
- Forgetting pinMode() setup
- Moving Serial.begin() to loop() incorrectly
- Thinking delay() breaks digitalRead()
pinMode(3, INPUT);
if (digitalRead(3) == HIGH) {
Serial.println("Pressed");
} else {
Serial.println("Not Pressed");
}
B)
pinMode(3, INPUT);
if (digitalRead(3) == LOW) {
Serial.println("Pressed");
} else {
Serial.println("Not Pressed");
}
C)
pinMode(3, OUTPUT);
if (digitalRead(3) == LOW) {
Serial.println("Pressed");
} else {
Serial.println("Not Pressed");
}
D)
pinMode(3, INPUT_PULLUP);
if (digitalRead(3) == HIGH) {
Serial.println("Pressed");
} else {
Serial.println("Not Pressed");
}Solution
Step 1: Understand active LOW switch wiring
The switch connects pin 3 to GND when pressed, so digitalRead(3) returns LOW when pressed.Step 2: Check pinMode and condition
pinMode must be INPUT (not OUTPUT). The condition to detect press is digitalRead(3) == LOW.Final Answer:
Code snippet B -> Option AQuick Check:
Active LOW switch reads LOW when pressed [OK]
- Using OUTPUT mode instead of INPUT
- Checking for HIGH instead of LOW for active LOW switch
- Using INPUT_PULLUP but checking wrong logic
