Servo motor control with PWM in Raspberry Pi - Time & Space Complexity
When controlling a servo motor with PWM on a Raspberry Pi, it's important to understand how the program's running time changes as we adjust parameters like the number of position commands.
We want to know how the time needed grows when we send more commands to the servo.
Analyze the time complexity of the following code snippet.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12, 50) # 50Hz frequency
pwm.start(0)
positions = [2.5, 7.5, 12.5] # duty cycles for servo positions
for pos in positions:
pwm.ChangeDutyCycle(pos)
time.sleep(0.5) # wait for servo to reach position
pwm.stop()
GPIO.cleanup()
This code moves a servo motor to different positions by changing the PWM duty cycle in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of positions to send PWM signals.
- How many times: Once for each position in the list.
Each new position adds one more PWM signal change and wait time.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 PWM changes and waits |
| 10 | 10 PWM changes and waits |
| 100 | 100 PWM changes and waits |
Pattern observation: The total time grows directly with the number of positions sent.
Time Complexity: O(n)
This means the time to control the servo grows linearly with the number of position commands.
[X] Wrong: "Changing the PWM duty cycle once controls all positions instantly."
[OK] Correct: Each position requires a separate PWM change and wait time; the servo needs time to move physically to each position.
Understanding how loops affect timing in hardware control shows you can connect software logic with real-world device behavior, a valuable skill in many projects.
"What if we removed the wait time after each PWM change? How would the time complexity change?"
Practice
Solution
Step 1: Understand PWM role in servo control
PWM (Pulse Width Modulation) sends signals to set the servo arm's angle by changing the pulse width.Step 2: Identify what PWM affects
It does not control speed, brightness, or temperature but the position of the servo arm.Final Answer:
The angle position of the servo arm -> Option BQuick Check:
PWM controls servo angle = D [OK]
- Confusing PWM with speed control
- Thinking PWM controls LED brightness here
- Assuming PWM changes temperature
Solution
Step 1: Check PWM initialization syntax
GPIO.PWM(pin, frequency) initializes PWM on the specified pin and frequency.Step 2: Verify correct parameters
Pin 18 and frequency 50 Hz are correct; start() takes duty cycle like 7.5.Final Answer:
pwm = GPIO.PWM(18, 50); pwm.start(7.5) -> Option AQuick Check:
GPIO.PWM(pin, freq) with start(duty) = B [OK]
- Swapping pin and frequency order
- Missing frequency argument
- Passing duty cycle in PWM() instead of start()
Solution
Step 1: Understand duty cycle to angle mapping
At 50 Hz, about 2.5% duty cycle corresponds to 0°, 7.5% to 90°, and 12.5% to 180°.Step 2: Match 12.5% duty cycle to angle
12.5% duty cycle sets the servo to its maximum angle, 180°.Final Answer:
180 degrees -> Option DQuick Check:
12.5% duty cycle = 180° [OK]
- Confusing duty cycle values with angles
- Assuming 12.5% is 90 degrees
- Thinking servo can rotate beyond 180 degrees
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) pwm = GPIO.PWM(18, 50) pwm.start(7.5) # Missing cleanup
Solution
Step 1: Review code for setup and cleanup
The code sets mode, pin, and starts PWM correctly but does not call GPIO.cleanup().Step 2: Identify missing cleanup importance
Not cleaning up GPIO pins can cause warnings or errors on next runs.Final Answer:
GPIO pins are not cleaned up after use -> Option AQuick Check:
Missing GPIO.cleanup() = A [OK]
- Ignoring GPIO.cleanup()
- Thinking frequency or duty cycle is wrong here
- Forgetting to set GPIO mode
Solution
Step 1: Understand servo sweeping method
Sweeping means moving smoothly from 0° to 180°, which requires changing duty cycle gradually.Step 2: Identify correct PWM control
Duty cycle must change from about 2.5% (0°) to 12.5% (180%) in small steps with short delays for smooth motion.Final Answer:
Change duty cycle gradually from 2.5% to 12.5% in small steps with delays -> Option CQuick Check:
Smooth sweep = gradual duty cycle change [OK]
- Using fixed duty cycle for sweeping
- Changing frequency instead of duty cycle
- Skipping delays causing jerky motion
Ready to prove it?
Pass the test and this pattern moves unknown
