digitalWrite() for output control in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to run a program changes when using digitalWrite() to control outputs.
Specifically, how does the number of digitalWrite() calls affect the program's speed?
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
digitalWrite(LED_PIN, HIGH);
delay(1);
digitalWrite(LED_PIN, LOW);
delay(1);
}
This code turns an LED on and off n times with a small delay each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop running digitalWrite() twice per cycle.
- How many times: The loop runs n times, so digitalWrite() is called 2n times.
As n grows, the number of digitalWrite() calls grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 digitalWrite() calls |
| 100 | 200 digitalWrite() calls |
| 1000 | 2000 digitalWrite() calls |
Pattern observation: The number of operations grows in a straight line as n increases.
Time Complexity: O(n)
This means the time to run the code grows directly in proportion to how many times the loop runs.
[X] Wrong: "digitalWrite() runs instantly, so the loop time does not depend on n."
[OK] Correct: Each digitalWrite() takes some time, and since it runs inside the loop n times, total time grows with n.
Understanding how loops and function calls like digitalWrite() affect time helps you write efficient Arduino code and explain your reasoning clearly.
"What if we removed the delay() calls inside the loop? How would the time complexity change?"
Practice
digitalWrite() function do in Arduino programming?Solution
Step 1: Understand the purpose of digitalWrite()
The function digitalWrite() is used to control the voltage level on a digital pin, setting it either HIGH (on) or LOW (off). It does not read values (that's digitalRead()), nor does it set analog values or initialize serial communication.Final Answer:
It sets a digital pin to HIGH or LOW voltage. -> Option DQuick Check:
digitalWrite() controls pin voltage = D [OK]
- Confusing digitalWrite() with digitalRead()
- Thinking digitalWrite() reads pin values
- Mixing digitalWrite() with analogWrite()
digitalWrite()?Solution
Step 1: Recall correct constants and check options
digitalWrite() uses HIGH and LOW constants. To turn on, use HIGH. A uses ON (not valid), B uses LOW (turns off), D uses TRUE (not valid). Only C: digitalWrite(13, HIGH); is correct.Final Answer:
digitalWrite(13, HIGH); -> Option AQuick Check:
Use HIGH or LOW with digitalWrite() = C [OK]
- Using ON or TRUE instead of HIGH
- Using LOW instead of HIGH
- Forgetting to set pinMode to OUTPUT first
pinMode(8, OUTPUT); digitalWrite(8, LOW); digitalWrite(8, HIGH); digitalWrite(8, LOW);
Solution
Step 1: Trace the digitalWrite() calls
pinMode sets OUTPUT, then LOW, HIGH, LOW. Final LOW so pin off.Final Answer:
Pin 8 will be LOW (off). -> Option CQuick Check:
Last digitalWrite sets pin LOW = A [OK]
- Assuming pin toggles automatically
- Confusing initial and final pin states
- Thinking multiple digitalWrite calls cause errors
digitalWrite(12, HIGH); pinMode(12, OUTPUT);
Solution
Step 1: Check order of pinMode() and digitalWrite()
pinMode() must set OUTPUT before digitalWrite(), otherwise unexpected behavior. Here digitalWrite first.Final Answer:
pinMode() must be called before digitalWrite(). -> Option BQuick Check:
Set pinMode before digitalWrite = A [OK]
- Calling digitalWrite before pinMode
- Assuming pinMode defaults to OUTPUT
- Using invalid pin numbers
digitalWrite() inside the loop() function to achieve this?Solution
Step 1: Understand blinking and analyze options
Blink: HIGH, delay(1000), LOW, delay(1000). A: HIGH immediate LOW, no on time. C: pinMode in loop inefficient, no LOW. D: HIGH twice, always on. Only B correct.Final Answer:
digitalWrite(9, HIGH); delay(1000); digitalWrite(9, LOW); delay(1000); -> Option AQuick Check:
Turn ON, delay, turn OFF, delay = B [OK]
- Missing delay between ON and OFF
- Not turning LED OFF after ON
- Setting pinMode inside loop repeatedly
