Sometimes it can be simple.

Posted: January 30, 2013 in Guides
Tags: , , , ,

I’ve been writing some GPIO control stuff recently using a few different languages and tonight it was the turn of python.
Now from using different languages I’ve grown accustomed to using the BCM references for the GPIO pins.  However as some of you may know, these differ slightly between Rev1 and Rev2 of the boards so it is important to ensure your code matches and is easily adapted for the board you are using.

Pinout for Rev1 and Rev2 RaspberryPi

Diagram includes BCM GPIO references (GPIO.BCM), common functions, Header GPIO references, and Pin numbers (GPIO.BOARD).

An easy solution is simply to use a define for each and edit the code accordingly.  But for python I’d often heard it suggested to automatically detect the revision of the board, which seems very sensible to me.

A quick search later and there is a jumble of answers with complex solutions, the original idea of commenting in or out a define didn’t seem so bad.
Then I found a reference to GPIO.RPI_REVISION, no real explanation with it, just that it is new to RPi.GPIO version 0.4.0a.

Well perhaps it is just a simple a that, we simply do…

import RPi.GPIO as GPIO
  signalwire1 = 21
  signalwire1 = 27
GPIO.setup(signalwire1, GPIO.OUT)
GPIO.output(signalwire1, GPIO.HIGH)

Is that just new or just too obvious for Linux gurus people to post?  I shall assume that my search just failed me instead.

When using the RPi.GPIO python library you can pick the numbering system to use:
GPIO.setmode(GPIO.BOARD) # to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BCM) # to use Broadcom's SoC references
For those using python only, using the GPIO.BOARD references probably makes most sense.

Well to test it, I made a simple script (I did consider using multiple lists, iterating through them and tab formatting into tables etc) but then why not just keep that simple too.  Just remember RPi.GPIO will require you to run as a super user (sudo python

import RPi.GPIO as GPIO

#Detect and display board information
#If additional changes are made beyond Rev2,
#this code will need to be updated.
print "Meltwater's Pi Hardware - GPIO Info"
print "Board Revision: " + str(GPIO.RPI_REVISION)
 print "Warning: New Revision detected check for new pinouts."
print "P1:"
print " 3V3 1 2 5V"

#Select board revision here to allow for pins 3 & 5 update
 print " SDA0 3 4 5V"
 print " SCL0 5 6 Gnd"
 print " SDA1 3 4 5V"
 print " SCL1 5 6 Gnd"

print " GPIO04 7 8 Tx"
print " Gnd 9 10 Rx"
print " GPIO17 11 12 GPIO18"

#Select board revision here to allow for pin 13 update
 print " GPIO21 13 14 Gnd"
 print " GPIO27 13 14 Gnd"

print " GPIO22 15 16 GPIO23"
print " 3V3 17 18 GPIO24"
print " MOSI 19 20 Gnd"
print " MISO 21 22 GPIO25"
print " SCLK 23 24 CE0"
print " Gnd 25 26 CE1"

#Select board revision here for Rev2's new P5 header
# (as viewed from topside)
 print ""
 print "P5:"
 print " 3V3 2 1 5V"
 print " SCL0 4 3 SDA0"
 print " GPIO31 6 5 GPIO30"
 print " Gnd 8 7 Gnd"


A copy of the code can be found here (with good spacings) –

I like pinout diagrams, I’ve put one on my case, I’ve even pressed one onto the GPIO pins*, because:

  1. My mind is spoiled by technology and it is no longer trained to remember long lists of numbers
  2. When connecting electronics it is always worth checking the connections before pressing go or switching it on!

So, this script will get added to my helpful quick command menu that I use (this saves me remembering all the pseudo commands Linux expects everyone to etch into their soul).

More on that menu another day…after-all keeping it simple is sometimes all that is needed.

By the way latest version of RPi.GPIO is available from
It is worth keeping up to date, as new features and improvements are added all the time.

*Press-On GPIO Pinout – Apparently it is “absolutely genius”…

Although it is not the one I used, you too can also press your own Pin Outs:

Dr. Simon Monks – GPIO “Raspberry Leaf”

Sorry for not thinking to post mine!

  1. Raspberry Pi says:

    “this is very interesting. thanks for that. we need more sites like this. i commend you on your great content and excellent topic choices.”

  2. Rasperry Pi says:

    This topic is not as simple as it seems. Thanks for the clarification.

  3. tpobrienjr says:

    If, perchance, GPIO.RPI_REVISION has a value of 3 or more sometime in the future, it would be a Good Thing (and Kind) to say something in the code to alert the programmer and avoid a problem-hunting exercise.

    That said, this is an interesting and very helpful blog. Thanks for all your efforts.

    • That is a good idea!
      Of course we shall first be hunting down Eben, who promised us Rev2 will be the only major change.

      The code would benefit from more comments throughout so will added some more, making it clear where it would need to change if Rev3 reversed all the pins or something.

  4. […] LED一個ではあまり面白くないので、今回はLEDを三個に増やしてGPIOの23~25に接続。ピンヘッダの番号で言うと16、18、22番になる。ピン番号の対応はこのページにある表が分かりやすい。コピーして保存しておくと良いだろう。 […]

  5. […] from Meltwater’s Raspberry Pi Hardware […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.