Bird
Raised Fist0
Raspberry Piprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does PWM control in a servo motor connected to a Raspberry Pi?
easy
A. The temperature of the servo motor
B. The angle position of the servo arm
C. The brightness of an LED
D. The speed of the Raspberry Pi processor

Solution

  1. 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.
  2. Step 2: Identify what PWM affects

    It does not control speed, brightness, or temperature but the position of the servo arm.
  3. Final Answer:

    The angle position of the servo arm -> Option B
  4. Quick Check:

    PWM controls servo angle = D [OK]
Hint: PWM changes pulse width to set servo angle [OK]
Common Mistakes:
  • Confusing PWM with speed control
  • Thinking PWM controls LED brightness here
  • Assuming PWM changes temperature
2. Which of the following is the correct way to start PWM on GPIO pin 18 at 50 Hz in Python using RPi.GPIO?
easy
A. pwm = GPIO.PWM(18, 50); pwm.start(7.5)
B. pwm = GPIO.PWM(50, 18); pwm.start(7.5)
C. pwm = GPIO.PWM(18); pwm.start(50)
D. pwm = GPIO.PWM(7.5, 18); pwm.start(50)

Solution

  1. Step 1: Check PWM initialization syntax

    GPIO.PWM(pin, frequency) initializes PWM on the specified pin and frequency.
  2. Step 2: Verify correct parameters

    Pin 18 and frequency 50 Hz are correct; start() takes duty cycle like 7.5.
  3. Final Answer:

    pwm = GPIO.PWM(18, 50); pwm.start(7.5) -> Option A
  4. Quick Check:

    GPIO.PWM(pin, freq) with start(duty) = B [OK]
Hint: Remember GPIO.PWM(pin, freq) then start(duty) [OK]
Common Mistakes:
  • Swapping pin and frequency order
  • Missing frequency argument
  • Passing duty cycle in PWM() instead of start()
3. What will be the output angle of the servo if the duty cycle is set to 12.5% using PWM at 50 Hz?
medium
A. 0 degrees
B. 90 degrees
C. 270 degrees
D. 180 degrees

Solution

  1. 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°.
  2. Step 2: Match 12.5% duty cycle to angle

    12.5% duty cycle sets the servo to its maximum angle, 180°.
  3. Final Answer:

    180 degrees -> Option D
  4. Quick Check:

    12.5% duty cycle = 180° [OK]
Hint: Max duty cycle ~12.5% means 180° angle [OK]
Common Mistakes:
  • Confusing duty cycle values with angles
  • Assuming 12.5% is 90 degrees
  • Thinking servo can rotate beyond 180 degrees
4. Given this code snippet, what is the error?
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
medium
A. GPIO pins are not cleaned up after use
B. PWM frequency is incorrect
C. Duty cycle value is out of range
D. GPIO mode is not set

Solution

  1. Step 1: Review code for setup and cleanup

    The code sets mode, pin, and starts PWM correctly but does not call GPIO.cleanup().
  2. Step 2: Identify missing cleanup importance

    Not cleaning up GPIO pins can cause warnings or errors on next runs.
  3. Final Answer:

    GPIO pins are not cleaned up after use -> Option A
  4. Quick Check:

    Missing GPIO.cleanup() = A [OK]
Hint: Always call GPIO.cleanup() after PWM use [OK]
Common Mistakes:
  • Ignoring GPIO.cleanup()
  • Thinking frequency or duty cycle is wrong here
  • Forgetting to set GPIO mode
5. You want to sweep a servo motor from 0° to 180° smoothly using PWM on Raspberry Pi. Which approach is best?
hard
A. Use a fixed duty cycle of 12.5% without delay
B. Set duty cycle to 7.5% and wait for 5 seconds
C. Change duty cycle gradually from 2.5% to 12.5% in small steps with delays
D. Change frequency from 50 Hz to 100 Hz rapidly

Solution

  1. Step 1: Understand servo sweeping method

    Sweeping means moving smoothly from 0° to 180°, which requires changing duty cycle gradually.
  2. 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.
  3. Final Answer:

    Change duty cycle gradually from 2.5% to 12.5% in small steps with delays -> Option C
  4. Quick Check:

    Smooth sweep = gradual duty cycle change [OK]
Hint: Sweep by small duty cycle steps with delays [OK]
Common Mistakes:
  • Using fixed duty cycle for sweeping
  • Changing frequency instead of duty cycle
  • Skipping delays causing jerky motion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard