Raspberry Pi Pico - Feu de circulation
Dans ce guide, nous vous montrerons comment gérer un module de feux de circulation avec un Raspberry Pi Pico. Nous discuterons de :
- Connexion d'un module de feux de circulation au Raspberry Pi Pico
- Programmation d'un Raspberry Pi Pico pour contrôler un module de feux de circulation
- Programmation d'un Raspberry Pi Pico pour contrôler un module de feux de circulation sans utiliser la fonction time.sleep()

Préparation du matériel
| 1 | × | Raspberry Pi Pico W | |
| 1 | × | Raspberry Pi Pico Alternativement: | |
| 1 | × | Câble Micro USB | |
| 1 | × | Module feu de circulation | |
| 1 | × | Fils de connexion | |
| 1 | × | Recommandé: Carte d'extension à bornier à vis pour Raspberry Pi Pico |
Ou vous pouvez acheter les kits suivants:
| 1 | × | Kit de Capteurs DIYables (30 capteurs/écrans) | |
| 1 | × | Kit de Capteurs DIYables (18 capteurs/écrans) |
Divulgation : Certains des liens fournis dans cette section sont des liens affiliés Amazon. Nous pouvons recevoir une commission pour tout achat effectué via ces liens, sans coût supplémentaire pour vous. Nous vous remercions de votre soutien.
À propos du module des feux de circulation
Schéma des broches
Le module de feux de circulation a quatre broches.
- Broche GND : Reliez cette broche de masse à la masse du Raspberry Pi Pico.
- Broche R : Cette broche commande la lumière rouge. Reliez-la à une sortie numérique du Raspberry Pi Pico.
- Broche Y : Cette broche commande la lumière jaune. Reliez-la à une sortie numérique du Raspberry Pi Pico.
- Broche G : Cette broche commande la lumière verte. Reliez-la à une sortie numérique du Raspberry Pi Pico.

Comment Ça Marche
Diagramme de câblage

Cette image a été créée avec Fritzing. Cliquez pour agrandir l'image.
Code du Raspberry Pi Pico
/*
* Ce code Raspberry Pi Pico a été développé par newbiely.fr
* Ce code Raspberry Pi Pico est mis à disposition du public sans aucune restriction.
* Pour des instructions complètes et des schémas de câblage, veuillez visiter:
* https://newbiely.fr/tutorials/raspberry-pico/raspberry-pi-pico-traffic-light
*/
import machine
import time
# Define pin numbers (you can change these to match your wiring)
PIN_RED = 19 # GPIO19 on Raspberry Pi Pico
PIN_YELLOW = 21 # GPIO21 on Raspberry Pi Pico
PIN_GREEN = 22 # GPIO22 on Raspberry Pi Pico
# Define times in seconds (MicroPython uses seconds for time.sleep)
RED_TIME = 4 # RED time in seconds
YELLOW_TIME = 4 # YELLOW time in seconds
GREEN_TIME = 4 # GREEN time in seconds
# Setup pins as output
red = machine.Pin(PIN_RED, machine.Pin.OUT)
yellow = machine.Pin(PIN_YELLOW, machine.Pin.OUT)
green = machine.Pin(PIN_GREEN, machine.Pin.OUT)
# Main loop
while True:
# Red light on
red.value(1) # turn on red
yellow.value(0) # turn off yellow
green.value(0) # turn off green
time.sleep(RED_TIME) # keep red light on for the defined period
# Yellow light on
red.value(0) # turn off red
yellow.value(1) # turn on yellow
green.value(0) # turn off green
time.sleep(YELLOW_TIME) # keep yellow light on for the defined period
# Green light on
red.value(0) # turn off red
yellow.value(0) # turn off yellow
green.value(1) # turn on green
time.sleep(GREEN_TIME) # keep green light on for the defined period
Étapes rapides
Veuillez suivre ces instructions étape par étape :
- Assurez-vous que l'IDE Thonny est installé sur votre ordinateur.
- Assurez-vous que le micrologiciel MicroPython est installé sur votre Raspberry Pi Pico.
- Si c'est la première fois que vous utilisez un Raspberry Pi Pico, reportez-vous au tutoriel Raspberry Pi Pico - Premiers pas. pour des instructions détaillées.
- Connectez le Raspberry Pi Pico au module de feu de circulation selon le schéma fourni.
- Connectez le Raspberry Pi Pico à votre ordinateur à l'aide d'un câble USB.
- Lancez l'IDE Thonny sur votre ordinateur.
- Dans l'IDE Thonny, sélectionnez l'interpréteur MicroPython (Raspberry Pi Pico) en accédant à Outils Options.
- Dans l'onglet Interpréteur, sélectionnez MicroPython (Raspberry Pi Pico) dans le menu déroulant.
- Assurez-vous que le port correct est sélectionné. L'IDE Thonny devrait détecter automatiquement le port, mais vous devrez peut-être le sélectionner manuellement (par exemple, COM3 sur Windows ou /dev/ttyACM0 sur Linux).
- Copiez le code ci-dessus et collez-le dans l'éditeur de l'IDE Thonny.
- Enregistrez le script sur votre Raspberry Pi Pico par :
- Cliquez sur le bouton Enregistrer, ou utilisez les touches Ctrl+S.
- Dans la boîte de dialogue d'enregistrement, vous verrez deux sections : Cet ordinateur et Raspberry Pi Pico. Sélectionnez Raspberry Pi Pico
- Enregistrez le fichier sous le nom main.py
- Cliquez sur le bouton vert Exécuter (ou appuyez sur F5) pour exécuter le script. Le script s'exécutera.
- Vérifiez l'état du feu de circulation.
Les feux de circulation fonctionnent de différentes manières, en fonction de leur conception dans chaque endroit. Voici une explication simple de la façon dont les feux de circulation régulent la circulation.
Le code montré ci-dessus vous permet de contrôler chaque lumière individuellement. Désormais, nous allons améliorer le code afin de le rendre plus efficace.
Optimisation du code pour Raspberry Pi Pico
- Améliorons le code en créant une fonction pour gérer la lumière.
/*
* Ce code Raspberry Pi Pico a été développé par newbiely.fr
* Ce code Raspberry Pi Pico est mis à disposition du public sans aucune restriction.
* Pour des instructions complètes et des schémas de câblage, veuillez visiter:
* https://newbiely.fr/tutorials/raspberry-pico/raspberry-pi-pico-traffic-light
*/
import machine
import time
# Define pin numbers (you can change these to match your wiring)
PIN_RED = 19 # GPIO19 on Raspberry Pi Pico
PIN_YELLOW = 21 # GPIO21 on Raspberry Pi Pico
PIN_GREEN = 22 # GPIO22 on Raspberry Pi Pico
# Define times in seconds (MicroPython uses seconds for time.sleep)
RED_TIME = 2 # RED time in seconds
YELLOW_TIME = 1 # YELLOW time in seconds
GREEN_TIME = 2 # GREEN time in seconds
# Define indexes
RED = 0
YELLOW = 1
GREEN = 2
# Setup pins as output and store them in a list
pins = [
machine.Pin(PIN_RED, machine.Pin.OUT),
machine.Pin(PIN_YELLOW, machine.Pin.OUT),
machine.Pin(PIN_GREEN, machine.Pin.OUT)
]
# Define the times array
times = [RED_TIME, YELLOW_TIME, GREEN_TIME]
def trafic_light_on(light):
for i in range(RED, GREEN + 1):
if i == light:
pins[i].value(1) # turn on
else:
pins[i].value(0) # turn off
# Main loop
while True:
# Red light on
trafic_light_on(RED)
time.sleep(times[RED]) # keep red light on during a period of time
# Yellow light on
trafic_light_on(YELLOW)
time.sleep(times[YELLOW]) # keep yellow light on during a period of time
# Green light on
trafic_light_on(GREEN)
time.sleep(times[GREEN]) # keep green light on during a period of time
- Nous pouvons améliorer le code en utilisant une boucle for.
/*
* Ce code Raspberry Pi Pico a été développé par newbiely.fr
* Ce code Raspberry Pi Pico est mis à disposition du public sans aucune restriction.
* Pour des instructions complètes et des schémas de câblage, veuillez visiter:
* https://newbiely.fr/tutorials/raspberry-pico/raspberry-pi-pico-traffic-light
*/
import machine
import time
# Define pin numbers (you can change these to match your wiring)
PIN_RED = 19 # GPIO19 on Raspberry Pi Pico
PIN_YELLOW = 21 # GPIO21 on Raspberry Pi Pico
PIN_GREEN = 22 # GPIO22 on Raspberry Pi Pico
# Define times in milliseconds (MicroPython can handle time in milliseconds with time.sleep_ms)
RED_TIME = 2000 # RED time in milliseconds
YELLOW_TIME = 1000 # YELLOW time in milliseconds
GREEN_TIME = 2000 # GREEN time in milliseconds
# Define indexes
RED = 0
YELLOW = 1
GREEN = 2
# Setup pins as output and store them in a list
pins = [
machine.Pin(PIN_RED, machine.Pin.OUT),
machine.Pin(PIN_YELLOW, machine.Pin.OUT),
machine.Pin(PIN_GREEN, machine.Pin.OUT)
]
# Define the times array
times = [RED_TIME, YELLOW_TIME, GREEN_TIME]
def trafic_light_on(light):
for i in range(RED, GREEN + 1):
if i == light:
pins[i].value(1) # turn on
else:
pins[i].value(0) # turn off
# Main loop
while True:
for light in range(RED, GREEN + 1):
trafic_light_on(light)
time.sleep_ms(times[light]) # keep light on during a period of time
- Améliorons le code en utilisant la fonction millis() plutôt que time.sleep().
/*
* Ce code Raspberry Pi Pico a été développé par newbiely.fr
* Ce code Raspberry Pi Pico est mis à disposition du public sans aucune restriction.
* Pour des instructions complètes et des schémas de câblage, veuillez visiter:
* https://newbiely.fr/tutorials/raspberry-pico/raspberry-pi-pico-traffic-light
*/
import machine
import time
# Define pin numbers (you can change these to match your wiring)
PIN_RED = 19 # GPIO19 on Raspberry Pi Pico
PIN_YELLOW = 21 # GPIO21 on Raspberry Pi Pico
PIN_GREEN = 22 # GPIO22 on Raspberry Pi Pico
# Define times in milliseconds (MicroPython can handle time in milliseconds with time.sleep_ms)
RED_TIME = 2000 # RED time in milliseconds
YELLOW_TIME = 1000 # YELLOW time in milliseconds
GREEN_TIME = 2000 # GREEN time in milliseconds
# Define indexes
RED = 0
YELLOW = 1
GREEN = 2
# Setup pins as output and store them in a list
pins = [
machine.Pin(PIN_RED, machine.Pin.OUT),
machine.Pin(PIN_YELLOW, machine.Pin.OUT),
machine.Pin(PIN_GREEN, machine.Pin.OUT)
]
# Define the times array
times = [RED_TIME, YELLOW_TIME, GREEN_TIME]
# Initialize variables
last_time = time.ticks_ms()
light = RED # start with RED light
def trafic_light_on(light):
for i in range(RED, GREEN + 1):
if i == light:
pins[i].value(1) # turn on
else:
pins[i].value(0) # turn off
# Initialize the first light
trafic_light_on(light)
# Main loop
while True:
current_time = time.ticks_ms()
if time.ticks_diff(current_time, last_time) > times[light]:
light += 1
if light >= 3:
light = RED # reset to RED for a new cycle
trafic_light_on(light)
last_time = current_time
# TO DO: your other code