LED brightness control with PWM in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When controlling LED brightness using PWM, it's important to understand how the program's running time changes as we adjust brightness levels.
We want to know how the time the program takes grows when we change the brightness steps.
Analyze the time complexity of the following code snippet.
const int ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness);
delay(10);
}
}
This code gradually increases LED brightness from off (0) to fully bright (255) using PWM, pausing briefly at each step.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from 0 to 255 to set brightness levels.
- How many times: 256 times each time the loop runs.
As the number of brightness steps increases, the program runs the loop more times, so the total work grows directly with the number of steps.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop cycles |
| 100 | 100 loop cycles |
| 1000 | 1000 loop cycles |
Pattern observation: The total operations increase in a straight line as input size grows.
Time Complexity: O(n)
This means the time to complete the brightness changes grows directly with the number of brightness levels you set.
[X] Wrong: "The delay inside the loop doesn't affect the time complexity because it's just a fixed pause."
[OK] Correct: The delay runs every loop cycle, so it adds up and grows with the number of brightness steps, affecting total time linearly.
Understanding how loops and delays affect program time helps you explain how your code scales, a useful skill in many programming tasks.
"What if we changed the for-loop to increase brightness in steps of 5 instead of 1? How would the time complexity change?"
Practice
analogWrite(pin, 128) do to an LED connected to the specified pin?Solution
Step 1: Understand PWM value range
TheanalogWritefunction accepts values from 0 (off) to 255 (full brightness).Step 2: Interpret the value 128
128 is about half of 255, so the LED brightness will be about half of maximum.Final Answer:
Sets the LED brightness to about half of its maximum brightness. -> Option AQuick Check:
PWM value 128 = half brightness [OK]
- Thinking 128 turns LED off
- Confusing analogWrite with digitalWrite
- Assuming 128 means blinking
Solution
Step 1: Recall analogWrite syntax
The correct syntax isanalogWrite(pin, value)where pin is the pin number and value is 0-255.Step 2: Check each option
analogWrite(9, 255); usesanalogWrite(9, 255);which is correct. Others have wrong function names or argument order.Final Answer:
analogWrite(9, 255); -> Option CQuick Check:
Correct function and argument order = analogWrite(9, 255); [OK]
- Swapping pin and value arguments
- Using digitalWrite instead of analogWrite
- Using analogRead instead of analogWrite
for (int brightness = 0; brightness <= 255; brightness += 51) {
analogWrite(6, brightness);
delay(100);
}Solution
Step 1: Analyze the for loop increments
The loop variable brightness starts at 0 and increases by 51 until it reaches 255, so values are 0, 51, 102, 153, 204, 255 (6 values, 5 steps).Step 2: Understand analogWrite effect
Each loop sets LED brightness to the current value, increasing brightness in steps with 100ms delay.Final Answer:
The LED brightness will increase in 5 steps from off to full brightness. -> Option DQuick Check:
Brightness steps with delay = gradual brightness increase [OK]
- Counting 6 steps instead of 5 increments
- Thinking LED blinks on/off instead of fading
- Assuming syntax error due to loop
for (int i = 0; i < 256; i++) {
analogWrite(10, i);
delay(50);
}
analogWrite(10, 256);Solution
Step 1: Check analogWrite value limits
analogWrite accepts values from 0 to 255. Using 256 is out of range and invalid.Step 2: Verify other code parts
The for loop correctly uses i from 0 to 255. Pin 10 supports PWM on most Arduino boards. delay() is allowed inside loops.Final Answer:
The value 256 is invalid for analogWrite; max is 255. -> Option BQuick Check:
analogWrite max value = 255, 256 causes error [OK]
- Using values above 255 for analogWrite
- Thinking delay() is not allowed in loops
- Assuming pin 10 can't do PWM
Solution
Step 1: Understand breathing LED effect
A breathing effect smoothly increases brightness from 0 to max, then back down to 0 repeatedly.Step 2: Analyze each option
for (int b = 0; b <= 255; b++) { analogWrite(3, b); delay(10); } for (int b = 255; b >= 0; b--) { analogWrite(3, b); delay(10); } uses two loops: one increasing PWM from 0 to 255, then decreasing back to 0 with small delays for smoothness. analogWrite(3, 255); delay(1000); analogWrite(3, 0); delay(1000); just turns LED fully on and off abruptly. for (int b = 0; b < 256; b += 50) { analogWrite(3, b); delay(100); } increases brightness in large steps, not smooth. digitalWrite(3, HIGH); delay(500); digitalWrite(3, LOW); delay(500); uses digitalWrite, which only turns LED fully on or off.Final Answer:
for (int b = 0; b <= 255; b++) { analogWrite(3, b); delay(10); } for (int b = 255; b >= 0; b--) { analogWrite(3, b); delay(10); } -> Option AQuick Check:
Smooth increase and decrease PWM = breathing LED [OK]
- Using digitalWrite instead of analogWrite for brightness
- Skipping the decreasing brightness loop
- Using large PWM steps causing jerky effect
