Raspberry Pi - Code-Struktur
Benötigte Hardware
| 1 | × | Raspberry Pi 5 | |
| 1 | × | (Empfohlen) Schraubklemmenblock-Shield für Raspberry Pi | |
| 1 | × | (Empfohlen) Raspberry Pi Prototyping-Grundplatte & Breadboard-Kit | |
| 1 | × | (Empfohlen) HDMI-Touchscreen-Monitor für Raspberry Pi |
Oder Sie können die folgenden Kits kaufen:
| 1 | × | DIYables Sensor-Kit (30 Sensoren/Displays) | |
| 1 | × | DIYables Sensor-Kit (18 Sensoren/Displays) |
Offenlegung: Einige der in diesem Abschnitt bereitgestellten Links sind Amazon-Affiliate-Links. Wir können eine Provision für Käufe erhalten, die über diese Links getätigt werden, ohne zusätzliche Kosten für Sie. Wir schätzen Ihre Unterstützung.
Grundstruktur
Der Raspberry Pi Code besteht aus folgenden Teilen:
- Import der erforderlichen Bibliotheken
- Initialisierung und Setup
- Hauptschleife: wird wiederholt und unendlich ausgeführt
- Ausnahmebehandlung (optional)
- Programmende
Es gibt zwei Code-Grundgerüste:
- Code-Grundgerüst #1
# 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
- Code-Grundgerüst #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
Raspberry Pi Beispielcode
Das Folgende bietet Beispielcodes, die eine LED blinken lassen
- Beispielcode für Grundgerüst #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
- Beispielcode für Grundgerüst #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()