Jul 122013
 
RasPiTV-RPi.GPIO-revcheck

It all started in September 2012. The Raspberry Pi Foundation went and made some improvements to the Pi. How dare they!! ;) Seriously, though, there were some significant improvements to the Pi and a Rev 2 version was launched with double the RAM and some other changes/additions/improvements.

This threw up some minor headaches for developers because some of the GPIO pinouts were changed and some new GPIO ports were made available on a brand new “solder it yourself if you want it” header called P5 (see the leaning header of Pi5a)

8 holes for P5 header on Raspberry Pi Rev 2. Square = pin 1

GPIO 0 became GPIO 2 (P1 pin 3)
GPIO 1 became GPIO 3 (P1 pin 5)
GPIO 21 became GPIO 27 (P1 pin 13)

The two i2c buses were swapped around:
SDA0 became SDA1 (P1 pin 3 alternative function)
SCL0 became SCL1 (P1 pin 5 alternative function)

Some new GPIO ports 28, 29, 30, 31 were made available if you solder on P5 header.

Who cares?

People whose programs no longer work with all Raspberry Pis – that’s who!

For example, the Gertboard LEDs program uses GPIO21. If using a Rev 1 Pi this worked fine, but on a Rev 2 Pi (including model A) the fifth LED wouldn’t light because GPIO21 is now GPIO27. To work properly, the program needs to be able to know whether it’s running on a Rev 1 or Rev 2 Pi, so it can use the correct ports.

How to check your Raspberry Pi Revision number?

There’s a way to see what your Pi Revision is…
cat /proc/cpuinfo

You can see this gives lots of info. Near the bottom is “Revision : 000f

This tells me I have a Rev 2 Pi, but it’s a bit clunky. There are several different rev. codes for different Pi models and manufacturers (here’s a list of them). We could write some code to check the cpuinfo and extract the bit we want, compare it with known revision codes etc. But we don’t need any of that because, from RPi.GPIO 0.4.0a onwards (September 2012) we can use a built-in RPi.GPIO variable which does it all for us. (If you have older than 0.4.0a, see yesterday’s post for updating instructions.)

It’s called GPIO.RPI_REVISION

As before, we can try this out in a live python session…

sudo python
import RPi.GPIO as GPIO
GPIO.RPI_REVISION

Mine returns a 2 because it’s a Rev 2 Pi.

Or you can incorporate the code into a program as we did in yesterday’s post

import RPi.GPIO as GPIO
print "Your Pi is a Revision %s, so port 21 becomes port 27 etc..." % GPIO.RPI_REVISION

So then, when you’re doing some GPIO work that uses any of the ports 0, 1, 2, 3, 21, 27, 28, 29, 30, 31, you can be sure to control the correct ports. See the example below…

import RPi.GPIO as GPIO
if GPIO.RPI_REVISION == 1:
    ports = [0, 1, 21]
else:
    ports = [2, 3, 27]
print "Your Pi is a Revision %s, so your ports are: %s" % (GPIO.RPI_REVISION, ports)

As long as no further changes to GPIO pin allocations are made, the above should work with all future Pi revisions. If further changes are made, they will need to be incorporated like this (and Ben will have to update RPi.GPIO again)…

import RPi.GPIO as GPIO
if GPIO.RPI_REVISION == 1:
    ports = [0, 1, 21]
elif GPIO.RPI_REVISION == 2:
    ports = [2, 3, 27]
else:
    ports = ["whatever the new changes will be"]
print "Your Pi is a Revision %s, so your ports are: %s" % (GPIO.RPI_REVISION, ports)

What next?

That was part 2: checking your Pi board revision number. Tomorrow, in part 3 we’ll cover the best way to handle a clean CTRL+C exit, protect your Pi and avoid all those nasty, horrid, “port already in use by another program” messages.

  14 Responses to “RPi.GPIO basics 2 – how to check what Pi board Revision you have”

  1. [...] 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 [...]

  2. If you use the BOARD numbering system rather than the BCM numbering system then the differences in board revisions are handled automatically for you. Your code will not need to detect board revision itself.

    • Yes indeed. I forgot to mention that because I habitually use the GPIO numbering scheme. Thanks for adding that Ben :)

      Oh yeah – and because I’m doing this “backwards” I won’t cover that until day 4 :)

      p.s. Does the board scheme cover the P5 header on Rev 2 Pis?

    • Getting the revision is useful even if using board numbering (P1/P5 connectors numbering) as you may also want to access I2C and that chamged at same time.

      Personally using Board numbering makes circuit/wiring instructions match the software and easier to follow especially for beginners

  3. For anybody curious, you can find a list of the different /proc/cpuinfo revision codes at http://elinux.org/RPi_HardwareHistory

  4. And if you are not wedded to Python, there are a handy bunch of scripts and code examples for finding board revision in several languages at http://raspberryalphaomega.org.uk/2013/02/06/automatic-raspberry-pi-board-revision-detection-model-a-b1-and-b2/

  5. I’m confused. I tried both methods and get different results, i.e. dumping /proc and I see a “0002″, but running the python code, I’m told “1″. Ideas? Thanks in advance. s.

    stephe@srw-pi ~ $ cat /proc/cpuinfo
    Processor : ARMv6-compatible processor rev 7 (v6l)
    BogoMIPS : 697.95
    Features : swp half thumb fastmult vfp edsp java tls
    CPU implementer : 0x41
    CPU architecture: 7
    CPU variant : 0x0
    CPU part : 0xb76
    CPU revision : 7

    Hardware : BCM2708
    Revision : 0002
    Serial : 00000000ce955231
    stephe@srw-pi ~ $ sudo python
    Python 2.7.3rc2 (default, May 6 2012, 20:02:25)
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import RPi.GPIO as GPIO
    >>> GPIO.RPI_REVISION
    1
    >>>

    • Easy answer:

      0002 is one of the Rev 1 codes

      AndrewS linked to a list of them 2 comments above. I think I’ll incorporate that link into the main post though.

      • Urg! Thanks. I’ll read the WHOLE post and links next time. s.

        • 0002 and 0003 were the Rev 1 codes, but it was made a bit more complicated in that if you overvolted your Pi, the code became 10000002 or 10000003, so you had to pull the last four digits from the code to parse the value properly.

          Everything else can still be treated as Rev 2 – until they bring out the next batch of changes, that is :)

  6. [...] 2: “how to check what Pi board Revision you have” follows up [...]

Leave a Reply