📖 Scenario: You have a Raspberry Pi and want to control the brightness of an LED. The LED can only be fully ON or fully OFF, but you want it to glow with different brightness levels like a dimmer switch.
🎯 Goal: Learn how to use PWM (Pulse Width Modulation) to simulate analog control of LED brightness using digital signals.
📋 What You'll Learn
Create a variable to represent the LED pin number
Create a variable to represent the PWM frequency
Use PWM to set the LED brightness to 50%
Print the duty cycle value to show the brightness level
💡 Why This Matters
🌍 Real World
PWM is used in many electronics to control brightness of lights, speed of motors, and volume of sound without needing expensive analog parts.
💼 Career
Understanding PWM is important for jobs in embedded systems, robotics, and hardware programming where digital control of analog devices is common.
Progress0 / 4 steps
1
Set up the LED pin and PWM frequency
Create a variable called led_pin and set it to 18. Create another variable called pwm_freq and set it to 1000 (this is the PWM frequency in Hertz).
Raspberry Pi
Hint
The LED pin is usually a GPIO number. PWM frequency controls how fast the signal switches ON and OFF.
2
Set the PWM duty cycle for brightness
Create a variable called duty_cycle and set it to 50 to represent 50% brightness.
Raspberry Pi
Hint
The duty cycle is the percentage of time the signal is ON in one cycle.
3
Explain why PWM is used for analog-like control
Write a comment explaining that PWM switches the LED ON and OFF very fast to simulate different brightness levels by changing the duty_cycle.
Raspberry Pi
Hint
Think about how turning a light ON and OFF quickly can look like dimming.
4
Print the PWM duty cycle
Write a print statement to display the text "LED brightness set to 50%" using the duty_cycle variable.
Raspberry Pi
Hint
Use an f-string to include the duty_cycle variable inside the print text.
Practice
(1/5)
1. Why do we use PWM (Pulse Width Modulation) on a Raspberry Pi to control devices like LEDs instead of just turning them fully on or off?
easy
A. Because PWM is the only way to turn devices on.
B. Because PWM makes the Raspberry Pi run faster.
C. Because PWM simulates different power levels by switching on and off quickly, making devices appear dimmer or brighter.
D. Because PWM uses less electricity than turning devices fully on.
Solution
Step 1: Understand what PWM does
PWM switches power on and off very fast to create the effect of different power levels.
Step 2: Compare PWM to simple on/off
Simple on/off only gives full power or no power, so devices can't appear dim or partially powered.
Final Answer:
Because PWM simulates different power levels by switching on and off quickly, making devices appear dimmer or brighter. -> Option C
Quick Check:
PWM simulates analog power = Because PWM simulates different power levels by switching on and off quickly, making devices appear dimmer or brighter. [OK]
Hint: PWM controls brightness by fast on/off switching [OK]
Common Mistakes:
Thinking PWM makes the Pi run faster
Believing PWM is only for turning devices fully on
Assuming PWM always saves electricity
2. Which of the following is the correct way to start PWM on a Raspberry Pi GPIO pin using the RPi.GPIO library?
easy
A. pwm = GPIO.PWM(pin, frequency); pwm.start(duty_cycle)
B. pwm = GPIO.startPWM(pin, duty_cycle); pwm.setFrequency(frequency)
C. pwm = GPIO.PWMStart(pin, frequency, duty_cycle)
D. pwm = GPIO.beginPWM(pin); pwm.setDutyCycle(frequency)
Solution
Step 1: Recall RPi.GPIO PWM syntax
The correct syntax is to create a PWM object with GPIO.PWM(pin, frequency) and then start it with pwm.start(duty_cycle).
Step 2: Check each option's method names
Only pwm = GPIO.PWM(pin, frequency); pwm.start(duty_cycle) uses the correct method names and order as per RPi.GPIO documentation.
Final Answer:
pwm = GPIO.PWM(pin, frequency); pwm.start(duty_cycle) -> Option A
C. You forgot to call GPIO.cleanup() before starting PWM.
D. The duty cycle is set to 100%, so the LED is always fully on.
Solution
Step 1: Check the duty cycle value
The code uses pwm.start(100), which means 100% duty cycle (always on).
Step 2: Understand effect of 100% duty cycle
At 100% duty cycle, the LED receives full power constantly, so it stays fully on.
Final Answer:
The duty cycle is set to 100%, so the LED is always fully on. -> Option D
Quick Check:
100% duty cycle = LED fully on = The duty cycle is set to 100%, so the LED is always fully on. [OK]
Hint: 100% duty cycle means always on [OK]
Common Mistakes:
Thinking frequency 100 is too low to work
Believing GPIO.cleanup() is needed before PWM start
Assuming pin 18 can't do PWM
5. You want to control a motor speed smoothly using PWM on Raspberry Pi. The motor needs a voltage between 0V (off) and 5V (full speed). Which PWM setup will best simulate this analog voltage control?
hard
A. Use a low frequency (e.g., 1 Hz) PWM with fixed 50% duty cycle.
B. Use a high frequency (e.g., 1 kHz) PWM with varying duty cycle from 0% to 100%.
C. Use a high frequency PWM with fixed 100% duty cycle.
D. Use a low frequency PWM with duty cycle always 0%.
Solution
Step 1: Understand motor voltage control with PWM
Varying the duty cycle changes the average voltage the motor receives, controlling speed smoothly.
Step 2: Choose frequency and duty cycle range
High frequency PWM (like 1 kHz) avoids motor jerks and noise; duty cycle must vary from 0% (off) to 100% (full speed).
Final Answer:
Use a high frequency (e.g., 1 kHz) PWM with varying duty cycle from 0% to 100%. -> Option B
Quick Check:
High freq + variable duty = smooth motor speed [OK]
Hint: High freq PWM + variable duty = smooth analog control [OK]
Common Mistakes:
Using low frequency causes jerky motor movement
Fixing duty cycle prevents speed control
Using 0% or 100% duty cycle only gives off or full speed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.