📖 Scenario: You have a Raspberry Pi connected to an LED light. You want to control how bright the LED is by changing its brightness level.
🎯 Goal: Build a simple program that sets up the LED pin, defines a brightness level, changes the LED brightness using PWM, and then shows the brightness level.
📋 What You'll Learn
Use the RPi.GPIO library to control the LED
Set up the GPIO pin 18 for PWM output
Define a brightness level between 0 and 100
Use PWM to set the LED brightness
Print the brightness level to the screen
💡 Why This Matters
🌍 Real World
Controlling LED brightness is useful in projects like mood lighting, indicators, or displays.
💼 Career
Understanding GPIO and PWM control is important for hardware programming and embedded systems jobs.
Progress0 / 4 steps
1
Set up GPIO and LED 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. Use GPIO.setup(18, GPIO.OUT) to prepare pin 18 for output.
2
Create PWM instance and set brightness level
Create a PWM instance called pwm on pin 18 with frequency 1000 Hz. Define a variable called brightness and set it to 75.
Raspberry Pi
Hint
Use GPIO.PWM(18, 1000) to create PWM on pin 18 at 1000 Hz. Set brightness to 75 to represent 75% brightness.
3
Start PWM and set duty cycle
Start the PWM with a duty cycle of 0. Then change the duty cycle to the value of brightness to set the LED brightness.
Raspberry Pi
Hint
Use pwm.start(0) to start PWM with 0% brightness, then pwm.ChangeDutyCycle(brightness) to set brightness to 75%.
4
Print the brightness level
Print the message "LED brightness is set to: " followed by the value of brightness.
Raspberry Pi
Hint
Use print(f"LED brightness is set to: {brightness}") to show the brightness level.
Practice
(1/5)
1. What does PWM stand for in the context of controlling LED brightness on a Raspberry Pi?
easy
A. Pulse Wire Mode
B. Power Wave Management
C. Pin Wiring Method
D. Pulse Width Modulation
Solution
Step 1: Understand PWM meaning
PWM is a technique used to control power by switching it on and off quickly.
Step 2: Match PWM to full form
Pulse Width Modulation means changing the width of the pulse to control power, which fits LED brightness control.
Final Answer:
Pulse Width Modulation -> Option D
Quick Check:
PWM = Pulse Width Modulation [OK]
Hint: PWM controls brightness by changing pulse width [OK]
Common Mistakes:
Confusing PWM with power management
Thinking PWM is a wiring method
Mixing up acronyms
2. Which of the following is the correct way to start PWM on GPIO pin 18 with a frequency of 1000 Hz using RPi.GPIO?
easy
A. pwm = GPIO.PWM(18, 1000); pwm.start(0)
B. pwm = GPIO.PWM(18); pwm.start(1000)
C. pwm = GPIO.PWM(1000, 18); pwm.start(0)
D. pwm = GPIO.PWM(18, 0); pwm.start(1000)
Solution
Step 1: Check PWM constructor parameters
RPi.GPIO PWM takes pin number first, then frequency in Hz.
Step 2: Verify start method usage
start() takes initial duty cycle (0-100), so start(0) is correct for off initially.
Final Answer:
pwm = GPIO.PWM(18, 1000); pwm.start(0) -> Option A
Quick Check:
GPIO.PWM(pin, freq) then start(duty) [OK]
Hint: PWM(pin, frequency) then start(duty cycle) [OK]
Common Mistakes:
Swapping frequency and pin
Starting PWM with frequency instead of duty
Using zero frequency
3. What will be the output brightness level of the LED after running this code snippet?