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. Thankfully this library is now including in the standard Raspbian image available from the Foundations Download Page.
If you are using a fresh image you don’t need to install it but I’ve kept the instructions here in case you ever want to try a manually installation.
Method 1 – Install from repository
If the package exists in the Raspbian repository is can be installed using apt-get. First you need to update the available package versions :
sudo apt-get update
Then attempt to install the RPi.GPIO package :
sudo apt-get install rpi.gpio
If it isn’t already installed it will be installed. If it is already installed it will be upgraded if a newer version is available.
Method 2 – Manual Installation
The package is available from http://pypi.python.org/pypi/RPi.GPIO and the current version is 0.5.11 (February 2015). If this version is updated you will need to make appropriate changes to the version number in the commands below.
Step 1 – Download the library
wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz
Step 2 – Extract the archive to a new folder
tar -xvf RPi.GPIO-0.5.11.tar.gz
Step 3 – Browse to the new directory
cd RPi.GPIO-0.5.11
Step 4 – Install the library
sudo python setup.py install
Step 5 – Remove the directory and archive file
cd ~ sudo rm -rf RPi.GPIO-0.*
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)
17 Comments
Pls i followed the step above but am still getting no attribute setmode error…how do i correct this pls. thanks
Use the latest version of Raspbian. This library is now installed by default so you do not need to install it manually.
Can you please explain the difference between GPIO.setmode(GPIO.BOARD) and GPIO.setmode(GPIO.BCM)?
Thanks in advance!!
BOARD allows you to use the physical pin numbers 1-26. BCM requires that you use the GPIO reference numbers. So physical Pin 8 (BOARD 8) is actually GPIO14 (BCM 14). Use the diagrams on my GPIO Header page to see the pin numbers against the GPIO references.
Hello
I’ve downloaded and tried to setup the library on raspberry b+ but it outputs this error
fatal error Python.h file not existent
What is this error?
Thank you
Roberto
If you use the latest version of Raspbian you don’t need to install manually.
When I run my script with the GPIO commands, the SETUP command returns a runtime error with “no access to /dev/mem” The import and setmode commands precede the setup command OK. What am I missing?
How do i get to see all GPIO python commands and syntaxes?
The examples here cover most of the commands : http://sourceforge.net/p/raspberry-gpio-python/wiki/Examples/
If you use the latest version of Raspbian you don’t need to install separately.
i follow your instruction but
when i run my script i’ll get this
sudo python LED.py
Traceback (most recent call last):
File “LED.py”, line 4, in
GPIO.setmode(GPIO.BCM)
AttributeError: ‘module’ object has no attribute ‘setmode’
Are you using the latest Raspbian image? It looks like RPi.GPIO isn’t installed properly. On the latest image RPi.GPIO is already included and doesn’t need installing separately.
Packages on PyPi are installable with pip, and you should only need to download their archives if your device has no internet and/or pip.
For Python 2:
# pip install RPi.GPIO
For Python 3:
# pip3 install RPi.GPIO
Awesome dude, i’m new on this world and your post really help me.
thank you
hi, i am using the Raspberry pi desktop and rpi.gpio is already installed in it but the python keeps throwing error of no found RPi.GPIO module, i dont have raspberry pi board so just wanted to code and practice with interpreter, please help.
Because there are no physical GPIO pins when using Raspbian on a PC/laptop you can’t use the GPIO library. It is possible to connect a Pi Zero to act as a “GPIO expander” so you can write code on a PC. More details here : https://www.raspberrypi.org/blog/gpio-expander/