Bird
Raised Fist0
Raspberry Piprogramming~20 mins

PWMLED for brightness in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PWMLED Brightness Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output brightness level?
Consider this Python code using gpiozero's PWMLED to set brightness. What brightness value will be printed?
Raspberry Pi
from gpiozero import PWMLED
led = PWMLED(17)
led.value = 0.5
print(led.value)
A0.0
BNone
C1.0
D0.5
Attempts:
2 left
💡 Hint
The value property sets brightness from 0 (off) to 1 (full brightness).
❓ Predict Output
intermediate
2:00remaining
What happens if you set PWMLED value to 1.5?
What will happen when running this code snippet?
Raspberry Pi
from gpiozero import PWMLED
led = PWMLED(17)
led.value = 1.5
print(led.value)
AValueError is raised
Bled.value prints 0.0
Cled.value prints 1.5
Dled.value prints 1.0
Attempts:
2 left
💡 Hint
PWMLED.value must be between 0 and 1 inclusive.
🔧 Debug
advanced
2:00remaining
Why does this PWMLED code not change brightness?
This code tries to set brightness but the LED stays off. What is the problem?
Raspberry Pi
from gpiozero import PWMLED
led = PWMLED(17)
led.value = -0.1
print(led.value)
APWMLED does not support setting value directly
BLED pin 17 is wrong, so no brightness change
CNegative brightness is invalid and ignored, so LED stays off (value 0)
DThe print statement causes the LED to turn off
Attempts:
2 left
💡 Hint
Brightness values must be between 0 and 1 inclusive.
🧠 Conceptual
advanced
2:00remaining
How does PWMLED control brightness?
Which best describes how PWMLED changes LED brightness?
AIt turns the LED on and off very fast to simulate brightness levels
BIt changes the voltage supplied to the LED continuously
CIt changes the LED color to appear dimmer
DIt uses a resistor to reduce current for brightness control
Attempts:
2 left
💡 Hint
PWM means Pulse Width Modulation.
❓ Predict Output
expert
2:00remaining
What is the final brightness value after this sequence?
What value will be printed after running this code?
Raspberry Pi
from gpiozero import PWMLED
led = PWMLED(17)
led.value = 0.3
led.value += 0.4
led.value -= 0.1
print(round(led.value, 2))
A0.0
B0.6
C0.7
D0.5
Attempts:
2 left
💡 Hint
Remember PWMLED.value clamps between 0 and 1.

Practice

(1/5)
1. What does the PWMLED class control on a Raspberry Pi?
easy
A. The speed of the Raspberry Pi fan
B. The color of an LED by mixing red, green, and blue lights
C. The temperature of the Raspberry Pi CPU
D. The brightness of an LED by changing how long it is on and off quickly

Solution

  1. Step 1: Understand PWMLED purpose

    PWMLED is used to control LED brightness by adjusting the time it stays on and off rapidly.
  2. Step 2: Compare options

    Only The brightness of an LED by changing how long it is on and off quickly describes brightness control using on/off timing, which matches PWMLED's function.
  3. Final Answer:

    The brightness of an LED by changing how long it is on and off quickly -> Option D
  4. Quick Check:

    PWMLED controls brightness = A [OK]
Hint: PWM means brightness by fast on/off timing [OK]
Common Mistakes:
  • Confusing brightness with color control
  • Thinking PWMLED controls temperature or fan speed
2. Which of the following is the correct way to set the brightness of a PWMLED named led to half brightness?
easy
A. led.set_brightness = 0.5
B. led.brightness(0.5)
C. led.brightness = 0.5
D. led.brightness = 50

Solution

  1. Step 1: Recall PWMLED brightness syntax

    Brightness is set by assigning a float between 0 and 1 to the brightness attribute.
  2. Step 2: Check each option

    led.brightness = 0.5 uses correct attribute assignment with 0.5 (half brightness). Others use wrong syntax or wrong value type.
  3. Final Answer:

    led.brightness = 0.5 -> Option C
  4. Quick Check:

    Set brightness by attribute = C [OK]
Hint: Use led.brightness = value between 0 and 1 [OK]
Common Mistakes:
  • Calling brightness like a function
  • Using integer 50 instead of 0.5
  • Trying to assign to a method
3. What will be the output behavior of this code snippet?
from gpiozero import PWMLED
import time
led = PWMLED(17)
led.brightness = 0.2
time.sleep(2)
led.brightness = 0.8
time.sleep(2)
led.off()
medium
A. LED glows dimly for 2 seconds, then brightly for 2 seconds, then turns off
B. LED glows brightly for 2 seconds, then dimly for 2 seconds, then turns off
C. LED stays off the whole time
D. Code causes an error because brightness is set incorrectly

Solution

  1. Step 1: Analyze brightness settings

    First brightness is 0.2 (dim), then after 2 seconds it changes to 0.8 (bright).
  2. Step 2: Understand timing and off command

    Each brightness level lasts 2 seconds, then LED is turned off.
  3. Final Answer:

    LED glows dimly for 2 seconds, then brightly for 2 seconds, then turns off -> Option A
  4. Quick Check:

    Brightness changes 0.2 to 0.8 then off = A [OK]
Hint: Brightness values control glow level over time [OK]
Common Mistakes:
  • Mixing order of brightness values
  • Thinking brightness values cause errors
  • Assuming LED stays on after off()
4. Identify the error in this code that tries to set LED brightness:
from gpiozero import PWMLED
led = PWMLED(18)
led.brightness(0.7)
medium
A. Using parentheses after brightness instead of assignment
B. Wrong pin number for PWMLED
C. Missing import for time module
D. PWMLED does not support brightness control

Solution

  1. Step 1: Check how brightness is set

    Brightness is an attribute, so it should be assigned a value, not called like a function.
  2. Step 2: Verify other options

    Pin 18 is valid, time module is not needed here, and PWMLED supports brightness.
  3. Final Answer:

    Using parentheses after brightness instead of assignment -> Option A
  4. Quick Check:

    brightness is attribute, not method = D [OK]
Hint: Set brightness with =, not parentheses [OK]
Common Mistakes:
  • Calling brightness like a function
  • Assuming pin number causes error
  • Thinking time import is required here
5. You want to smoothly increase an LED's brightness from off to full over 5 seconds using PWMLED. Which code snippet correctly achieves this?
hard
A.
from gpiozero import PWMLED
import time
led = PWMLED(21)
for i in range(5):
    led.brightness = i
    time.sleep(1)
led.off()
B.
from gpiozero import PWMLED
import time
led = PWMLED(21)
for i in range(6):
    led.brightness = i / 5
    time.sleep(1)
led.off()
C.
from gpiozero import PWMLED
import time
led = PWMLED(21)
for i in range(0, 1, 0.2):
    led.brightness = i
    time.sleep(1)
led.off()
D.
from gpiozero import PWMLED
import time
led = PWMLED(21)
for i in range(5):
    led.brightness = 1 - i / 5
    time.sleep(1)
led.off()

Solution

  1. Step 1: Understand brightness range and timing

    Brightness must go from 0 to 1 in steps over 5 seconds, so dividing i by 5 in 6 steps (0 to 5) works.
  2. Step 2: Check each code snippet

    from gpiozero import PWMLED
    import time
    led = PWMLED(21)
    for i in range(6):
        led.brightness = i / 5
        time.sleep(1)
    led.off()
    correctly uses i/5 for brightness from 0 to 1 in 1-second steps.
    from gpiozero import PWMLED
    import time
    led = PWMLED(21)
    for i in range(5):
        led.brightness = i
        time.sleep(1)
    led.off()
    sets brightness to integers >1 (invalid).
    from gpiozero import PWMLED
    import time
    led = PWMLED(21)
    for i in range(0, 1, 0.2):
        led.brightness = i
        time.sleep(1)
    led.off()
    uses range with float step (invalid syntax).
    from gpiozero import PWMLED
    import time
    led = PWMLED(21)
    for i in range(5):
        led.brightness = 1 - i / 5
        time.sleep(1)
    led.off()
    decreases brightness instead of increasing.
  3. Final Answer:

    Code that increases brightness smoothly from 0 to 1 over 5 seconds using i/5 steps -> Option B
  4. Quick Check:

    Brightness from 0 to 1 in steps = B [OK]
Hint: Divide loop index by max steps for smooth brightness [OK]
Common Mistakes:
  • Using integer brightness values >1
  • Trying to use float step in range (not allowed)
  • Decreasing brightness instead of increasing
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