Bird
Raised Fist0
Raspberry Piprogramming~20 mins

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

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

Practice

(1/5)
1. What does PWM stand for in the context of controlling LED brightness on a Raspberry Pi?
easy
A. Pulse Wire Mode
B. Power Wave Management
C. Pin Wiring Method
D. Pulse Width Modulation

Solution

  1. Step 1: Understand PWM meaning

    PWM is a technique used to control power by switching it on and off quickly.
  2. Step 2: Match PWM to full form

    Pulse Width Modulation means changing the width of the pulse to control power, which fits LED brightness control.
  3. Final Answer:

    Pulse Width Modulation -> Option D
  4. Quick Check:

    PWM = Pulse Width Modulation [OK]
Hint: PWM controls brightness by changing pulse width [OK]
Common Mistakes:
  • Confusing PWM with power management
  • Thinking PWM is a wiring method
  • Mixing up acronyms
2. Which of the following is the correct way to start PWM on GPIO pin 18 with a frequency of 1000 Hz using RPi.GPIO?
easy
A. pwm = GPIO.PWM(18, 1000); pwm.start(0)
B. pwm = GPIO.PWM(18); pwm.start(1000)
C. pwm = GPIO.PWM(1000, 18); pwm.start(0)
D. pwm = GPIO.PWM(18, 0); pwm.start(1000)

Solution

  1. Step 1: Check PWM constructor parameters

    RPi.GPIO PWM takes pin number first, then frequency in Hz.
  2. Step 2: Verify start method usage

    start() takes initial duty cycle (0-100), so start(0) is correct for off initially.
  3. Final Answer:

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

    GPIO.PWM(pin, freq) then start(duty) [OK]
Hint: PWM(pin, frequency) then start(duty cycle) [OK]
Common Mistakes:
  • Swapping frequency and pin
  • Starting PWM with frequency instead of duty
  • Using zero frequency
3. What will be the output brightness level of the LED after running this code snippet?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 500)
pwm.start(0)
pwm.ChangeDutyCycle(75)
medium
A. LED will be at 25% brightness
B. LED will be off
C. LED will be at 75% brightness
D. Code will cause an error

Solution

  1. Step 1: Understand duty cycle effect

    Duty cycle 75 means LED receives power 75% of the time, so brightness is 75%.
  2. Step 2: Check code correctness

    Code sets up PWM on pin 18 at 500 Hz, starts at 0, then changes duty cycle to 75 correctly.
  3. Final Answer:

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

    ChangeDutyCycle(75) = 75% brightness [OK]
Hint: Duty cycle % equals brightness % [OK]
Common Mistakes:
  • Confusing duty cycle with frequency
  • Thinking 0 means full brightness
  • Forgetting to start PWM before changing duty
4. Identify the error in this code that controls LED brightness and fix it:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 1000)
pwm.ChangeDutyCycle(50)
pwm.start(50)
GPIO.cleanup()
medium
A. Frequency 1000 is too high, use 100
B. ChangeDutyCycle called before start; swap their order
C. GPIO.cleanup() should be before pwm.start()
D. Missing GPIO.setwarnings(False)

Solution

  1. Step 1: Check PWM start and ChangeDutyCycle order

    ChangeDutyCycle must be called after pwm.start(), not before.
  2. Step 2: Correct the order

    Swap lines so pwm.start(50) comes before pwm.ChangeDutyCycle(50).
  3. Final Answer:

    ChangeDutyCycle called before start; swap their order -> Option B
  4. Quick Check:

    start() must come before ChangeDutyCycle() [OK]
Hint: Always start PWM before changing duty cycle [OK]
Common Mistakes:
  • Calling ChangeDutyCycle before start
  • Cleaning up GPIO too early
  • Incorrect frequency assumptions
5. You want to smoothly increase LED brightness from 0% to 100% over 5 seconds using PWM on pin 18. Which code snippet correctly implements this?
hard
A. import time for dc in range(0, 101): pwm.ChangeDutyCycle(dc) time.sleep(0.05)
B. import time for dc in range(0, 101, 10): pwm.ChangeDutyCycle(dc) time.sleep(0.5)
C. import time for dc in range(0, 101, 20): pwm.ChangeDutyCycle(dc) time.sleep(1)
D. import time for dc in range(0, 51): pwm.ChangeDutyCycle(dc*2) time.sleep(0.1)

Solution

  1. Step 1: Calculate total steps and delay

    To increase from 0 to 100 in 5 seconds smoothly, small steps with short delay are needed.
  2. Step 2: Analyze each option timing

    import time for dc in range(0, 101): pwm.ChangeDutyCycle(dc) time.sleep(0.05) uses 101 steps with 0.05s delay = 5.05s total, smooth increase.
  3. Final Answer:

    import time for dc in range(0, 101): pwm.ChangeDutyCycle(dc) time.sleep(0.05) -> Option A
  4. Quick Check:

    101 steps * 0.05s = ~5 seconds smooth ramp [OK]
Hint: Use small steps with short delay for smooth brightness [OK]
Common Mistakes:
  • Using too few steps causing jumps
  • Incorrect total time calculation
  • Multiplying duty cycle incorrectly
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