0
0
Raspberry Piprogramming~10 mins

LED brightness control in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - LED brightness control
Start
Setup GPIO pin for PWM
Set initial brightness (duty cycle)
Enter loop
Increase brightness step
Check if brightness > max
Reset to min
Apply brightness to LED
Wait short delay
Repeat loop
The program sets up a PWM pin on the Raspberry Pi to control LED brightness by changing the duty cycle in a loop, increasing brightness step by step and resetting after reaching max.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 1000)
pwm.start(0)

try:
    while True:
        for dc in range(0, 101, 10):
            pwm.ChangeDutyCycle(dc)
            time.sleep(0.2)
finally:
    pwm.stop()
    GPIO.cleanup()
This code smoothly increases LED brightness from off (0%) to full (100%) in steps of 10%, pausing briefly at each step.
Execution Table
StepDuty Cycle (dc)ActionLED BrightnessDelay (seconds)
10Set PWM duty cycle to 0%LED off0.2
210Increase duty cycle to 10%Dim LED0.2
320Increase duty cycle to 20%Slightly brighter0.2
430Increase duty cycle to 30%Brighter0.2
540Increase duty cycle to 40%More bright0.2
650Increase duty cycle to 50%Half brightness0.2
760Increase duty cycle to 60%More than half bright0.2
870Increase duty cycle to 70%Bright LED0.2
980Increase duty cycle to 80%Very bright0.2
1090Increase duty cycle to 90%Almost full brightness0.2
11100Increase duty cycle to 100%Full brightness0.2
12Loop repeatsRestart from 0%LED off0.2
💡 Loop runs indefinitely until program is stopped manually.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10After Step 11Final
dc (duty cycle)001020304050607080901000 (loop restart)
Key Moments - 3 Insights
Why does the LED brightness change smoothly instead of instantly?
Because the duty cycle changes in small steps (10% increments) with a short delay (0.2 seconds) between each step, as shown in execution_table rows 1-11.
What happens when the duty cycle reaches 100%?
The program resets the duty cycle back to 0% to start the brightness cycle again, as seen in execution_table row 12.
Why do we use PWM to control LED brightness?
PWM changes how long the LED is on versus off very quickly, which our eyes see as brightness changes. This is shown by changing the duty cycle variable 'dc' in the code and execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 5. What is the LED brightness when duty cycle is 40%?
AMore bright
BLED off
CDim LED
DFull brightness
💡 Hint
Check the 'LED Brightness' column at Step 5 in the execution_table.
At which step does the duty cycle reach full brightness (100%)?
AStep 10
BStep 11
CStep 12
DStep 9
💡 Hint
Look at the 'Duty Cycle (dc)' column in the execution_table to find 100%.
If the delay time is increased from 0.2 to 1 second, how would the execution_table change?
AThe LED brightness would skip some levels.
BThe duty cycle values would change to larger steps.
CThe 'Delay (seconds)' column would show 1.0 instead of 0.2 for each step.
DThe loop would stop after one cycle.
💡 Hint
Focus on the 'Delay (seconds)' column in the execution_table.
Concept Snapshot
LED brightness control uses PWM on a Raspberry Pi GPIO pin.
Set up PWM with frequency, then change duty cycle (0-100%) to adjust brightness.
Increase duty cycle in steps with delays for smooth brightness change.
Loop to repeat brightness cycle.
Use GPIO.cleanup() to reset pins after program ends.
Full Transcript
This example shows how to control LED brightness on a Raspberry Pi using PWM. First, the GPIO pin is set up for PWM output. The program starts PWM with 0% duty cycle, meaning LED is off. Then, inside a loop, the duty cycle increases from 0% to 100% in steps of 10%. Each step changes the LED brightness slightly brighter. After reaching 100%, the duty cycle resets to 0%, and the cycle repeats. A short delay after each step makes the brightness change smooth and visible. The variable 'dc' tracks the duty cycle value. The program runs indefinitely until stopped. Finally, PWM is stopped and GPIO pins are cleaned up to avoid issues. This method uses PWM to simulate different brightness levels by changing how long the LED is on in each cycle.