PWMLED lets you control how bright a light is by turning it on and off very fast. This makes the light look dimmer or brighter without changing the bulb.
PWMLED for brightness in Raspberry Pi
from gpiozero import PWMLED led = PWMLED(pin_number) led.value = brightness_level # brightness_level is a number from 0 (off) to 1 (full brightness)
The pin_number is the GPIO pin connected to the LED.
The value property controls brightness: 0 means off, 1 means fully on, and numbers in between set dim levels.
from gpiozero import PWMLED led = PWMLED(17) led.value = 0.5
from gpiozero import PWMLED led = PWMLED(18) led.value = 1
from gpiozero import PWMLED led = PWMLED(22) led.value = 0
This program changes the LED brightness on GPIO 17 from off to full brightness in steps. It waits 1 second between each change and prints the current brightness level.
from gpiozero import PWMLED from time import sleep led = PWMLED(17) for brightness in [0, 0.25, 0.5, 0.75, 1]: led.value = brightness print(f"LED brightness set to {brightness}") sleep(1) led.off()
Make sure your LED is connected with a resistor to avoid damage.
Not all GPIO pins support PWM; check your Raspberry Pi model's pinout.
Using PWMLED is better than turning the LED on/off quickly yourself because it handles timing smoothly.
PWMLED controls LED brightness by changing how much time the LED is on versus off very fast.
You set brightness with a number from 0 (off) to 1 (full brightness).
This is useful for dimming lights or creating smooth light effects.