0
0
Raspberry Piprogramming~5 mins

PWMLED for brightness in Raspberry Pi

Choose your learning style9 modes available
Introduction

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.

You want to make a light slowly get brighter or dimmer, like a sunrise alarm clock.
You need to save power by dimming LEDs instead of turning them fully on.
You want to show different brightness levels for status lights on a device.
You want to create smooth light effects for decorations or indicators.
Syntax
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.

Examples
This sets the LED on GPIO 17 to half brightness.
Raspberry Pi
from gpiozero import PWMLED
led = PWMLED(17)
led.value = 0.5
This turns the LED on GPIO 18 fully on (maximum brightness).
Raspberry Pi
from gpiozero import PWMLED
led = PWMLED(18)
led.value = 1
This turns the LED on GPIO 22 completely off.
Raspberry Pi
from gpiozero import PWMLED
led = PWMLED(22)
led.value = 0
Sample Program

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.

Raspberry Pi
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()
OutputSuccess
Important Notes

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.

Summary

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.