🛠 Premier script Python sur Raspbian legacy

Fiche carte — Raspberry Pi Model B Revision 2 (2012) · 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

Objectifs

Écrire, rendre exécutable et lancer un premier script Python complet sur le Model B Rev 2 : affichage texte, boucle, et pilotage d'une LED avec RPi.GPIO, la bibliothèque standard de l'ère Raspbian.

Matériel

Code complet

#!/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.")

Vérification attendue

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

Le terminal affiche le message puis les dix itérations ; la LED clignote une seconde par cycle ; le script s'arrête seul et éteint la LED.

Erreurs fréquentes

Autres tutoriels pour cette carte

Exemples de code pour cette carte

← Retour à la fiche 🛠 Tous les tutoriels