🛠 Premier script Python sur Raspbian legacy

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

Objectives

Write, make executable, and run a first complete Python script on the Model B Rev 2: text display, loop, and LED control using RPi.GPIO, the standard library from the Raspbian era.

Hardware

Complete code

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

Expected verification

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

The terminal displays the message followed by the ten iterations; the LED blinks for one second per cycle; the script stops on its own and turns off the LED.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials