0
0
Raspberry Piprogramming~10 mins

LED brightness control in Raspberry Pi - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the PWM class from the gpiozero library.

Raspberry Pi
from gpiozero import [1]
Drag options to blanks, or click blank then click option'
APWMLED
BLED
CButton
DOutputDevice
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'LED' instead of 'PWMLED' which only supports on/off control.
2fill in blank
medium

Complete the code to create a PWMLED object on GPIO pin 18.

Raspberry Pi
led = PWMLED([1])
Drag options to blanks, or click blank then click option'
A17
B18
C22
D23
Attempts:
3 left
💡 Hint
Common Mistakes
Using a pin that does not support PWM or is not connected to the LED.
3fill in blank
hard

Fix the error in setting the LED brightness to 50%.

Raspberry Pi
led.value = [1]
Drag options to blanks, or click blank then click option'
A50
B1
C0.5
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer 50 instead of decimal 0.5 causes the LED to stay fully on or off.
4fill in blank
hard

Fill both blanks to create a dictionary that maps brightness levels to LED values for 0%, 50%, and 100%.

Raspberry Pi
brightness_levels = {0: [1], 50: [2], 100: 1.0}
Drag options to blanks, or click blank then click option'
A0.0
B0.5
C1
D50
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer percentages instead of decimal values for LED brightness.
5fill in blank
hard

Fill all three blanks to set the LED brightness to 75% using the dictionary and print the value.

Raspberry Pi
level = 75
led.value = brightness_levels.get([1], [2])
print(f"LED brightness set to [3]")
Drag options to blanks, or click blank then click option'
Alevel
B0.75
D75%
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong key or default value causes the LED to not set correctly.