0
0
Raspberry Piprogramming~20 mins

LED brightness control in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LED 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 of this PWM LED brightness code?

Consider this Python code running on a Raspberry Pi to control LED brightness using PWM. 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, 1000)  # 1kHz frequency
pwm.start(0)

for duty_cycle in range(0, 101, 25):
    pwm.ChangeDutyCycle(duty_cycle)
    print(f"LED brightness set to {duty_cycle}%")
    time.sleep(0.1)

pwm.stop()
GPIO.cleanup()
A
LED brightness set to 0%
LED brightness set to 50%
LED brightness set to 100%
B
LED brightness set to 0%
LED brightness set to 20%
LED brightness set to 40%
LED brightness set to 60%
LED brightness set to 80%
C
LED brightness set to 0%
LED brightness set to 25%
LED brightness set to 50%
LED brightness set to 75%
LED brightness set to 100%
D
LED brightness set to 0%
LED brightness set to 10%
LED brightness set to 20%
LED brightness set to 30%
LED brightness set to 40%
Attempts:
2 left
💡 Hint

Look at the range(0, 101, 25) which controls the duty cycle steps.

🧠 Conceptual
intermediate
1:30remaining
Which component controls LED brightness in PWM?

In PWM (Pulse Width Modulation) for LED brightness control on Raspberry Pi, what does changing the duty cycle do?

AIt changes the percentage of time the LED is ON in each cycle.
BIt changes the frequency of the LED blinking.
CIt changes the voltage supplied to the LED continuously.
DIt changes the color of the LED.
Attempts:
2 left
💡 Hint

Think about how PWM controls power by switching ON and OFF rapidly.

🔧 Debug
advanced
2:00remaining
Why does this LED brightness code raise an error?

Look at this code snippet for Raspberry Pi LED brightness control. It raises an error. What is the cause?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 500)
pwm.start(50)

for i in range(0, 110, 10):
    pwm.ChangeDutyCycle(i)
    print(f"Brightness: {i}%")

pwm.stop()
GPIO.cleanup()
APWM frequency 500 is too high and causes an error.
BThe GPIO pin 18 is not set as output before PWM start.
CThe pwm.start() method requires a duty cycle of 0 initially.
DDuty cycle values above 100 cause a ValueError in ChangeDutyCycle.
Attempts:
2 left
💡 Hint

Check the range of duty cycle values in the loop.

📝 Syntax
advanced
1:00remaining
Which option has correct syntax for PWM duty cycle change?

Which code snippet correctly changes the PWM duty cycle on Raspberry Pi?

Apwm.changeDutyCycle(75)
Bpwm.ChangeDutyCycle(75)
Cpwm.ChangeDutyCycle = 75
Dpwm.change_duty_cycle(75)
Attempts:
2 left
💡 Hint

Method names in RPi.GPIO are case sensitive.

🚀 Application
expert
1:30remaining
How many brightness levels can be set with 8-bit PWM?

If you use an 8-bit PWM controller for LED brightness on Raspberry Pi, how many distinct brightness levels can you set?

A256 levels
B512 levels
C128 levels
D1024 levels
Attempts:
2 left
💡 Hint

8-bit means how many bits are used to represent the duty cycle value?