📖 Scenario: You are working on a Raspberry Pi project that controls the brightness of an LED using PWM (Pulse Width Modulation). You want to understand how changing the PWM frequency and duty cycle affects the LED's brightness.
🎯 Goal: Build a simple Python program that sets up PWM on a GPIO pin, changes the frequency and duty cycle, and prints the current settings to observe their relationship.
📋 What You'll Learn
Create a variable for the GPIO pin number used for PWM
Create a variable for the initial PWM frequency
Create a variable for the initial duty cycle
Use a loop to change the duty cycle from 0 to 100 in steps
Print the current frequency and duty cycle values
💡 Why This Matters
🌍 Real World
PWM is used in many electronics projects to control LED brightness, motor speed, and other devices by adjusting the power delivered.
💼 Career
Understanding PWM is important for embedded systems, robotics, and hardware interfacing roles where precise control of electronic components is required.
Progress0 / 4 steps
1
Set up PWM pin and initial values
Create a variable called pwm_pin and set it to 18. Create a variable called frequency and set it to 1000. Create a variable called duty_cycle and set it to 0.
Raspberry Pi
Hint
Use simple variable assignments for pwm_pin, frequency, and duty_cycle.
2
Import RPi.GPIO and set up PWM
Import the RPi.GPIO module as GPIO. Set the GPIO mode to GPIO.BCM. Set up pwm_pin as an output pin. Create a PWM object called pwm on pwm_pin with the frequency variable frequency. Start PWM with the duty cycle variable duty_cycle.
Raspberry Pi
Hint
Remember to import the GPIO library and set the pin mode before starting PWM.
3
Change duty cycle in a loop
Use a for loop with variable dc to go from 0 to 100 in steps of 20. Inside the loop, change the PWM duty cycle to dc using pwm.ChangeDutyCycle(dc). Print the current frequency and duty cycle using print(f"Frequency: {frequency} Hz, Duty Cycle: {dc}%").
Raspberry Pi
Hint
Use a for loop with range(0, 101, 20) and update the duty cycle inside the loop.
4
Clean up and stop PWM
After the loop, stop the PWM using pwm.stop(). Clean up the GPIO pins using GPIO.cleanup(). Print "PWM test complete." to show the program finished.
Raspberry Pi
Hint
Remember to stop PWM and clean up GPIO pins to avoid warnings. Then print the completion message.
Practice
(1/5)
1. What does the duty cycle in PWM control on a Raspberry Pi?
easy
A. The percentage of time the signal is ON during one cycle
B. The speed at which the signal pulses repeat
C. The voltage level of the signal
D. The total duration of the PWM signal
Solution
Step 1: Understand duty cycle meaning
The duty cycle is the fraction of one cycle when the signal is ON (high).
Step 2: Differentiate from frequency
Frequency controls how fast cycles repeat, not how long signal stays ON.
Final Answer:
The percentage of time the signal is ON during one cycle -> Option A
Quick Check:
Duty cycle = ON time percentage [OK]
Hint: Duty cycle = ON time percentage in each pulse [OK]
Common Mistakes:
Confusing duty cycle with frequency
Thinking duty cycle controls voltage
Assuming duty cycle affects total signal duration
2. Which of the following is the correct Python syntax to set a PWM frequency to 1000 Hz on a Raspberry Pi GPIO pin using RPi.GPIO?
easy
A. pwm = GPIO.PWM(pin, 1000)
B. pwm = GPIO.setPWM(pin, 1000)
C. pwm = GPIO.PWM(pin).frequency(1000)
D. pwm = GPIO.frequency(pin, 1000)
Solution
Step 1: Recall RPi.GPIO PWM syntax
The correct way to create a PWM instance is GPIO.PWM(pin, frequency).
Step 2: Check options for correct method and parameters
pwm = GPIO.PWM(pin, 1000) matches the correct syntax; others use invalid methods or chaining.
Final Answer:
pwm = GPIO.PWM(pin, 1000) -> Option A
Quick Check:
GPIO.PWM(pin, freq) creates PWM [OK]
Hint: Use GPIO.PWM(pin, frequency) to set PWM frequency [OK]
Common Mistakes:
Using non-existent methods like setPWM or frequency()
Trying to set frequency after creating PWM without parameters
Confusing method chaining syntax
3. Consider this Python code snippet controlling an LED brightness with PWM on Raspberry Pi:
A. ChangeFrequency is not a valid method; use pwm.ChangeFrequency(2000)
B. ChangeFrequency method name is incorrect; use pwm.ChangeFrequency(2000)
C. Frequency cannot be changed after PWM.start(); recreate PWM object
D. Method name is case sensitive; use pwm.ChangeFrequency(2000)
Solution
Step 1: Identify method name case sensitivity
RPi.GPIO does not provide a method to change frequency after PWM object creation.
Step 2: Correct approach to change frequency
To change frequency, stop PWM and create a new PWM instance with the new frequency.
Final Answer:
Frequency cannot be changed after PWM.start(); recreate PWM object -> Option C
Quick Check:
RPi.GPIO PWM frequency fixed at creation [OK]
Hint: To change frequency, recreate PWM object [OK]
Common Mistakes:
Using wrong method name spelling
Ignoring case sensitivity in Python
Trying to change frequency by creating new PWM instance
5. You want to control a motor speed using PWM on Raspberry Pi. The motor requires a minimum frequency of 1000 Hz for smooth operation. If you set frequency to 1000 Hz and duty cycle to 20%, what happens if you increase frequency to 2000 Hz but keep duty cycle at 20%?
hard
A. Motor speed doubles because frequency doubled
B. Motor speed stays roughly the same because duty cycle is unchanged
C. Motor stops because frequency is too high
D. Motor speed halves because frequency doubled
Solution
Step 1: Understand frequency vs duty cycle effect on motor speed
Motor speed depends mainly on duty cycle (power ON time), not frequency.
Step 2: Analyze frequency change with constant duty cycle
Increasing frequency while keeping duty cycle constant keeps average power same, so speed stays similar.
Final Answer:
Motor speed stays roughly the same because duty cycle is unchanged -> Option B
Quick Check:
Speed ~ duty cycle, frequency affects smoothness [OK]
Hint: Motor speed depends on duty cycle, not frequency [OK]
Common Mistakes:
Assuming frequency change affects speed directly
Thinking doubling frequency doubles speed
Ignoring duty cycle's role in power delivery
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.