Pulsing LED on the Raspberry Pi

FunduinoPublished on

Eine LED per PWM heller und dunkler werden lassen – mit Python und gpiozero

Eine LED, die langsam heller und wieder dunkler wird, zeigt das Prinzip der Pulsweitenmodulation besonders anschaulich.

Du lernst, wie PWM funktioniert, schließt die LED an einen GPIO-Pin an und steuerst ihre Helligkeit mit einem kurzen Python-Programm.

An LED with a series resistor connected to GPIO 23 of a Raspberry Pi

You need this

830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 4 parts together 8,33 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Brightness via pulse-width modulation

An LED should gradually brighten and dim – “fade” in English.

To change the brightness, you would actually need to vary the voltage. Instead, pulse-width modulation (PWM) is used: the voltage is switched on and off in the millisecond range. At a high PWM value, the signal is on almost continuously; at a low value, it is barely on at all. To the eye, it looks as if the LED is getting brighter or dimmer.

On the Raspberry Pi, you can use software PWM on all GPIO pins. There are also dedicated pins with hardware PWM.

The GPIO pins of the Raspberry Pi operate at 3.3 V and cannot tolerate 5 V.

Connect the LED

The LED connects through a 220-ohm resistor to GPIO 23 (Pin 16). The short leg connects to GND (Pin 6) via the breadboard’s negative rail.

The Raspberry Pi needs a power supply with a USB-C connector. A standard USB cable is not enough.

An LED with a series resistor connected to GPIO 23 of a Raspberry Pi

The program

Write the code on the Raspberry Pi in Thonny (Menu → Programming → Thonny), save it as pulsierende_led.py, and run it with the green arrow. The gpiozero library is already installed on Raspberry Pi OS.

The Helligkeit variable ranges from 0 (off) to 1 (full brightness). Each loop changes it by the value of Fadeschritte. When it reaches 1 or 0, Fadeschritte changes sign - the LED gets dimmer or brighter again. A smaller value in sleep() makes it pulse faster.

pulsating_led.py
# Include the library for the LED with PWM
from gpiozero import PWMLED
from time import sleep

LED = PWMLED(23)      # The LED is connected to GPIO 23 (Pin 16)

# Fade steps: determines the speed of the "fade".
FadeSteps = 0.02
# Brightness: the value output via PWM.
# 0 means off, 1 means full brightness. 0 is just an arbitrary starting value.
Brightness = 0

while True:
    # The LED is controlled at the current brightness here.
    LED.value = Brightness
    # Now the value is changed: the fade steps
    # are added to the previous brightness.
    Brightness = Brightness + FadeSteps
    # The LED should maintain its brightness only very briefly.
    # Reducing this value makes the pulsing faster.
    sleep(0.025)
    # As soon as the brightness reaches 1 or 0, the sign
    # of the fade steps changes. The LED then gets darker or brighter again.
    if Brightness >= 1:
        Brightness = 1
        FadeSteps = -FadeSteps
    if Brightness <= 0:
        Brightness = 0
        FadeSteps = -FadeSteps
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.