🛠 Premier script Python : faire clignoter la LED

Raspberry Pi Model B+ (2014) — Board Sheet · 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.

Objectives

Issue commands to programs: write, run, and cleanly stop a first Python 3 script that blinks the LED from tutorial 1, using the RPi.GPIO library still provided on legacy images (ARMv6).

Hardware

Complete code

# 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

Expected verification

The LED blinks 10 times (0.3 s on / 0.3 s off) then the script terminates without any message. Ctrl+C interrupts the program and still triggers GPIO.cleanup thanks to the finally block.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials