0
0
Raspberry Piprogramming~20 mins

Software PWM with RPi.GPIO in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PWM Mastery with RPi.GPIO
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 using RPi.GPIO to create a software PWM on pin 18. 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, 100)  # 100 Hz frequency
pwm.start(0)  # Start with 0% duty cycle

for dc in range(0, 101, 50):
    pwm.ChangeDutyCycle(dc)
    print(f"Duty Cycle set to: {dc}%")
    time.sleep(0.1)

pwm.stop()
GPIO.cleanup()
A
Duty Cycle set to: 0%
Duty Cycle set to: 50%
B
Duty Cycle set to: 0%
Duty Cycle set to: 50%
Duty Cycle set to: 100%
C
Duty Cycle set to: 0%
Duty Cycle set to: 50%
Duty Cycle set to: 100%
Duty Cycle set to: 150%
D
Duty Cycle set to: 0%
Duty Cycle set to: 25%
Duty Cycle set to: 50%
Duty Cycle set to: 75%
Duty Cycle set to: 100%
Attempts:
2 left
💡 Hint
Look at the range function parameters and step size.
Predict Output
intermediate
2:00remaining
What error does this PWM code raise?
What error will this code raise when run on a Raspberry Pi?
Raspberry Pi
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 0)  # Frequency set to 0
pwm.start(50)
ARuntimeError: PWM frequency cannot be zero
BTypeError: frequency argument must be int
CNo error, PWM starts with 50% duty cycle
DValueError: PWM frequency must be greater than 0
Attempts:
2 left
💡 Hint
Think about what frequency zero means for PWM.
🔧 Debug
advanced
3:00remaining
Why does this PWM code not change brightness smoothly?
This code is intended to smoothly increase LED brightness using PWM. Why does the LED jump abruptly instead of smoothly changing brightness?
Raspberry Pi
import RPi.GPIO as GPIO
import time

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

for dc in range(0, 101, 20):
    pwm.ChangeDutyCycle(dc)
    time.sleep(0.5)

pwm.stop()
GPIO.cleanup()
APWM must be started with 100% duty cycle for smooth change
BThe frequency 1000 Hz is too high for LED brightness control
CThe step size of 20 is too large for smooth brightness change
Dtime.sleep(0.5) is too short to see brightness change
Attempts:
2 left
💡 Hint
Think about how many steps the brightness changes in.
📝 Syntax
advanced
2:00remaining
Which option fixes the syntax error in this PWM code?
This code snippet has a syntax error. Which option fixes it correctly?
Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 100)
pwm.start(50
pwm.ChangeDutyCycle(75)
pwm.stop()
GPIO.cleanup()
AAdd a closing parenthesis after 50: pwm.start(50)
BReplace pwm.start(50 with pwm.start[50]
CAdd a colon after pwm.start(50: pwm.start(50):
DRemove pwm.start(50 entirely
Attempts:
2 left
💡 Hint
Look carefully at the parentheses in the pwm.start line.
🚀 Application
expert
2:00remaining
How many PWM cycles occur in 2 seconds at 500 Hz?
You set up software PWM on a Raspberry Pi with frequency 500 Hz. How many complete PWM cycles will occur in 2 seconds?
A1000
B250
C2000
D500
Attempts:
2 left
💡 Hint
Multiply frequency by time in seconds.