Bird
Raised Fist0
Raspberry Piprogramming~20 mins

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

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

Practice

(1/5)
1. What does Software PWM using RPi.GPIO.PWM allow you to control on a Raspberry Pi?
easy
A. The temperature of the Raspberry Pi CPU
B. The file system permissions on the Raspberry Pi
C. The network speed of the Raspberry Pi
D. The brightness of LEDs or speed of motors by switching pins on and off quickly

Solution

  1. Step 1: Understand what PWM controls

    PWM (Pulse Width Modulation) controls how long a pin stays on versus off, affecting devices like LEDs or motors.
  2. Step 2: Identify the effect of switching pins fast

    Switching pins on and off quickly changes brightness or speed smoothly.
  3. Final Answer:

    The brightness of LEDs or speed of motors by switching pins on and off quickly -> Option D
  4. Quick Check:

    Software PWM controls brightness/speed = B [OK]
Hint: PWM controls brightness or speed by fast switching [OK]
Common Mistakes:
  • Confusing PWM with CPU or network control
  • Thinking PWM changes file permissions
  • Assuming PWM controls temperature
2. Which of the following is the correct way to start PWM on pin 18 with a frequency of 1000 Hz using RPi.GPIO?
easy
A. pwm = GPIO.PWM(1000, 18); pwm.start(50)
B. pwm = GPIO.PWM(18); pwm.start(1000)
C. pwm = GPIO.PWM(18, 1000); pwm.start(50)
D. pwm = GPIO.PWM(18, 50); pwm.start(1000)

Solution

  1. Step 1: Check PWM constructor parameters

    The PWM constructor takes pin number first, then frequency in Hz, so GPIO.PWM(18, 1000) is correct.
  2. Step 2: Verify start method usage

    The start method takes duty cycle percentage, so pwm.start(50) starts PWM at 50% duty cycle.
  3. Final Answer:

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

    Pin, frequency order + start with duty cycle = A [OK]
Hint: PWM(pin, freq), then start(duty cycle) [OK]
Common Mistakes:
  • Swapping frequency and pin arguments
  • Passing duty cycle to PWM constructor
  • Using start() with frequency instead of duty cycle
3. What will be the output on the LED connected to pin 12 after running this code snippet?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12, 500)
pwm.start(75)
medium
A. LED will be at 75% brightness
B. LED will blink slowly
C. LED will be off
D. Code will raise an error

Solution

  1. Step 1: Analyze PWM setup

    Pin 12 is set as output, PWM started at 500 Hz frequency with 75% duty cycle.
  2. Step 2: Understand duty cycle effect on LED

    75% duty cycle means LED is on 75% of the time, so it appears bright but not fully on.
  3. Final Answer:

    LED will be at 75% brightness -> Option A
  4. Quick Check:

    75% duty cycle = 75% brightness [OK]
Hint: Duty cycle % = LED brightness % [OK]
Common Mistakes:
  • Thinking LED is off at start
  • Assuming LED blinks instead of steady brightness
  • Expecting code error without setup
4. Identify the error in this code snippet for PWM control:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
pwm = GPIO.PWM(21, 1000)
pwm.start(110)
medium
A. Duty cycle value 110 is invalid; must be between 0 and 100
B. Pin 21 is not set as output
C. PWM frequency 1000 is too low
D. Missing GPIO.cleanup() call

Solution

  1. Step 1: Check duty cycle range

    Duty cycle must be between 0 and 100; 110 is invalid and causes error.
  2. Step 2: Verify pin setup and frequency

    Pin 21 is correctly set as output; frequency 1000 Hz is valid.
  3. Final Answer:

    Duty cycle value 110 is invalid; must be between 0 and 100 -> Option A
  4. Quick Check:

    Duty cycle > 100 invalid = D [OK]
Hint: Duty cycle must be 0-100% only [OK]
Common Mistakes:
  • Using duty cycle above 100
  • Forgetting to set pin as output
  • Assuming frequency limits cause error
  • Ignoring cleanup importance
5. You want to smoothly dim an LED connected to pin 17 from off to full brightness using Software PWM. Which approach correctly achieves this?
hard
A. Set duty cycle to 100 immediately and then to 0 after 5 seconds
B. Use a loop increasing duty cycle from 0 to 100 with small delays between steps
C. Change frequency from 0 to 1000 Hz gradually while keeping duty cycle constant
D. Call GPIO.cleanup() repeatedly in a loop while PWM is running

Solution

  1. Step 1: Understand smooth dimming with PWM

    Smooth dimming requires gradually changing the duty cycle from 0% (off) to 100% (full brightness).
  2. Step 2: Identify correct method

    Using a loop to increase duty cycle with small delays creates a smooth brightness change.
  3. Final Answer:

    Use a loop increasing duty cycle from 0 to 100 with small delays between steps -> Option B
  4. Quick Check:

    Gradual duty cycle change = smooth dimming [OK]
Hint: Increase duty cycle stepwise with delay for smooth dimming [OK]
Common Mistakes:
  • Changing frequency instead of duty cycle
  • Setting duty cycle instantly without steps
  • Calling cleanup during PWM operation
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