Challenge - 5 Problems
Servo PWM Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Look at the print statements and the values passed to ChangeDutyCycle.
✗ Incorrect
The code prints the duty cycle values exactly as given in the print statements. The first print shows 7.5, the second shows 12.5. No syntax errors occur.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about how often the PWM signal repeats per second.
✗ Incorrect
50Hz means the PWM signal repeats 50 times each second, which is the standard frequency for many hobby servos to interpret position commands.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Think about the initial duty cycle value when starting PWM.
✗ Incorrect
Starting PWM with 0 duty cycle means no signal is sent initially, so the servo may not respond. Starting with 7.5 (neutral) is standard to initialize the servo position.
📝 Syntax
advanced1: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.
Attempts:
2 left
💡 Hint
Look carefully at parentheses in each option.
✗ Incorrect
Option C is missing a closing parenthesis in ChangeDutyCycle call, causing a SyntaxError.
🚀 Application
expert3: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()
Attempts:
2 left
💡 Hint
Count how many values the range produces and how they are used.
✗ Incorrect
The range(25, 126, 25) produces 5 values: 25, 50, 75, 100, 125. Dividing by 10 gives 2.5, 5.0, 7.5, 10.0, 12.5 duty cycles, so 5 distinct positions.