The RPi.GPIO Python library allows you to easily configure and read-write the input/output pins on the Pi’s GPIO header within a Python script. In order to use it you need to install the package as this isn’t currently included in the Debian Squeeze image.
UPDATE : The latest Raspbian SD card image includes the RPi.GPIO library already installed so if you are using that image you do not need to bother reading the rest of this article.
The package is available from http://pypi.python.org/pypi/RPi.GPIO and the current version is 0.3.1a. If this version is updated you will need to make appropriate changes to the version number in the commands below.
To install it launch a command line (i.e. LXTerminal) and enter the following commands :
Step 1 – Download the library
Step 2 – Extract the archive to a new folder
tar zxf RPi.GPIO-0.3.1a.tar.gz
Step 3 – Browse to the new directory
Step 4 – Install the library
sudo python setup.py install
Step 5 – Remove the directory and its contents
cd .. sudo rm -rf RPi.GPIO-0.3.1a/
Step 6 – Delete the archive file
This will now mean you can use the library within Python.
import RPi.GPIO as GPIO # to use Raspberry Pi board pin numbers GPIO.setmode(GPIO.BOARD) # set up the GPIO channels - one input and one output GPIO.setup(11, GPIO.IN) GPIO.setup(12, GPIO.OUT) # input from pin 11 input_value = GPIO.input(11) # output to pin 12 GPIO.output(12, GPIO.HIGH) # the same script as above but using BCM GPIO 00..nn numbers GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN) GPIO.setup(18, GPIO.OUT) input_value = GPIO.input(17) GPIO.output(18, GPIO.HIGH)