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.
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 GPIO.setmode(GPIO.BCM) if GPIO.RPI_REVISION == 1: signalwire1 = 21 else: 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.
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 gpiopins.py):
#!/usr/bin/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) if (GPIO.RPI_REVISION > 2): 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 if (GPIO.RPI_REVISION == 1): print " SDA0 3 4 5V" print " SCL0 5 6 Gnd" else: 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 if (GPIO.RPI_REVISION == 1): print " GPIO21 13 14 Gnd" else: 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) if (GPIO.RPI_REVISION != 1): print "" print "P5:" print " 3V3 2 1 5V" print " SCL0 4 3 SDA0" print " GPIO31 6 5 GPIO30" print " Gnd 8 7 Gnd" #End
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:
- My mind is spoiled by technology and it is no longer trained to remember long lists of numbers
- 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 http://pypi.python.org/pypi/RPi.GPIO.
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:
Sorry for not thinking to post mine!