Jul 112013
 

It struck me the other day that I’ve published some fairly advanced RPi.GPIO tutorials, (e.g. interrupts and PWM) but not done anything more basic, apart from the Gertboard examples. So here’s an attempt to remedy that situation.

Over the next few days, we’re going to have a walk around Ben Croston’s RPi.GPIO, which is now at version 0.5.3a. Some of this stuff may be new to you even if you’ve been using RPi.GPIO’s more advanced features, so it’s worth having a look.

What is RPi.GPIO?

It’s an easy way of controlling the Pi’s General Purpose Input Output ports in Python. Control the world with your Raspberry Pi.

How to check what RPi.GPIO version you have

This works for all versions of RPi.GPIO

find /usr | grep -i gpio
And the output will look something like this (although there will be more of it)…

You can see all those lines with 0.5.3a.egg-info telling me I have version 0.5.3a.

How to check RPi.GPIO version in Python (works with 0.4.1a +)

While the above works without even using Python, from RPi.GPIO 0.4.1 onwards (September 2012), you can check the version in your Python programs.

It’s quite useful to be able to find out what version of RPi.GPIO you have. Some features require a recent version, whereas others work with older versions. For example, interrupts were added at 0.5.1, PWM at 0.5.2 and with 0.5.3 some bugs have been squashed.

You don’t have to write or run any programs to find out which version you have. You can do it very quickly in a live Python session. If you don’t know how to do that, it’s a great way of testing things out in Python.

Running a live Python session

In the command line environment (either before typing startx or, if you’re in LXDE, start LXTerminal) type…

sudo python run python as super user (needed for RPi.GPIO to work)
Ignore the “help” “copyright” “credits” “license” notice. Just type the next line at the >>> prompt
import RPi.GPIO as GPIO loads the RPi.GPIO library so we can use it
GPIO.VERSION this returns the version number of RPi.GPIO
CTRL+D ends the python session cleanly (it won’t appear on the screen).

Watch out for RPi.GPIO. The i in RPi, is lowercase. All the rest is uppercase. Get it wrong and it won’t work.

So next time someone publishes a python script “that requires RPi.GPIO 0.5.3 or higher”, you’ll know how to check which version you have.

If you want to make it into a little program…

import RPi.GPIO as GPIO
a = GPIO.VERSION
print a

If you don’t like live python environments, you could make it into a little script using the code above.
cd ~
nano version.py

copy and paste or retype the above script
CTRL+O save
ENTER confirm
CTRL+X exit

Then, to run it, type sudo python version.py

…and this is what you should see…

How to update your RPi.GPIO

If you find your RPi.GPIO version is horribly out of date, you can update it to the latest version with…
sudo apt-get update && sudo apt-get upgrade

But, if you haven’t updated for a while it could take quite a long time to update all your packages (>1hr).

If you don’t want to do it that way, you could use this, which updates your package list and installs the latest RPi.GPIO

sudo apt-get update && sudo apt-get install python-rpi.gpio python3-rpi.gpio

Coming soon…

In the next part, we’ll cover how to check what revision of Raspberry Pi you have. Currently there’s only Rev 1 and Rev 2, but there are some GPIO differences (e.g. i2c and port 21/27) between them, so it’s worthwhile being able to check, so you can make sure your programs will work on any Pi.

RasPiO® GPIO Reference Aids

Our sister site RasPiO has three really useful reference products for Raspberry Pi GPIO work...

  1. Portsplus port ID board
  2. GPIO Ruler with RPi.GPIO code
  3. GPIO Zero Ruler with GPIO Zero code

  15 Responses to “RPi.GPIO basics 1 – how to check what RPi.GPIO version you have”

  1. The other option is to type
    dpkg -l python-rpi.gpio
    but that will only work if you’ve installed it from the repository with apt-get (rather than installing it from source / by hand).

  2. Another small tip: Your “find” command-line can be sped up by changing it to:
    find /usr -iname "*gpio*"
    (and this also has the advantage that it doesn’t print out the same directory-match multiple times)

  3. Oops running GPIO as superuser is mandatory? That’s such a bad practice, it’s giving me the creeps… well after being cultured by unix by 20 years LOL. Setting proper permissions is the right things to do.

    • You’re almost certainly right, but there’s nothing I can do about it. I am a mere Python dabbler not a low level C programmer.

    • There’s two ways of accessing the Pi’s GPIO pins – you can either use the high-level GPIO sysfs interface e.g. http://elinux.org/RPi_Low-level_peripherals#Bash_shell_script.2C_using_sysfs.2C_part_of_the_raspbian_operating_system and as this is file-based it’s fairly straightforward to set up appropriate user/group/etc. access permissions. The other way is by accessing /dev/mem and prodding registers in memory directly (which is obviously potentially dangerous, and so will always need superuser access).
      I believe RPi.GPIO originally used sysfs access from Python, but was then re-written in C to use direct memory access, because the latter allows much faster GPIO functionality (since you’re banging the hardware directly rather than going through a translation layer, coupled with the Pi’s relatively slow CPU).

      On another forum post on Alex’s blog somebody recently mentioned that the WiringPi library doesn’t need superuser access, but I’ve not yet looked at that myself.

  4. Hello,

    Awesome blog, thank god i found it.. Thanks for all that awesome info.
    I am however having an issue. Following all of the above to a t (running the update and upgrade now, so that might help). Anyways, my problem is i get an error when I include the GPIO.cleanup(). When i run find /usr | grep -i gpio
    I basically get what you are getting 0.5.3a.egg-info. What is the deal, any clues?
    I have no issues with other functions, they run fine. Help would be greatly appreciated since I would have to fry a pin. I am running Wheezy if that matters.

    Thanks again

    • Andrew understands this better than me, but it sounds like you may have manually installed RPi.GPIO at some point in the past and this overrides the apt-get install.

      I’m not sure how to remove it though (apart from reflash the SD card with the latest OS ;) ) There is a way though.

    • As always, just saying “I get an error” is never very helpful :-(

      What would be much more helpful would be including exactly what the error message actually is…

  5. I have just followed the upgrade process as described above. When I go int python I get the following:-
    >>> import RPi.GPIO as GPIO
    >>> GPIO.VERSION
    Traceback (most recent call last):
    File “”, line 1, in
    AttributeError: ‘module’ object has no attribute ‘VERSION’
    >>>

    If I use the find linux command I get the following:-
    pi@raspberrypi ~ $ find /usr | grep -i gpio
    /usr/lib/python3/dist-packages/RPi/GPIO.cpython-32mu.so
    /usr/lib/python3/dist-packages/RPi.GPIO-0.5.3a.egg-info
    /usr/lib/pyshared/python2.6/RPi/GPIO.so
    /usr/lib/pyshared/python2.7/RPi/GPIO.so
    /usr/lib/python2.6/dist-packages/RPi/GPIO.so
    /usr/lib/python2.6/dist-packages/RPi.GPIO-0.5.3a.egg-info
    /usr/lib/python2.7/dist-packages/RPi/GPIO.so
    /usr/lib/python2.7/dist-packages/RPi.GPIO-0.5.3a.egg-info
    /usr/local/lib/python2.7/dist-packages/RPIO-0.10.0-py2.7-linux-armv6l.egg/RPIO/_GPIO.py
    /usr/local/lib/python2.7/dist-packages/RPIO-0.10.0-py2.7-linux-armv6l.egg/RPIO/_GPIO.pyc
    /usr/local/lib/python2.7/dist-packages/RPIO-0.10.0-py2.7-linux-armv6l.egg/RPIO/_GPIO.so
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/RPi
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/RPi/GPIO.py
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/RPi/__init__.pyc
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/RPi/GPIO.so
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/RPi/__init__.py
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/RPi/GPIO.pyc
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/EGG-INFO
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/EGG-INFO/zip-safe
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/EGG-INFO/top_level.txt
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/EGG-INFO/SOURCES.txt
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/EGG-INFO/PKG-INFO
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/EGG-INFO/dependency_links.txt
    /usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.3.1a-py2.7-linux-armv6l.egg/EGG-INFO/native_libs.txt
    /usr/share/doc/python-rpi.gpio
    /usr/share/doc/python-rpi.gpio/changelog.gz
    /usr/share/doc/python-rpi.gpio/copyright
    /usr/share/doc/python-rpi.gpio/changelog.Debian.gz
    /usr/share/doc/python3-rpi.gpio
    /usr/share/doc/python3-rpi.gpio/changelog.gz
    /usr/share/doc/python3-rpi.gpio/copyright
    /usr/share/doc/python3-rpi.gpio/changelog.Debian.gz
    /usr/share/pyshared/RPi.GPIO-0.5.3a.egg-info

    I am trying to use PWM and get the following error-
    >>> import RPi.GPIO as GPIO
    >>> GPIO.setmode(GPIO.BCM)
    >>> GPIO.setup(25, GPIO.OUT)
    >>> p = GPIO.PWM(25, 50)
    Traceback (most recent call last):
    File “”, line 1, in
    AttributeError: ‘module’ object has no attribute ‘PWM’
    >>>

    Please can you advise

  6. Thanks Alex I will do.

  7. I just found out that when running idle3 with python, you can obtain super user privledges by pressing ALT-F3, and then enter “sudo idle3” in the box. I haven’t tried it on idle yet.
    Strang but it worked

Leave a Reply