Reading digital input pin state in Embedded C - Time & Space Complexity
When reading a digital input pin, we want to know how the time to get the pin state changes as we do it more times.
We ask: How does the time grow if we read the pin many times?
Analyze the time complexity of the following code snippet.
// Read digital input pin state multiple times
int readPinState(int pin, int times) {
int state = 0;
for (int i = 0; i < times; i++) {
state = digitalRead(pin);
}
return state;
}
This code reads the state of a digital input pin repeatedly for a given number of times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling
digitalRead(pin)inside a loop. - How many times: Exactly
timestimes, controlled by the loop.
Each time we increase the number of reads, the total work grows directly with it.
| Input Size (times) | Approx. Operations |
|---|---|
| 10 | 10 calls to digitalRead |
| 100 | 100 calls to digitalRead |
| 1000 | 1000 calls to digitalRead |
Pattern observation: The number of operations grows in a straight line as the input size increases.
Time Complexity: O(n)
This means the time to read the pin grows directly in proportion to how many times we read it.
[X] Wrong: "Reading the pin multiple times takes the same time as reading it once."
[OK] Correct: Each read is a separate action, so doing it more times adds more work and takes more time.
Understanding how repeated hardware reads affect time helps you reason about performance in embedded systems clearly and confidently.
"What if we added a delay inside the loop after each read? How would the time complexity change?"
Practice
digitalRead(pin) do in embedded C programming?Solution
Step 1: Understand the purpose of digitalRead
The function digitalRead(pin) is used to check the current electrical state of a digital input pin, which can be either HIGH (on) or LOW (off).Step 2: Differentiate from other pin functions
Unlike functions that set pin modes or write signals, digitalRead only reads the pin's state without changing it.Final Answer:
Reads the current state (HIGH or LOW) of a digital input pin. -> Option CQuick Check:
digitalRead reads pin state = D [OK]
- Confusing digitalRead with pinMode or digitalWrite
- Thinking digitalRead sets pin mode
- Assuming digitalRead writes output signals
Solution
Step 1: Identify the function to set pin mode
pinMode(pin, mode) is used to configure a pin as INPUT or OUTPUT.Step 2: Choose the correct mode for reading input
To read a digital input pin, it must be set as INPUT using pinMode(pin, INPUT).Final Answer:
pinMode(pin, INPUT); -> Option BQuick Check:
Set pin as INPUT before reading = A [OK]
- Setting pin as OUTPUT before reading input
- Calling digitalRead before setting pin mode
- Using digitalWrite to set pin mode
pinMode(7, INPUT);
int buttonState = digitalRead(7);
if (buttonState == HIGH) {
printf("Button is pressed\n");
} else {
printf("Button is not pressed\n");
}Solution
Step 1: Analyze pin mode and reading
The pin 7 is set as INPUT, so digitalRead(7) correctly reads the pin state.Step 2: Check condition for pressed button
If the button is pressed, the pin reads HIGH, so the if condition is true and prints "Button is pressed".Final Answer:
Button is pressed -> Option AQuick Check:
Pressed button = HIGH = prints pressed [OK]
- Forgetting to set pinMode before reading
- Confusing HIGH and LOW states
- Assuming digitalRead returns boolean true/false
int pin = 5;
int state;
state = digitalRead();
if(state == HIGH) {
// do something
}Solution
Step 1: Check digitalRead usage
digitalRead requires the pin number as an argument, but here it is called without any argument.Step 2: Verify other parts of code
Pin mode setting is missing but not the main error asked; the if condition syntax is correct.Final Answer:
Missing pin number argument in digitalRead function. -> Option DQuick Check:
digitalRead needs pin argument = B [OK]
- Calling digitalRead without pin argument
- Setting pin as OUTPUT before reading input
- Misunderstanding return type of digitalRead
Solution
Step 1: Set correct pin modes
Pin 3 is input for the switch, so pinMode(3, INPUT). Pin 13 is output for the LED, so pinMode(13, OUTPUT).Step 2: Read switch and control LED
If digitalRead(3) is HIGH (switch ON), turn LED on with digitalWrite(13, HIGH), else turn it off with digitalWrite(13, LOW).Final Answer:
pinMode(3, INPUT); pinMode(13, OUTPUT); if (digitalRead(3) == HIGH) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); } -> Option AQuick Check:
Input pin read controls output pin write = A [OK]
- Setting input pin as OUTPUT or vice versa
- Reading wrong pin for switch state
- Not controlling LED pin properly
Ready to prove it?
Pass the test and this pattern moves unknown
