LED brightness control in Raspberry Pi - Time & Space Complexity
When controlling LED brightness on a Raspberry Pi, we often adjust values repeatedly in a loop.
We want to understand how the time it takes grows as we change the number of brightness steps.
Analyze the time complexity of the following code snippet.
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
LED_PIN = 18
GPIO.setup(LED_PIN, GPIO.OUT)
pwm = GPIO.PWM(LED_PIN, 1000)
pwm.start(0)
for brightness in range(0, 101):
pwm.ChangeDutyCycle(brightness)
time.sleep(0.01)
pwm.stop()
GPIO.cleanup()
This code gradually increases LED brightness from 0% to 100% in steps, pausing briefly at each step.
- Primary operation: The for-loop that changes brightness 101 times.
- How many times: Once for each brightness level from 0 to 100.
As the number of brightness steps increases, the loop runs more times, increasing total operations.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 brightness changes |
| 100 | 100 brightness changes |
| 1000 | 1000 brightness changes |
Pattern observation: The total work grows directly with the number of brightness steps.
Time Complexity: O(n)
This means the time to complete grows in a straight line as you increase brightness steps.
[X] Wrong: "Changing brightness a few more times won't affect the total time much."
[OK] Correct: Each brightness change takes time, so more steps add up and increase total time linearly.
Understanding how loops affect time helps you explain how your code scales, a key skill in many programming tasks.
"What if we changed the loop to increase brightness in steps of 5 instead of 1? How would the time complexity change?"
Practice
Solution
Step 1: Understand PWM meaning
PWM is a technique used to control power by switching it on and off quickly.Step 2: Match PWM to full form
Pulse Width Modulation means changing the width of the pulse to control power, which fits LED brightness control.Final Answer:
Pulse Width Modulation -> Option DQuick Check:
PWM = Pulse Width Modulation [OK]
- Confusing PWM with power management
- Thinking PWM is a wiring method
- Mixing up acronyms
Solution
Step 1: Check PWM constructor parameters
RPi.GPIO PWM takes pin number first, then frequency in Hz.Step 2: Verify start method usage
start() takes initial duty cycle (0-100), so start(0) is correct for off initially.Final Answer:
pwm = GPIO.PWM(18, 1000); pwm.start(0) -> Option AQuick Check:
GPIO.PWM(pin, freq) then start(duty) [OK]
- Swapping frequency and pin
- Starting PWM with frequency instead of duty
- Using zero frequency
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) pwm = GPIO.PWM(18, 500) pwm.start(0) pwm.ChangeDutyCycle(75)
Solution
Step 1: Understand duty cycle effect
Duty cycle 75 means LED receives power 75% of the time, so brightness is 75%.Step 2: Check code correctness
Code sets up PWM on pin 18 at 500 Hz, starts at 0, then changes duty cycle to 75 correctly.Final Answer:
LED will be at 75% brightness -> Option CQuick Check:
ChangeDutyCycle(75) = 75% brightness [OK]
- Confusing duty cycle with frequency
- Thinking 0 means full brightness
- Forgetting to start PWM before changing duty
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) pwm = GPIO.PWM(18, 1000) pwm.ChangeDutyCycle(50) pwm.start(50) GPIO.cleanup()
Solution
Step 1: Check PWM start and ChangeDutyCycle order
ChangeDutyCycle must be called after pwm.start(), not before.Step 2: Correct the order
Swap lines so pwm.start(50) comes before pwm.ChangeDutyCycle(50).Final Answer:
ChangeDutyCycle called before start; swap their order -> Option BQuick Check:
start() must come before ChangeDutyCycle() [OK]
- Calling ChangeDutyCycle before start
- Cleaning up GPIO too early
- Incorrect frequency assumptions
Solution
Step 1: Calculate total steps and delay
To increase from 0 to 100 in 5 seconds smoothly, small steps with short delay are needed.Step 2: Analyze each option timing
import time for dc in range(0, 101): pwm.ChangeDutyCycle(dc) time.sleep(0.05) uses 101 steps with 0.05s delay = 5.05s total, smooth increase.Final Answer:
import time for dc in range(0, 101): pwm.ChangeDutyCycle(dc) time.sleep(0.05) -> Option AQuick Check:
101 steps * 0.05s = ~5 seconds smooth ramp [OK]
- Using too few steps causing jumps
- Incorrect total time calculation
- Multiplying duty cycle incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
