0
0
Raspberry Piprogramming~20 mins

Servo motor control with PWM in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Servo PWM Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PWM duty cycle print?
Consider this Python code controlling a servo motor's PWM on a Raspberry Pi. What will be printed?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50)  # 50Hz frequency
pwm.start(7.5)  # Neutral position

time.sleep(1)
print(f"Current duty cycle: {7.5}")
pwm.ChangeDutyCycle(12.5)
time.sleep(1)
print(f"Current duty cycle: {12.5}")
pwm.stop()
GPIO.cleanup()
A
Current duty cycle: 7.5
Current duty cycle: 12.5
B
Current duty cycle: 12.5
Current duty cycle: 7.5
C
Current duty cycle: 0
Current duty cycle: 100
DSyntaxError due to missing GPIO.cleanup()
Attempts:
2 left
💡 Hint
Look at the print statements and the values passed to ChangeDutyCycle.
🧠 Conceptual
intermediate
1:30remaining
What does a 50Hz PWM frequency mean for servo control?
In servo motor control using PWM on a Raspberry Pi, what does setting the PWM frequency to 50Hz imply?
AThe PWM signal repeats 50 times per second, matching typical servo requirements
BThe servo motor will rotate at 50 revolutions per second
CThe duty cycle must always be 50%
DThe PWM signal is too fast for any servo to respond
Attempts:
2 left
💡 Hint
Think about how often the PWM signal repeats per second.
🔧 Debug
advanced
2:30remaining
Why does this servo not move as expected?
This code is intended to move a servo to 0, 90, and 180 degrees but the servo stays still. What is the problem?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50)
pwm.start(0)

positions = [2.5, 7.5, 12.5]
for pos in positions:
    pwm.ChangeDutyCycle(pos)
    time.sleep(1)
pwm.stop()
GPIO.cleanup()
AThe GPIO pin 18 is not set as input
BStarting PWM with 0 duty cycle causes servo not to respond; should start with 7.5
CThe frequency 50Hz is too high for servo control
DThe positions list values are out of range for duty cycle
Attempts:
2 left
💡 Hint
Think about the initial duty cycle value when starting PWM.
📝 Syntax
advanced
1:30remaining
Which option causes a SyntaxError in this PWM code?
Identify which code snippet will cause a syntax error when controlling a servo with PWM on Raspberry Pi.
A
pwm = GPIO.PWM(18, 50)
pwm.start(7.5)
pwm.ChangeDutyCycle(10)
B
pwm = GPIO.PWM(18, 50)
pwm.start(7.5)
pwm.ChangeDutyCycle(10.0)
C
pwm = GPIO.PWM(18, 50)
pwm.start(7.5)
pwm.ChangeDutyCycle(10
D
pwm = GPIO.PWM(18, 50)
pwm.start(7.5)
pwm.ChangeDutyCycle(7.5)
Attempts:
2 left
💡 Hint
Look carefully at parentheses in each option.
🚀 Application
expert
3:00remaining
How many distinct positions can this servo code produce?
Given this code controlling a servo with PWM on Raspberry Pi, how many distinct servo positions will it produce?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50)
pwm.start(7.5)

for duty in range(25, 126, 25):  # duty cycles from 2.5 to 12.5 stepping by 2.5
    pwm.ChangeDutyCycle(duty / 10)
    time.sleep(0.5)
pwm.stop()
GPIO.cleanup()
A6
B4
C3
D5
Attempts:
2 left
💡 Hint
Count how many values the range produces and how they are used.