Raspberry Pi - Structure du Code

Préparation du matériel

1×Raspberry Pi 5
1×Recommandé: Shield à bornier à vis pour Raspberry Pi
1×Recommandé: Kit plaque de base prototypage et plaque d'essai pour Raspberry Pi
1×Recommandé: Écran tactile HDMI pour Raspberry Pi

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.

Structure de base

Le code du Raspberry Pi se compose des parties suivantes :

  • Importer les bibliothèques nécessaires
  • Initialisation et configuration
  • Boucle principale : s'exécute de manière répétée, infiniment
  • Gestion des exceptions (optionnelle)
  • Fin du programme

Les deux squelettes de code :

  • Détourner du contenu fonctionnel ordinaire sans implication de logique, garantir l’adaptabilité, simplifier la maintenance.
# Import Required Libraries # Initialization and Setup # Perform one-time setup tasks here try: # Main Loop while True: # Main code logic goes here pass # Replace with your code except KeyboardInterrupt: # Handle Ctrl+C interruption print("\nExiting the program.") # Program Exit # Add any cleanup tasks or final actions here
  • Squelette de Code #2
# Import Required Libraries # Initialization and Setup # Perform one-time setup tasks here try: # Main Loop while True: # Main code logic goes here pass # Replace with your code except KeyboardInterrupt: # Handle Ctrl+C interruption print("\nExiting the program.") finally: # Program Exit # Add any cleanup tasks or final actions here

Exemple de code Raspberry Pi

Voici des exemples de codes qui font clignoter une LED.

  • Exemple de code pour le squelette n°1
# IMPORT REQUIRED LIBRARIES import RPi.GPIO as GPIO import time # INITIALIZATION AND SETUP # Set the GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Define the GPIO pin for the LED LED_PIN = 17 # Use GPIO pin 17 # Set up the LED pin as an output GPIO.setup(LED_PIN, GPIO.OUT) try: # MAIN LOOP while True: # Main code logic goes here # Turn on the LED GPIO.output(LED_PIN, GPIO.HIGH) # Wait for a second time.sleep(1) # Turn off the LED GPIO.output(LED_PIN, GPIO.LOW) # Wait for a second time.sleep(1) except KeyboardInterrupt: # Handle Ctrl+C interruption print("\nExiting the program.") GPIO.cleanup() # Clean up the GPIO
  • Exemple de code pour le squelette n°2
# IMPORT REQUIRED LIBRARIES import RPi.GPIO as GPIO import time # INITIALIZATION AND SETUP # Set the GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Define the GPIO pin for the LED LED_PIN = 17 # Use GPIO pin 17 # Set up the LED pin as an output GPIO.setup(LED_PIN, GPIO.OUT) try: # MAIN LOOP while True: # Main code logic goes here # Turn on the LED GPIO.output(LED_PIN, GPIO.HIGH) # Wait for a second time.sleep(1) # Turn off the LED GPIO.output(LED_PIN, GPIO.LOW) # Wait for a second time.sleep(1) except KeyboardInterrupt: # Exception Handling (Optional) # Handle Ctrl+C interruption print("\nExiting the program.") finally: # Program Exit # Cleanup GPIO on exit GPIO.cleanup()

※ NOS MESSAGES

  • N'hésitez pas à partager le lien de ce tutoriel. Cependant, veuillez ne pas utiliser notre contenu sur d'autres sites web. Nous avons investi beaucoup d'efforts et de temps pour créer ce contenu, veuillez respecter notre travail !