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.
This article is an updated version to take into account changes in the RPi.GPIO library and the release of the Raspbian SD card image.
UPDATE : The latest Raspbian (2012-09-18-wheezy-raspbian) 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.
The latest packages are available on the RPi.GPIO downloads page.
To install it you need to enter some command line instructions. You can either use the command prompt after you have logged into your Pi or use a command line tool (i.e. LXTerminal) once you have entered the graphical interface (by typing startx).
Step 1 – Download the library
Take a look at the downloads page and select a suitable package. I have selected the “Raspbian package for Python 2″. Use wget to download the package to your Raspberry Pi.
Step 2 – Install the library
The following command will use the dpkg utility to install the package.
sudo dpkg -i python-rpi.gpio_0.3.1a-1_armhf.deb
That’s all there is to it! This will now mean you can use the library within Python.
Copy the following script into a text file called “gpio_test.py” :
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)
To run this script you can use the following command :
sudo python gpio_test.py
You need to include “sudo” in front of this command as the GPIO library requires root privileges.