GPIO LED-Steuerung mit dem Raspberry Pi
Lerne, wie du eine LED über die GPIO-Pins deines Raspberry Pi anschließt und mit der Kommandozeile sowie Python steuerst.
Learn how to connect an LED to the GPIO pins of your Raspberry Pi and control it using the command line and Python.
Warnung – Verkabelung
Warning – Wiring
Schließe niemals eine LED ohne Vorwiderstand direkt an die GPIO-Pins an! Ohne Widerstand fließt zu viel Strom, was sowohl die LED als auch den GPIO-Pin oder den gesamten Raspberry Pi zerstören kann. Verwende immer einen Widerstand (220Ω–330Ω). Überprüfe deine Verkabelung sorgfältig, bevor du den Pi einschaltest. Trenne den Pi immer vom Strom, bevor du Kabel änderst.
Never connect an LED directly to the GPIO pins without a resistor! Without a resistor, too much current will flow, which can destroy both the LED and the GPIO pin or even the entire Raspberry Pi. Always use a resistor (220Ω–330Ω). Carefully check your wiring before powering on the Pi. Always disconnect the Pi from power before changing any wires.
GPIO-Pin-Layout verstehen
Der Raspberry Pi verfügt über einen 40-Pin GPIO-Header (General Purpose Input/Output). Diese Pins ermöglichen es dir, elektronische Bauteile wie LEDs, Sensoren und Motoren direkt an den Pi anzuschließen.
The Raspberry Pi has a 40-pin GPIO header (General Purpose Input/Output). These pins allow you to connect electronic components like LEDs, sensors and motors directly to the Pi.
Wichtige Pin-Typen:
Important pin types:
3.3V (Pin 1, 17) – Spannungsversorgung 3,3 Volt
3.3V (Pin 1, 17) – 3.3 volt power supply
5V (Pin 2, 4) – Spannungsversorgung 5 Volt
5V (Pin 2, 4) – 5 volt power supply
GND (Pin 6, 9, 14, 20, 25, 30, 34, 39) – Masse/Ground
GND (Pin 6, 9, 14, 20, 25, 30, 34, 39) – Ground
GPIO – Programmierbare Ein-/Ausgangs-Pins
GPIO – Programmable input/output pins
Zeige das GPIO-Layout mit folgendem Befehl an:
Display the GPIO layout with the following command:
pinout
Tipp
Es gibt zwei Nummerierungssysteme: BCM (Broadcom-Nummern, z.B. GPIO17) und BOARD (physische Pin-Position, z.B. Pin 11). In diesem Tutorial verwenden wir BCM-Nummern.
There are two numbering systems: BCM (Broadcom numbers, e.g. GPIO17) and BOARD (physical pin position, e.g. Pin 11). In this tutorial, we use BCM numbers.
LED und Widerstand verkabeln
Für dieses Projekt benötigst du folgende Bauteile:
For this project you need the following components:
1x LED (beliebige Farbe)
1x LED (any color)
1x Widerstand 220Ω oder 330Ω
1x resistor 220Ω or 330Ω
2x Jumper-Kabel (Male-to-Female)
2x jumper wires (male-to-female)
1x Breadboard (optional, aber empfohlen)
1x breadboard (optional but recommended)
Schaltungsaufbau:
Circuit setup:
Verbinde GPIO17 (Pin 11) über ein Jumper-Kabel mit einer Reihe auf dem Breadboard
Connect GPIO17 (Pin 11) via a jumper wire to a row on the breadboard
Setze den 220Ω-Widerstand in dieselbe Reihe wie das Kabel und in eine benachbarte Reihe
Place the 220Ω resistor in the same row as the wire and into an adjacent row
Setze die LED so ein, dass das längere Bein (Anode, +) in der Reihe mit dem Widerstand ist
Place the LED so that the longer leg (anode, +) is in the row with the resistor
Verbinde das kürzere Bein (Kathode, –) der LED mit GND (Pin 6) über ein Jumper-Kabel
Connect the shorter leg (cathode, –) of the LED to GND (Pin 6) via a jumper wire
Zusammenfassung der Verbindungen:
Connection summary:
GPIO17 (Pin 11) ---[ 220Ω ]---[LED +]---[LED -]--- GND (Pin 6)
LED über die Kommandozeile steuern
Du kannst GPIO-Pins direkt über das Dateisystem steuern, ohne zusätzliche Software zu installieren. Hier nutzen wir das sysfs-Interface:
You can control GPIO pins directly through the filesystem without installing any additional software. Here we use the sysfs interface:
GPIO-Pin aktivieren (exportieren):
Activate (export) GPIO pin:
echo 17 | sudo tee /sys/class/gpio/export
Pin als Ausgang konfigurieren:
Configure pin as output:
echo out | sudo tee /sys/class/gpio/gpio17/direction
LED einschalten:
Turn LED on:
echo 1 | sudo tee /sys/class/gpio/gpio17/value
LED ausschalten:
Turn LED off:
echo 0 | sudo tee /sys/class/gpio/gpio17/value
Pin wieder freigeben (unexport):
Release (unexport) pin:
echo 17 | sudo tee /sys/class/gpio/unexport
LED mit Python steuern (RPi.GPIO)
Für fortgeschrittene Steuerung verwenden wir die Python-Bibliothek RPi.GPIO. Diese ist auf Raspberry Pi OS vorinstalliert. Falls nicht, installiere sie mit:
For advanced control, we use the Python library RPi.GPIO. It comes pre-installed on Raspberry Pi OS. If not, install it with:
sudo apt install python3-rpi.gpio -y
Erstelle eine neue Python-Datei:
Create a new Python file:
nano led_blink.py
Füge folgenden Code ein – die LED blinkt 10 Mal im Sekundentakt:
Paste the following code – the LED will blink 10 times at one-second intervals:
import RPi.GPIO as GPIO
import time
# BCM-Nummerierung verwenden
GPIO.setmode(GPIO.BCM)
# GPIO17 als Ausgang konfigurieren
LED_PIN = 17
GPIO.setup(LED_PIN, GPIO.OUT)
try:
# LED 10 Mal blinken lassen
for i in range(10):
GPIO.output(LED_PIN, GPIO.HIGH) # LED an
print(f"LED AN (Durchlauf {i + 1}/10)")
time.sleep(1)
GPIO.output(LED_PIN, GPIO.LOW) # LED aus
print(f"LED AUS (Durchlauf {i + 1}/10)")
time.sleep(1)
print("Fertig!")
except KeyboardInterrupt:
print("\nAbgebrochen durch Benutzer.")
finally:
# GPIO-Pins zuruecksetzen
GPIO.cleanup()
print("GPIO aufgeraeumt.")
Speichere die Datei (Ctrl+O, dann Enter, dann Ctrl+X) und führe sie aus:
Save the file (Ctrl+O, then Enter, then Ctrl+X) and run it:
sudo python3 led_blink.py
Die LED sollte nun 10 Mal blinken. Du kannst das Skript jederzeit mit Ctrl+C abbrechen – der finally-Block sorgt dafür, dass die GPIO-Pins sicher zurückgesetzt werden.
The LED should now blink 10 times. You can stop the script at any time with Ctrl+C – the finally block ensures that the GPIO pins are safely reset.
Tipp
Rufe immer GPIO.cleanup() am Ende deines Skripts auf. Das setzt alle GPIO-Pins zurück und verhindert Warnungen beim nächsten Ausführen. Nutze dafür einen try/finally-Block wie im Beispiel oben.
Always call GPIO.cleanup() at the end of your script. This resets all GPIO pins and prevents warnings on the next run. Use a try/finally block as shown in the example above.