0
0
Raspberry Piprogramming~10 mins

Servo motor control with PWM in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Servo motor control with PWM
Start program
Setup GPIO pin for PWM
Initialize PWM with frequency
Set duty cycle to move servo
Wait for servo to reach position
Change duty cycle for new position
Repeat or stop PWM
Cleanup GPIO and end
The program sets up a GPIO pin for PWM, sends signals with different duty cycles to move the servo, waits for the servo to move, then cleans up.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12, 50)
pwm.start(7.5)  # Middle position

# Move servo to 0 degrees
duty = 2.5
pwm.ChangeDutyCycle(duty)
time.sleep(1)

# Move servo to 180 degrees
duty = 12.5
pwm.ChangeDutyCycle(duty)
time.sleep(1)

pwm.stop()
GPIO.cleanup()
This code moves a servo motor connected to pin 12 from middle to 0 degrees, then to 180 degrees using PWM signals.
Execution Table
StepActionDuty Cycle (%)Servo PositionWait Time (s)
1Start PWM at middle position7.590 degrees (middle)0
2Change duty cycle to 2.52.50 degrees (left)1
3Change duty cycle to 12.512.5180 degrees (right)1
4Stop PWM and cleanup-Servo stops moving0
💡 PWM stopped and GPIO cleaned up, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
duty-7.52.512.5-
Servo Position-90 degrees0 degrees180 degreesStopped
Key Moments - 3 Insights
Why does changing the duty cycle move the servo?
Changing the duty cycle changes the pulse width sent to the servo, which tells it what angle to move to, as shown in steps 2 and 3 of the execution_table.
Why do we wait after changing the duty cycle?
The servo needs time to physically move to the new position, so the program waits 1 second after each duty cycle change (see Wait Time column in execution_table).
What happens if we don't stop PWM and cleanup GPIO?
The servo might keep receiving signals and the GPIO pins stay reserved, which can cause errors later. Step 4 shows stopping PWM and cleaning up to avoid this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the duty cycle when the servo is at 0 degrees?
A7.5
B12.5
C2.5
D0
💡 Hint
Check Step 2 in the execution_table where the servo moves to 0 degrees.
At which step does the servo move to 180 degrees?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the Servo Position column in the execution_table.
If we remove the time.sleep(1) after changing duty cycle, what happens?
AServo moves instantly to position
BServo might not reach the position before next command
CProgram crashes
DDuty cycle stays the same
💡 Hint
Refer to the key_moments explanation about waiting after duty cycle changes.
Concept Snapshot
Servo control uses PWM signals on a GPIO pin.
Set frequency (usually 50Hz) and duty cycle to control angle.
Duty cycle ~2.5% = 0°, ~7.5% = 90°, ~12.5% = 180°.
Wait after changing duty cycle for servo to move.
Stop PWM and cleanup GPIO when done.
Full Transcript
This visual execution shows how to control a servo motor using PWM on a Raspberry Pi. The program starts by setting up the GPIO pin and PWM frequency. It begins PWM with a 7.5% duty cycle to position the servo at 90 degrees. Then it changes the duty cycle to 2.5% to move the servo to 0 degrees, waits 1 second, changes to 12.5% for 180 degrees, waits again, and finally stops PWM and cleans up GPIO. Variables like duty cycle and servo position change step by step. Waiting after changing duty cycle is important for the servo to physically move. Stopping PWM and cleaning GPIO prevents errors. The quiz checks understanding of duty cycles and timing.