🛠 Premier script Python : faire clignoter la LED

Raspberry Pi Model B+ (2014) — Hoja de placa · debutant · Schéma : IMG-model-b-plus-debutant-3-modelbp-premier-python.svg — même LED que le tuto 1 sur GPIO17 (broche 11) avec résistance 330 Ω et GND broche 14, éditeur de texte et terminal lançant blink.py.

Objetivos

Enviar órdenes a los programas: escribir, ejecutar y detener correctamente un primer script de Python 3 que hace parpadear el LED del tutorial 1, utilizando la biblioteca RPi.GPIO aún incluida en las imágenes legacy (ARMv6).

Material

Código completo

# Installation de la bibliothèque GPIO pour Python 3
sudo apt update && sudo apt install -y python3-rpi.gpio
nano blink.py   # coller le script ci-dessous, puis Ctrl+O, Ctrl+X
sudo python3 blink.py
#!/usr/bin/env python3
"""Clignotant LED sur GPIO17 (broche 11) — Raspberry Pi Model B+."""
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)          # on raisonne en numérotation BCM
GPIO.setup(17, GPIO.OUT)
try:
    for _ in range(10):         # 10 clignotements
        GPIO.output(17, GPIO.HIGH)
        time.sleep(0.3)
        GPIO.output(17, GPIO.LOW)
        time.sleep(0.3)
finally:
    GPIO.cleanup              # rend la broche propre à la sortie

Verificación esperada

El LED parpadea 10 veces (0,3 s encendido / 0,3 s apagado) y luego el script termina sin mensajes. Ctrl+C interrumpe el programa y, aun así, ejecuta GPIO.cleanup gracias al bloque finally.

Errores frecuentes

Otros tutoriales para esta placa

Ejemplos de código para esta placa

← Volver a la ficha 🛠 Todos los tutoriales