analogWrite() and PWM output in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how long it takes for the Arduino to run code using analogWrite() for PWM output.
Specifically, how does the time change when we call analogWrite() multiple times?
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n; i++) {
analogWrite(9, i); // Set PWM on pin 9
}
This code sets PWM output on pin 9 repeatedly, increasing the value each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop running analogWrite() calls.
- How many times: The loop runs n times, calling analogWrite() once each time.
Each time we increase n, the number of analogWrite() calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 analogWrite() calls |
| 100 | 100 analogWrite() calls |
| 1000 | 1000 analogWrite() calls |
Pattern observation: The total work grows directly with n. Double n, double the calls.
Time Complexity: O(n)
This means the time to run the code grows in a straight line as you increase the number of analogWrite() calls.
[X] Wrong: "analogWrite() runs instantly, so the loop time does not depend on n."
[OK] Correct: Each analogWrite() call takes some time to set the PWM signal, so more calls mean more total time.
Knowing how repeated hardware calls like analogWrite() scale helps you write efficient Arduino code and explain your reasoning clearly.
"What if we replaced the for-loop with a nested loop calling analogWrite() multiple times per iteration? How would the time complexity change?"
Practice
analogWrite() function do on an Arduino board?Solution
Step 1: Understand the purpose of analogWrite()
TheanalogWrite()function does not output a true analog voltage but uses PWM (Pulse Width Modulation) to simulate varying voltage levels on digital pins.Step 2: Compare options with function behavior
It outputs a PWM signal to simulate an analog voltage on a digital pin. correctly describes this behavior. Options A, B, and D describe other functions or actions unrelated toanalogWrite().Final Answer:
It outputs a PWM signal to simulate an analog voltage on a digital pin. -> Option CQuick Check:
analogWrite() = PWM output [OK]
- Confusing analogWrite() with analogRead()
- Thinking analogWrite() outputs true analog voltage
- Assuming analogWrite() sets pin HIGH or LOW directly
analogWrite()?Solution
Step 1: Understand the value range for analogWrite()
TheanalogWrite()function accepts values from 0 to 255, where 0 is off and 255 is full brightness.Step 2: Calculate half brightness value
Half brightness is about half of 255, which is approximately 127. A uses 512 (out of range), B uses 0 (off), C uses 255 (full brightness), so D is correct.Final Answer:
analogWrite(9, 127); -> Option DQuick Check:
Half brightness ≈ 127 [OK]
- Using values above 255 (like 512)
- Confusing digitalWrite() with analogWrite()
- Using full brightness value instead of half
void setup() {
pinMode(6, OUTPUT);
}
void loop() {
analogWrite(6, 0);
delay(1000);
analogWrite(6, 255);
delay(1000);
}Solution
Step 1: Analyze analogWrite values and delays
The code sets pin 6 to 0 (off) for 1 second, then to 255 (full brightness) for 1 second, repeatedly.Step 2: Understand LED behavior
When the pin is 0, the LED is off; when 255, it is fully on. The delays cause the LED to stay in each state for 1 second, making it blink on and off every second.Final Answer:
The LED will blink on and off every second. -> Option AQuick Check:
0 and 255 with delays = blink [OK]
- Thinking analogWrite(0) dims LED instead of off
- Ignoring delay effects on LED timing
- Assuming LED flickers rapidly without delay
void setup() {
pinMode(10, OUTPUT);
}
void loop() {
for (int i = 0; i <= 255; i++) {
analogWrite(10, i);
delay(10);
}
for (int i = 255; i >= 0; i--) {
analogWrite(10, i);
delay(10);
}
}Solution
Step 1: Check pinMode setup
Pin 10 is correctly set as OUTPUT in setup().Step 2: Analyze the for loops and analogWrite usage
The loops increase and then decrease the PWM value from 0 to 255 and back, with delays to create a smooth fade effect. This is a common and correct pattern.Final Answer:
The code will work correctly and fade the LED in and out. -> Option AQuick Check:
For loops with analogWrite create fade [OK]
- Forgetting to set pinMode to OUTPUT
- Thinking analogWrite can't be used on pin 10
- Misunderstanding loop variable scope
Solution
Step 1: Convert percentage to PWM value
60% of the maximum PWM value 255 is 0.6 x 255 = 153.Step 2: Check the analogWrite parameter
analogWrite() requires an integer between 0 and 255. analogWrite(3, 153); // 60% of 255 is about 153 uses 153, which is correct. analogWrite(3, 60); // 60 is the percentage value uses 60 which is too low, C uses a decimal which is invalid, and D sets full power.Final Answer:
analogWrite(3, 153); // 60% of 255 is about 153 -> Option BQuick Check:
60% x 255 = 153 [OK]
- Passing percentage directly instead of scaled value
- Using decimal numbers instead of integers
- Always setting full power without scaling
