🛠 Premier script Python sur Raspbian legacy

Raspberry Pi Model B Revision 2 (2012) — Fiche carte · debutant · Schéma : IMG-model-b-rev2-debutant-3-modelb2-premier-script-python.svg — Écran HDMI connecté au Model B Rev 2, éditeur de texte et terminal affichant un script Python, LED clignotante sur la carte

Objetivos

Escribir, hacer ejecutable y lanzar un primer script Python completo en el Model B Rev 2: visualización de texto, bucle y control de un LED con RPi.GPIO, la biblioteca estándar de la era Raspbian.

Material

Código completo

#!/usr/bin/env python
# hello_led.py - premier script: bonjour + LED qui clignote sur GPIO17
import RPi.GPIO as GPIO
import time

LED = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)
print("Bonjour depuis le Raspberry Pi Model B Rev 2 (BCM2835, 512 MB)")
try:
    for i in range(10):
        GPIO.output(LED, GPIO.HIGH)
        print("iteration %d" % (i + 1))
        time.sleep(0.5)
        GPIO.output(LED, GPIO.LOW)
        time.sleep(0.5)
finally:
    GPIO.cleanup
print("Termine.")

Verificación esperada

nano hello_led.py
chmod +x hello_led.py
sudo ./hello_led.py

La terminal muestra el mensaje y luego las diez iteraciones; el LED parpadea un segundo por ciclo; el script se detiene solo y apaga el LED.

Errores frecuentes

Otros tutoriales para esta placa

Ejemplos de código para esta placa

← Volver a la ficha 🛠 Todos los tutoriales