📖 Scenario: You have a small servo motor connected to your Raspberry Pi. You want to control its position by sending the right pulse width modulation (PWM) signals.Servo motors move to a position based on the length of the pulse they receive. By changing the pulse width, you can make the servo point to different angles.
🎯 Goal: Build a simple Python program that sets up PWM on a GPIO pin and moves the servo motor to a specific angle.
📋 What You'll Learn
Use the RPi.GPIO library to control GPIO pins
Set up PWM on GPIO pin 18
Calculate the correct duty cycle for a given servo angle
Move the servo to 90 degrees
Print the duty cycle used
💡 Why This Matters
🌍 Real World
Servo motors are used in robotics, remote-controlled vehicles, and home automation to control precise movements like steering or opening doors.
💼 Career
Understanding PWM control of servos is important for embedded systems engineers, robotics developers, and IoT device programmers.
Progress0 / 4 steps
1
Set up GPIO and PWM
Import the RPi.GPIO library as GPIO. Set the GPIO mode to GPIO.BCM. Create a PWM instance called pwm on GPIO pin 18 with frequency 50 Hz.
Raspberry Pi
Hint
Use GPIO.setmode(GPIO.BCM) to select pin numbering. Then set pin 18 as output. Finally, create PWM on pin 18 with 50Hz frequency.
2
Start PWM with 0 duty cycle
Start the pwm instance with a duty cycle of 0 to initialize the servo control.
Raspberry Pi
Hint
Use pwm.start(0) to begin PWM with zero duty cycle.
3
Calculate duty cycle for 90 degrees
Create a variable called angle and set it to 90. Calculate the duty cycle for this angle using the formula duty = (angle / 18) + 2. Store the result in a variable called duty.
Raspberry Pi
Hint
The duty cycle for a servo angle is usually calculated as (angle / 18) + 2.
4
Move servo and print duty cycle
Use pwm.ChangeDutyCycle(duty) to move the servo to the calculated position. Then print the duty cycle value using print(f"Duty cycle set to: {duty}"). Finally, clean up GPIO with GPIO.cleanup().
Raspberry Pi
Hint
Use pwm.ChangeDutyCycle(duty) to move the servo. Then print the duty cycle with an f-string. Don't forget to clean up GPIO.
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
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 B
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
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 A
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
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°.