0
0
Raspberry Piprogramming~15 mins

PWMLED for brightness in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
PWMLED for brightness
📖 Scenario: You have a Raspberry Pi connected to an LED through a PWM pin. You want to control the brightness of the LED by changing the PWM duty cycle.
🎯 Goal: Build a simple program that sets up a PWM LED on pin 18 and changes its brightness by setting the PWM value.
📋 What You'll Learn
Create a PWMLED object on pin 18
Set a brightness level variable
Use the brightness variable to set the LED brightness
Print the brightness value
💡 Why This Matters
🌍 Real World
Controlling LED brightness is common in home automation, indicators, and lighting projects using Raspberry Pi.
💼 Career
Understanding PWM control of LEDs is useful for hardware interfacing and embedded systems programming.
Progress0 / 4 steps
1
Set up the PWMLED on pin 18
Import PWMLED from gpiozero and create a PWMLED object called led on pin 18.
Raspberry Pi
Need a hint?

Use from gpiozero import PWMLED to import the class.

Create the LED with led = PWMLED(18).

2
Create a brightness variable
Create a variable called brightness and set it to 0.5 to represent half brightness.
Raspberry Pi
Need a hint?

Set brightness = 0.5 to represent 50% brightness.

3
Set the LED brightness using the brightness variable
Set the value property of led to the brightness variable.
Raspberry Pi
Need a hint?

Use led.value = brightness to set the LED brightness.

4
Print the brightness value
Print the text "Brightness level:" followed by the brightness variable.
Raspberry Pi
Need a hint?

Use print(f"Brightness level: {brightness}") to show the brightness.