0
0
Raspberry Piprogramming~30 mins

LED brightness control in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
LED brightness control
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(f"LED brightness is set to: {brightness}") to show the brightness level.