Challenge - 5 Problems
PWMLED Brightness Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
The value property sets brightness from 0 (off) to 1 (full brightness).
✗ Incorrect
The PWMLED.value property holds the brightness level as a float between 0 and 1. Setting it to 0.5 means half brightness, so printing led.value outputs 0.5.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
PWMLED.value must be between 0 and 1 inclusive.
✗ Incorrect
Setting led.value to 1.5 is invalid because brightness must be between 0 and 1. This raises a ValueError.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Brightness values must be between 0 and 1 inclusive.
✗ Incorrect
Setting led.value to a negative number is invalid. The PWMLED class clamps the value to 0, so the LED stays off.
🧠 Conceptual
advanced2:00remaining
How does PWMLED control brightness?
Which best describes how PWMLED changes LED brightness?
Attempts:
2 left
💡 Hint
PWM means Pulse Width Modulation.
✗ Incorrect
PWMLED uses Pulse Width Modulation: it switches the LED on and off very quickly. The ratio of on-time to off-time controls perceived brightness.
❓ Predict Output
expert2: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))
Attempts:
2 left
💡 Hint
Remember PWMLED.value clamps between 0 and 1.
✗ Incorrect
Starting at 0.3, adding 0.4 gives 0.7, subtracting 0.1 gives 0.6. The final value is 0.6.