Raspbian Wheezy: GPIO-Pin in Python als Ausgang programmieren
Das folgende Python-Skript zeigt beispielhaft wie Sie einen GPIO-Pin des Raspberry Pi's als Ausgang nutzen können. Das Skript initialisiert den GPIO-Pin als Ausgang und schaltet den Zustand des Ausgangs zyklisch nach einer Sekunde um. Die Ausführung des Skripts können Sie mit der Tastenkombination Strg+C beenden. Dadurch werden auch die Einstellungen für den GPIO-Pin wieder zurückgesetzt.
Für das folgende Python-Skript erstellen Sie mit einem Editor die Datei output.py.
pi@raspberrypi ~ $ vi output.py
Fügen Sie in diese Datei den folgenden Python-Code ein und speichern die Datei ab.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Creation: 27.08.2013
# Last Update: 07.04.2015
#
# Copyright (c) 2013-2015 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# import required modules
import RPi.GPIO as GPIO
import time
# define GPIO pin with connected LED
GPIOPin = 7
# main function
def main():
try:
# use GPIO pin numbering convention
GPIO.setmode(GPIO.BCM)
# set up GPIO pin for output
GPIO.setup(GPIOPin, GPIO.OUT)
while True:
# set GPIO pin to high
GPIO.output(GPIOPin, True)
# wait 1000ms
time.sleep(1.0)
# set GPIO pin to low
GPIO.output(GPIOPin, False)
# wait 1000ms
time.sleep(1.0)
# reset GPIO settings if user pressed Ctrl+C
except KeyboardInterrupt:
print("Execution stopped by user")
GPIO.cleanup()
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
#
# Creation: 27.08.2013
# Last Update: 07.04.2015
#
# Copyright (c) 2013-2015 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# import required modules
import RPi.GPIO as GPIO
import time
# define GPIO pin with connected LED
GPIOPin = 7
# main function
def main():
try:
# use GPIO pin numbering convention
GPIO.setmode(GPIO.BCM)
# set up GPIO pin for output
GPIO.setup(GPIOPin, GPIO.OUT)
while True:
# set GPIO pin to high
GPIO.output(GPIOPin, True)
# wait 1000ms
time.sleep(1.0)
# set GPIO pin to low
GPIO.output(GPIOPin, False)
# wait 1000ms
time.sleep(1.0)
# reset GPIO settings if user pressed Ctrl+C
except KeyboardInterrupt:
print("Execution stopped by user")
GPIO.cleanup()
if __name__ == '__main__':
main()
Nachdem Sie Das Python-Skript erstellt haben, ändern Sie die Rechte der Datei.
pi@raspberrypi ~ $ chmod 0755 output.py
Jetzt können Sie das Python-Skript wie folgt ausführen.
pi@raspberrypi ~ $ sudo ./output.py
Weiterführende Artikel
Raspbian Wheezy: GPIO-Pin in Python als Eingang programmieren
Raspbian Wheezy: GPIO-Pin in Python als PWM-Ausgang programmieren
Dieser Eintrag wurde am 27.08.2013 erstellt und zuletzt am 25.09.2016 bearbeitet.
Direkter Link zu dieser Seite: http://www.gtkdb.de/index_31_2305.html
[ Zur Startseite ] [ Zur Kategorie ]
© 2004-2021 by Georg Kainzbauer