Raspberry PI GPIO in python
Before you continue you should get a breakoutbox to the GPIO-pins.
If you shorten the GPIObus you can break the Raspberry PI.
There are no protection from this on the GPIO-pins!
You can try Adafruit PI-Cobbler to connect to a breadboard, BUT it will
not protect you from shorten the GPIO-pins. Be carefull.
This is only a simple GUI to simulate a control of a GPIO-pin.
(No GPIO included) GPIOgui1st.py
And here is the GUI that actually controls the GPIO-pin of the green LED,
(next to the RED one on the Raspberry PI 2)
GPIOguiComb.py
First make sure you have the latest GPIO (otherwise you may get an error like:
"RuntimeError: This module can only be run on a Raspberry Pi!"
=========================================================
sudo apt-get update
sudo apt-get install python-rpi.gpio python3-rpi.gpio
For further help goto: http://sourceforge.net/p/raspberry-gpio-python/wiki/install/
Then import the module into your python-script
=================================
import RPi.GPIO as GPIO

Picture 1: from Meltwaters homepage
There are two ways to number the GPIO-pins:
* BCM which is more general and works with different programming languages,
but differs between PI1 and PI2-models.
* As it is, Board numbering
To setup the board using BCM type:
GPIO.setmode(GPIO.BCM)
To setup the board usning board numbering type:
GPIO.setmode(GPIO.BOARD)
The Raspberry also has pull-up and pull-down resistors and can be activated with this commands:
GPIO.setup(GPIOpinNO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GPIOpinNO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
When the program end reset the pins state, otherwise they will preserve their state:
GPIO.cleanup()
To read an pin type:
GPIO.setup(GPIOpinNO, GPIO.IN)
GPIO.input(GPIOpinNO)
And to Output type:
GPIO.setup(GPIOpinNO, GPIO.OUT)
GPIO.output(GPIOpinNO, GPIOstate)
Where GPIOstate=1 or 0
There are two ways to react on input changes, polling and interrupt (event)
I really prefer interrupts (events) and only use polling if there is no other way.
I will now describe the two ways to react and the differense between them.
Polling
=======
GPIO.wait_for_edge(GPIOpinNO, GPIO.RISING)
GPIO.wait_for_edge(GPIOpinNO, GPIO.FALLING)
Program will halt and wait untill condition is fullfilled, and then continue.
interrupts (events)
=============
def eventFunctionName(GPIOpinNO):
some code
#main function
GPIO.add_event_detect(GPIOpinNO, GPIO.RISING, callback=eventFunctionName)
while True:
some code
Program will run in the main loop (While True). When an event accours the program will jump to the function
eventFunctionName(channel)and execute the function and then return to the main loop. The program
will not need to wait for an input pin to change state.
Let us combine this into several scripts:
======================================
GPIOshell.py (A simple script that sets BCM pin nr 4 to high,
wait for return-kay to be pressed and outputs a low)
GPIOevent.py (A simple script that exit when BCM pin 4 is set to low.
The script is event driven.