raspberry:raspberry_gpio_first_steps
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende Überarbeitung | |||
raspberry:raspberry_gpio_first_steps [2015/02/02 09:46] – gpipperr | raspberry:raspberry_gpio_first_steps [2015/02/02 20:14] (aktuell) – [Eine LED ein und ausschalten über einen Taster] gpipperr | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ===== Raspberry Pi Model B - GPIO Port einsetzen - erste Schritte ===== | ||
+ | ** Raspberry Pi Model B – Pidora Linux**\\ | ||
+ | Als OS verwende ich die die Pidora Distribution, | ||
+ | Daher gegeben sich anscheinend kleinere Unterschied in Pfaden wie zum Beispiel im /sys . | ||
+ | |||
+ | ==== Pin Belegung ==== | ||
+ | Pin Belegung im Detail | ||
+ | |||
+ | Gesamtanzahl aller GPIO Pins (nur 17 davon können genützt werden): | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | Pro Output Port können maximal 16mA, alle Pins zusammen höchstens 51mA Strom fließen. | ||
+ | |||
+ | Siehe auch: http:// | ||
+ | |||
+ | ====Abfragen der Ports über /sys ==== | ||
+ | |||
+ | |||
+ | <code bash> | ||
+ | |||
+ | cat / | ||
+ | |||
+ | 54 | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | Mit dem Kommando " | ||
+ | |||
+ | ==== Abfrage mit Python mit RPIO === | ||
+ | |||
+ | Library: | ||
+ | |||
+ | * https:// | ||
+ | |||
+ | |||
+ | Installation über: | ||
+ | <code bash> | ||
+ | |||
+ | yum install python-setuptools | ||
+ | easy_install -U RPIO | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | Dokumentation: | ||
+ | Oder: | ||
+ | <code python> | ||
+ | import RPIO | ||
+ | help(RPIO) | ||
+ | </ | ||
+ | |||
+ | == Benennung der GPIO Ports in der Library == | ||
+ | |||
+ | |||
+ | Für die Benennung der GPIO Ports stehen zwei Methoden zur Verfügung: | ||
+ | * **GPIO.BOARD** => PIN Nummer auf dem Board d.h. <fc # | ||
+ | * **GPIO.BCM** | ||
+ | |||
+ | |||
+ | === Eine LED blinken lassen === | ||
+ | Ziel: LED über eine GPIO Port blinken lassen | ||
+ | |||
+ | Schaltung: | ||
+ | |||
+ | * Pin 6 - Ground | ||
+ | * Pin 15 - (+) der LED | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | |||
+ | |||
+ | Wird der GPIO Port auf HIGH gesetzt, liegen 3,3 V an den Port an, die LED leuchtet. | ||
+ | |||
+ | |||
+ | Das „Hello Word“ beim der GPIO Programmierung: | ||
+ | <code python> | ||
+ | import RPIO | ||
+ | import time | ||
+ | |||
+ | |||
+ | # supress warning | ||
+ | RPIO.setwarnings(False) | ||
+ | |||
+ | |||
+ | # Which RPIO Numbering you like to use | ||
+ | RPIO.setmode(RPIO.BCM) | ||
+ | |||
+ | # My RPIO PORTs | ||
+ | # OUT | ||
+ | out_port=27 | ||
+ | RPIO.setup(out_port, | ||
+ | |||
+ | #Frist Blink | ||
+ | for i in range(0, | ||
+ | #set the out port to HIGH | ||
+ | #LED ON | ||
+ | RPIO.gpio_function(out_port) | ||
+ | RPIO.output(out_port, | ||
+ | #wait a short moment | ||
+ | time.sleep(2) | ||
+ | #set the out port to LOW again | ||
+ | # LED OFF | ||
+ | RPIO.output(out_port, | ||
+ | RPIO.gpio_function(out_port) | ||
+ | time.sleep(2) | ||
+ | |||
+ | |||
+ | # Clean up | ||
+ | RPIO.cleanup() | ||
+ | |||
+ | </ | ||
+ | |||
+ | === Eine LED ein und ausschalten | ||
+ | Über einen Taster kann einen LED an- und ausgeschaltet werden. | ||
+ | |||
+ | Über einen Pull-down Widerstand wird der Pin 11 - GPIO 17 definiert auf Masse (= LOW ) gezogen. Wird der Schalter S1 betätigt, wird ein positives Potential auf den Pin 11 gelegt, der GPIO 17 wird als HIGH erkannt. | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | ==Polling== | ||
+ | Lösung 1 - GPIO Port 17 Zustand prüfen und Out Port GPIO 27 auf HIGH setzen (LED leuchtet), dies ständig abfragen(pollen): | ||
+ | <code python> | ||
+ | import RPIO | ||
+ | import time | ||
+ | |||
+ | # supress warning | ||
+ | RPIO.setwarnings(False) | ||
+ | |||
+ | # Which RPIO Numbering you like to use | ||
+ | RPIO.setmode(RPIO.BCM) | ||
+ | |||
+ | # My RPIO PORTs | ||
+ | # OUT | ||
+ | out_port=27 | ||
+ | RPIO.setup(out_port, | ||
+ | |||
+ | # IN Port with the Switch | ||
+ | in_port=17 | ||
+ | RPIO.setup(in_port, | ||
+ | |||
+ | #endless loop | ||
+ | run=True | ||
+ | push_count=0 | ||
+ | while run : | ||
+ | #if Switch is pushed set Output HIGH | ||
+ | if (RPIO.input(in_port)): | ||
+ | push_count+=1 | ||
+ | RPIO.output(out_port, | ||
+ | #If not pushed set Output LOW | ||
+ | else: | ||
+ | RPIO.output(out_port, | ||
+ | # Print status | ||
+ | print "Port 17 => {0:5} :: Button pressed:: {1} " | ||
+ | time.sleep(2) | ||
+ | # end after 10 pushs | ||
+ | if (push_count > 10): | ||
+ | run=False | ||
+ | |||
+ | # Clean up | ||
+ | RPIO.cleanup() | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ==Interrupt Methode verwenden: | ||
+ | Lösung 2 – Interrupt setzen: | ||
+ | <code python> | ||
+ | |||
+ | import RPIO | ||
+ | import time | ||
+ | import signal | ||
+ | |||
+ | |||
+ | #globals | ||
+ | push_count=0 | ||
+ | push_state=0 | ||
+ | run=True | ||
+ | |||
+ | #define the interrupt function | ||
+ | def SetLed(gpio_id, | ||
+ | global push_count | ||
+ | push_count+=1 | ||
+ | global push_state | ||
+ | if (push_state==0): | ||
+ | push_state=1 | ||
+ | RPIO.output(out_port, | ||
+ | else: | ||
+ | RPIO.output(out_port, | ||
+ | push_state=0 | ||
+ | print "Port 17 => {0:5} :: Button pressed:: {1} " | ||
+ | print "GPIO port {} :: Value :: {}" | ||
+ | # end after 10 pushs | ||
+ | if (push_count > 10): | ||
+ | clean_exit() | ||
+ | |||
+ | # Clean exit! | ||
+ | def clean_exit(): | ||
+ | global run | ||
+ | RPIO.output(out_port, | ||
+ | run=False | ||
+ | exit() | ||
+ | |||
+ | |||
+ | #define the handler if the program is stopped with ^c | ||
+ | def handler(signum, | ||
+ | print "Catch Error {} - frame :: {}" | ||
+ | clean_exit() | ||
+ | |||
+ | # register the signal handler | ||
+ | signal.signal(signal.SIGINT, | ||
+ | |||
+ | |||
+ | # supress warning | ||
+ | RPIO.setwarnings(False) | ||
+ | |||
+ | # Which RPIO Numbering you like to use | ||
+ | RPIO.setmode(RPIO.BCM) | ||
+ | |||
+ | # My RPIO PORTs | ||
+ | # OUT | ||
+ | out_port=27 | ||
+ | RPIO.setup(out_port, | ||
+ | |||
+ | # IN Port with the Switch | ||
+ | in_port=17 | ||
+ | RPIO.setup(in_port, | ||
+ | |||
+ | # register the Interrupt | ||
+ | # RPIO.add_interrupt_callback(gpio_id, | ||
+ | |||
+ | RPIO.add_interrupt_callback(gpio_id=in_port, | ||
+ | |||
+ | |||
+ | # Non Blocking wait (own Thread will do the interrupt handling) | ||
+ | RPIO.wait_for_interrupts(threaded=True) | ||
+ | |||
+ | #Do something others | ||
+ | while run: | ||
+ | print " | ||
+ | time.sleep(2) | ||
+ | |||
+ | |||
+ | |||
+ | # Clean up | ||
+ | RPIO.cleanup() | ||
+ | |||
+ | </ | ||
+ | |||
+ | In diesen Beispiel soll bei jeden Tastendruck (Pegelwechsel von LOW nach HIGH) ( edge=' | ||
+ | Der Tastendruck schaltet die LED ein oder aus, je nachdem wie zuvor der Status war ( Variable push_stat ), nach 10 Tastendrücken wird beendet. | ||
+ | Programm wartet bis der Interrupt eintrifft und führt dann die definierte Funktion durch. | ||
+ | |||
+ | Damit aber das Programm währenddessen etwas anders tun kann, ist bei " | ||
+ | |||
+ | Die while Schleife wird ausgeführt und gleichzeitig kann der Interrupt empfangen werden. | ||
+ | Damit beim Beenden auch ein sauberer Zustand definiert wird, wird mit " | ||
+ | |||
+ | Damit aber das Programm auch sauber wieder stoppt, muss die Schleife explizit beendet werden (daher in der Clean Exit Funktion run auf false!, sonst wird nur der Interrupt Thread beendet!). | ||
+ | |||
+ | Wird das Script mit " | ||
+ | |||
+ | |||
+ | |||
+ | siehe auch => http:// | ||
+ | |||
+ | |||
+ | ==== Quellen ==== | ||
+ | |||
+ | |||
+ | Siehe auch: | ||
+ | |||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * https:// | ||
+ | |||
+ | Hardware: | ||
+ | |||
+ | * http:// | ||
+ | |||
+ | |||
+ | Scratch Raspberry Pi control: | ||
+ | |||
+ | * http:// | ||
+ | |||
raspberry/raspberry_gpio_first_steps.txt · Zuletzt geändert: 2015/02/02 20:14 von gpipperr