Raspberry Pi - Structure du Code

Préparation du matériel

1×Raspberry Pi 4 Model B
1×(Optional) Screw Terminal Block Shield for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B
1×(Optional) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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()

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!