Servo motors need special signals to move to the right position. PWM helps send these signals easily.
Servo motor control with PWM in Raspberry Pi
import RPi.GPIO as GPIO import time pin_number = 12 # Define your pin number here GPIO.setmode(GPIO.BOARD) GPIO.setup(pin_number, GPIO.OUT) pwm = GPIO.PWM(pin_number, 50) # 50 Hz frequency pwm.start(7.5) # Start at middle position # To change angle: pwm.ChangeDutyCycle(new_duty_cycle) # When done: pwm.stop() GPIO.cleanup()
Use 50 Hz frequency because servo motors expect a 20 ms period signal.
Duty cycle controls the angle: usually between 2.5% (0°) and 12.5% (180°).
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) pwm = GPIO.PWM(12, 50) # 50 Hz pwm.start(7.5) # Middle position # Move to 0 degrees pwm.ChangeDutyCycle(2.5) time.sleep(1) # Move to 180 degrees pwm.ChangeDutyCycle(12.5) time.sleep(1) pwm.stop() GPIO.cleanup()
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(18, GPIO.OUT) pwm = GPIO.PWM(18, 50) pwm.start(0) for duty in [2.5, 5, 7.5, 10, 12.5]: pwm.ChangeDutyCycle(duty) time.sleep(0.5) pwm.stop() GPIO.cleanup()
This program moves the servo motor to 0°, 90°, and 180° positions with pauses and prints the current angle. It cleans up GPIO pins at the end.
import RPi.GPIO as GPIO import time # Set up GPIO pin 11 for PWM GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.OUT) # Create PWM instance at 50 Hz servo = GPIO.PWM(11, 50) # Start PWM with 0 duty cycle (servo off) servo.start(0) try: # Move servo to 0 degrees servo.ChangeDutyCycle(2.5) print("Servo at 0 degrees") time.sleep(1) # Move servo to 90 degrees servo.ChangeDutyCycle(7.5) print("Servo at 90 degrees") time.sleep(1) # Move servo to 180 degrees servo.ChangeDutyCycle(12.5) print("Servo at 180 degrees") time.sleep(1) finally: # Stop PWM and clean up servo.stop() GPIO.cleanup() print("Cleaned up GPIO")
Always clean up GPIO pins after use to avoid warnings or errors next time.
Servo angles depend on the duty cycle; check your servo's datasheet for exact values.
Use delays after changing duty cycle to give the servo time to move.
Servo motors use PWM signals to set their angle.
Use 50 Hz frequency and adjust duty cycle between about 2.5% and 12.5% for 0° to 180°.
Always clean up GPIO pins after controlling the servo.
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
