Pulsing LED on the Raspberry Pi

FunduinoLast updated 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

Raspberry Pi 4 Model B - 2 GB RAM, 1,5 GHz, WLAN, gebraucht Raspberry Pi 4 Model B - 2 GB RAM, 1,5 GHz, WLAN, gebraucht 44,90 € *Item no. F23110180About the product — Raspberry Pi 4 Model B - 2 GB RAM, 1,5 GHz, WLAN, gebraucht
Breadboard 830 Kontakte - Steckbrett für Arduino, lötfrei Breadboard 830 Kontakte - Steckbrett für Arduino, lötfrei 2,90 € *Item no. FUN-1010553About the product — Breadboard 830 Kontakte - Steckbrett für Arduino, lötfrei
Breadboardkabel männlich-männlich - 40 Stück, 20 cm Breadboardkabel männlich-männlich - 40 Stück, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboardkabel männlich-männlich - 40 Stück, 20 cm
LED 5 mm - 100 Stück, 5 Farben, 2 V LED 5 mm - 100 Stück, 5 Farben, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Stück, 5 Farben, 2 V
Widerstandssortiment 100 Stück - 5 Werte 100 Ω bis 10 kΩ, 0,5 W Widerstandssortiment 100 Stück - 5 Werte 100 Ω bis 10 kΩ, 0,5 W 1,72 € *Item no. F23107216About the product — Widerstandssortiment 100 Stück - 5 Werte 100 Ω bis 10 kΩ, 0,5 W

All 5 parts together 53,23 € *

* 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 article explains PWM with 5 V (“5V, when the LED is brightly lit”). The Raspberry Pi’s GPIO pins 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 parts list says “Raspberry Pi with a suitable USB cable,” but the link points to a used Raspberry Pi 4. The Pi needs a power supply with a USB-C connector to operate, not just a USB cable. The article heading also calls the Raspberry Pi a microcontroller, but it is a single-board computer.

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.