Install RPi.GPIO Python Library

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.5.5 (April 2014). 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

wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.5.tar.gz

Step 2 – Extract the archive to a new folder

tar zxf RPi.GPIO-0.5.5.tar.gz

Step 3 – Browse to the new directory

cd RPi.GPIO-0.5.5

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.5.5/

Step 6 – Delete the archive file

rm RPi.GPIO-0.5.5.tar.gz

This will now mean you can use the library within Python.

Example Usage

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)
This entry was posted in Python, Tutorials & Help and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>