Writing HIGH and LOW to output pins in Embedded C - Time & Space Complexity
When writing HIGH or LOW to output pins, it's important to know how the time taken changes as we do more writes.
We want to see how the number of pin writes affects the total time the program takes.
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
digitalWrite(pin, HIGH);
digitalWrite(pin, LOW);
}
This code writes HIGH then LOW to the same output pin repeatedly n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Writing HIGH and LOW to the output pin.
- How many times: Each write happens inside a loop that runs n times, so 2 writes per loop, total 2*n writes.
Each time we increase n, the number of writes grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 writes |
| 100 | 200 writes |
| 1000 | 2000 writes |
Pattern observation: The total operations grow in a straight line as n increases.
Time Complexity: O(n)
This means the time taken grows directly in proportion to the number of writes we do.
[X] Wrong: "Writing HIGH and LOW inside the loop takes constant time no matter how many times we run it."
[OK] Correct: Each write takes some time, so doing it many times adds up and the total time grows with n.
Understanding how repeated pin writes affect time helps you write efficient embedded code and explain your reasoning clearly in interviews.
"What if we only wrote HIGH once before the loop and LOW once after? How would the time complexity change?"
Practice
Solution
Step 1: Understand pin configuration
Before writing HIGH or LOW, the pin must be set as an output to control voltage.Step 2: Identify the correct function
The function to set pin mode ispinMode(pin, OUTPUT), which configures the pin for output.Final Answer:
pinMode(pin, OUTPUT) -> Option CQuick Check:
Set pin as output = pinMode(pin, OUTPUT) [OK]
- Using digitalWrite to set pin mode
- Confusing HIGH/LOW with OUTPUT/INPUT
- Forgetting to set pin mode before writing
Solution
Step 1: Identify the function to write pin state
The functiondigitalWrite(pin, value)sets the pin voltage HIGH or LOW.Step 2: Use correct constants
UseHIGHto turn the pin ON. The correct syntax isdigitalWrite(5, HIGH);.Final Answer:
digitalWrite(5, HIGH); -> Option DQuick Check:
Turn pin ON = digitalWrite(pin, HIGH) [OK]
- Using ON instead of HIGH
- Using OUTPUT instead of HIGH
- Using numeric 1 instead of HIGH constant
pinMode(3, OUTPUT); digitalWrite(3, LOW); digitalWrite(3, HIGH);
Solution
Step 1: Set pin 3 as output
The code sets pin 3 as output withpinMode(3, OUTPUT);.Step 2: Write LOW then HIGH to pin 3
First, pin 3 is set LOW, then immediately set HIGH, so final state is HIGH.Final Answer:
Pin 3 will be HIGH -> Option BQuick Check:
Last digitalWrite sets pin state = HIGH [OK]
- Assuming pin stays LOW after writing HIGH
- Forgetting pinMode sets output mode
- Thinking pin state is undefined without delay
pinMode(7, OUTPUT); digitalWrite(7, HIGH) digitalWrite(7, LOW);
Solution
Step 1: Check syntax line by line
The second linedigitalWrite(7, HIGH)is missing a semicolon at the end.Step 2: Confirm other lines are correct
pinMode(7, OUTPUT);anddigitalWrite(7, LOW);are correct syntax.Final Answer:
Missing semicolon after digitalWrite(7, HIGH) -> Option AQuick Check:
Every statement must end with semicolon [OK]
- Forgetting semicolon after function calls
- Calling pinMode after digitalWrite
- Misusing HIGH/LOW constants
Solution
Step 1: Set pin 10 as output
UsepinMode(10, OUTPUT);to configure pin 10 for output.Step 2: Write HIGH, wait, then LOW, wait
UsedigitalWrite(10, HIGH);thendelay(1000);to wait 1 second, thendigitalWrite(10, LOW);and delay again.Final Answer:
pinMode(10, OUTPUT); digitalWrite(10, HIGH); delay(1000); digitalWrite(10, LOW); delay(1000); -> Option AQuick Check:
Set output, write states with delay between [OK]
- Forgetting to set pinMode as OUTPUT
- Missing delay between HIGH and LOW
- Setting pinMode to INPUT instead of OUTPUT
Ready to prove it?
Pass the test and this pattern moves unknown
