Software PWM lets you control devices like LEDs by turning a pin on and off very fast. This makes the device look like it is dimmed or at different power levels.
You want to dim an LED connected to your Raspberry Pi.
You need to control the speed of a small motor using your Pi.
You want to create different brightness levels for a light without extra hardware.
You want to generate sound tones by turning a pin on and off quickly.
Syntax
Raspberry Pi
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_number, GPIO.OUT)
pwm = GPIO.PWM(pin_number, frequency)
pwm.start(duty_cycle)
# To change brightness or speed
pwm.ChangeDutyCycle(new_duty_cycle)
# To stop PWM
pwm.stop()
GPIO.cleanup()
pin_number is the GPIO pin you connect your device to.
frequency is how fast the pin switches on and off (in Hertz).
Examples
This example sets GPIO 18 to output PWM at 1000 Hz with half brightness for 2 seconds.
Raspberry Pi
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 1000) # 1000 Hz frequency
pwm.start(50) # 50% brightness
time.sleep(2)
pwm.stop()
GPIO.cleanup()
This example gradually increases brightness on GPIO 12 from 0% to 100% in steps.
Raspberry Pi
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12, 500) # 500 Hz frequency
pwm.start(0) # Start with LED offfor dc inrange(0, 101, 20):
pwm.ChangeDutyCycle(dc) # Increase brightness
time.sleep(1)
pwm.stop()
GPIO.cleanup()
Sample Program
This program smoothly increases and then decreases the brightness of an LED connected to GPIO 17 using software PWM.
Raspberry Pi
import RPi.GPIO as GPIO
import time
# Use BCM pin numbering
GPIO.setmode(GPIO.BCM)
# Set GPIO 17 as output
GPIO.setup(17, GPIO.OUT)
# Create PWM instance at 500 Hz
pwm = GPIO.PWM(17, 500)
# Start PWM with 0% duty cycle (off)
pwm.start(0)
try:
# Gradually increase brightnessfor duty_cycle inrange(0, 101, 10):
pwm.ChangeDutyCycle(duty_cycle)
print(f"Brightness: {duty_cycle}%")
time.sleep(0.5)
# Gradually decrease brightnessfor duty_cycle inrange(100, -1, -10):
pwm.ChangeDutyCycle(duty_cycle)
print(f"Brightness: {duty_cycle}%")
time.sleep(0.5)
finally:
pwm.stop()
GPIO.cleanup()
print("PWM stopped and GPIO cleaned up.")
OutputSuccess
Important Notes
Always call GPIO.cleanup() to reset pins after your program ends.
Software PWM is not perfect for very high precision or very fast signals.
Use try-finally to ensure PWM stops even if your program crashes.
Summary
Software PWM lets you control brightness or speed by switching pins on and off fast.
Use RPi.GPIO.PWM to create and control PWM on Raspberry Pi pins.
Always clean up GPIO pins after use to avoid issues.
Practice
(1/5)
1. What does Software PWM using RPi.GPIO.PWM allow you to control on a Raspberry Pi?
easy
A. The temperature of the Raspberry Pi CPU
B. The file system permissions on the Raspberry Pi
C. The network speed of the Raspberry Pi
D. The brightness of LEDs or speed of motors by switching pins on and off quickly
Solution
Step 1: Understand what PWM controls
PWM (Pulse Width Modulation) controls how long a pin stays on versus off, affecting devices like LEDs or motors.
Step 2: Identify the effect of switching pins fast
Switching pins on and off quickly changes brightness or speed smoothly.
Final Answer:
The brightness of LEDs or speed of motors by switching pins on and off quickly -> Option D
Quick Check:
Software PWM controls brightness/speed = B [OK]
Hint: PWM controls brightness or speed by fast switching [OK]
Common Mistakes:
Confusing PWM with CPU or network control
Thinking PWM changes file permissions
Assuming PWM controls temperature
2. Which of the following is the correct way to start PWM on pin 18 with a frequency of 1000 Hz using RPi.GPIO?
easy
A. pwm = GPIO.PWM(1000, 18); pwm.start(50)
B. pwm = GPIO.PWM(18); pwm.start(1000)
C. pwm = GPIO.PWM(18, 1000); pwm.start(50)
D. pwm = GPIO.PWM(18, 50); pwm.start(1000)
Solution
Step 1: Check PWM constructor parameters
The PWM constructor takes pin number first, then frequency in Hz, so GPIO.PWM(18, 1000) is correct.
Step 2: Verify start method usage
The start method takes duty cycle percentage, so pwm.start(50) starts PWM at 50% duty cycle.
Final Answer:
pwm = GPIO.PWM(18, 1000); pwm.start(50) -> Option C
Quick Check:
Pin, frequency order + start with duty cycle = A [OK]
Hint: PWM(pin, freq), then start(duty cycle) [OK]
Common Mistakes:
Swapping frequency and pin arguments
Passing duty cycle to PWM constructor
Using start() with frequency instead of duty cycle
3. What will be the output on the LED connected to pin 12 after running this code snippet?