📖 Scenario: You have a Raspberry Pi and want to control the brightness of an LED using software PWM (Pulse Width Modulation). This means turning the LED on and off very quickly to simulate different brightness levels.We will use the RPi.GPIO library to control the GPIO pin connected to the LED.
🎯 Goal: Create a Python program that uses software PWM with RPi.GPIO to control the brightness of an LED connected to GPIO pin 18.
📋 What You'll Learn
Use the RPi.GPIO library to control GPIO pins
Set up GPIO pin 18 as an output
Create a software PWM instance on GPIO pin 18 with a frequency of 100 Hz
Start PWM with 0% duty cycle
Change the duty cycle to 50%
Stop PWM and clean up GPIO at the end
💡 Why This Matters
🌍 Real World
Software PWM is used to control devices like LEDs, motors, and buzzers by simulating analog output using digital pins.
💼 Career
Understanding PWM and GPIO control is essential for embedded systems, robotics, and IoT device programming.
Progress0 / 4 steps
1
Set up GPIO and pin
Import the RPi.GPIO library as GPIO. Set the GPIO mode to GPIO.BCM. Set up GPIO pin 18 as an output pin.
Raspberry Pi
Hint
Use GPIO.setmode(GPIO.BCM) to select pin numbering by BCM. Use GPIO.setup(18, GPIO.OUT) to set pin 18 as output.
2
Create PWM instance
Create a PWM instance called pwm on GPIO pin 18 with a frequency of 100 Hz.
Raspberry Pi
Hint
Use GPIO.PWM(pin, frequency) to create a PWM object.
3
Start PWM with 0% duty cycle and change to 50%
Start the PWM on pwm with a duty cycle of 0 percent. Then change the duty cycle to 50 percent.
Raspberry Pi
Hint
Use pwm.start(0) to start PWM with 0% brightness. Use pwm.ChangeDutyCycle(50) to set brightness to 50%.
4
Stop PWM and clean up GPIO
Stop the PWM on pwm and clean up the GPIO pins using GPIO.cleanup(). Then print "PWM stopped and GPIO cleaned up".
Raspberry Pi
Hint
Use pwm.stop() to stop PWM and GPIO.cleanup() to reset GPIO pins.
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?