Software PWM with RPi.GPIO in Raspberry Pi - Time & Space Complexity
When using software PWM on a Raspberry Pi, the program repeatedly changes pin states to create a signal.
We want to understand how the time spent grows as we increase the PWM frequency or duration.
Analyze the time complexity of the following software PWM code using RPi.GPIO.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm_pin = 18
frequency = 50 # Hz
period = 1 / frequency
duty_cycle = 0.5 # 50%
for i in range(100):
GPIO.output(pwm_pin, GPIO.HIGH)
time.sleep(period * duty_cycle)
GPIO.output(pwm_pin, GPIO.LOW)
time.sleep(period * (1 - duty_cycle))
This code creates a 50 Hz PWM signal on pin 18 by turning it on and off 100 times.
Look at what repeats in the code.
- Primary operation: The loop that sets the pin HIGH and LOW repeatedly.
- How many times: Exactly 100 times, as set by the for-loop.
The total time grows directly with the number of PWM cycles we run.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 cycles of HIGH and LOW switching |
| 100 | 100 cycles of HIGH and LOW switching |
| 1000 | 1000 cycles of HIGH and LOW switching |
Pattern observation: If you double the number of cycles, the total operations double too.
Time Complexity: O(n)
This means the time to run the PWM signal grows directly in proportion to the number of cycles you want.
[X] Wrong: "The time to run PWM stays the same no matter how many cycles I run."
[OK] Correct: Each cycle requires turning the pin on and off, so more cycles mean more work and more time.
Understanding how loops affect timing is important when controlling hardware like LEDs or motors with a Raspberry Pi.
This skill shows you can reason about how your code's running time changes as you ask it to do more.
"What if we changed the loop to run indefinitely until stopped? How would the time complexity be described then?"
Practice
RPi.GPIO.PWM allow you to control on a Raspberry Pi?Solution
Step 1: Understand what PWM controls
PWM (Pulse Width Modulation) controls how long a pin stays on versus off, affecting devices like LEDs or motors.Step 2: Identify the effect of switching pins fast
Switching pins on and off quickly changes brightness or speed smoothly.Final Answer:
The brightness of LEDs or speed of motors by switching pins on and off quickly -> Option DQuick Check:
Software PWM controls brightness/speed = B [OK]
- Confusing PWM with CPU or network control
- Thinking PWM changes file permissions
- Assuming PWM controls temperature
Solution
Step 1: Check PWM constructor parameters
The PWM constructor takes pin number first, then frequency in Hz, so GPIO.PWM(18, 1000) is correct.Step 2: Verify start method usage
The start method takes duty cycle percentage, so pwm.start(50) starts PWM at 50% duty cycle.Final Answer:
pwm = GPIO.PWM(18, 1000); pwm.start(50) -> Option CQuick Check:
Pin, frequency order + start with duty cycle = A [OK]
- Swapping frequency and pin arguments
- Passing duty cycle to PWM constructor
- Using start() with frequency instead of duty cycle
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(12, GPIO.OUT) pwm = GPIO.PWM(12, 500) pwm.start(75)
Solution
Step 1: Analyze PWM setup
Pin 12 is set as output, PWM started at 500 Hz frequency with 75% duty cycle.Step 2: Understand duty cycle effect on LED
75% duty cycle means LED is on 75% of the time, so it appears bright but not fully on.Final Answer:
LED will be at 75% brightness -> Option AQuick Check:
75% duty cycle = 75% brightness [OK]
- Thinking LED is off at start
- Assuming LED blinks instead of steady brightness
- Expecting code error without setup
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(21, GPIO.OUT) pwm = GPIO.PWM(21, 1000) pwm.start(110)
Solution
Step 1: Check duty cycle range
Duty cycle must be between 0 and 100; 110 is invalid and causes error.Step 2: Verify pin setup and frequency
Pin 21 is correctly set as output; frequency 1000 Hz is valid.Final Answer:
Duty cycle value 110 is invalid; must be between 0 and 100 -> Option AQuick Check:
Duty cycle > 100 invalid = D [OK]
- Using duty cycle above 100
- Forgetting to set pin as output
- Assuming frequency limits cause error
- Ignoring cleanup importance
Solution
Step 1: Understand smooth dimming with PWM
Smooth dimming requires gradually changing the duty cycle from 0% (off) to 100% (full brightness).Step 2: Identify correct method
Using a loop to increase duty cycle with small delays creates a smooth brightness change.Final Answer:
Use a loop increasing duty cycle from 0 to 100 with small delays between steps -> Option BQuick Check:
Gradual duty cycle change = smooth dimming [OK]
- Changing frequency instead of duty cycle
- Setting duty cycle instantly without steps
- Calling cleanup during PWM operation
Ready to prove it?
Pass the test and this pattern moves unknown
