There have now been a number of different PCB revisions which have made small changes to the design of the Raspberry Pi PCB. In the latest revision some of these changes may affect the operation of Python code developed for earlier versions. In order to make you script react to these changes you may need to identify the board revision so your script can take appropriate action.
The following Python function “getrevision()” can be used to return a string containing the hardware revision number. This is a four character string such as “0002”.
Here is the Python function :
def getrevision(): # Extract board revision from cpuinfo file myrevision = "0000" try: f = open('/proc/cpuinfo','r') for line in f: if line[0:8]=='Revision': length=len(line) myrevision = line[11:length-1] f.close() except: myrevision = "0000" return myrevision
If you include this definition at the beginning of your Python script you can use it to set a variable equal to the board revision number :
myrevision = getrevision()
If this variable is equal to “0000” then there was an error while running the function.
Raspberry Pis that have been overvolted will have a code prefixed with “100”. If I overvolted my device I would end up with a hardware revision code of “1000002”.
At the time of writing your board revision number could be “0002”, “0003”, “0004”, “0005” or “0006”. You can use the Checking Your Raspberry Pi Board Version post to double check the results of this Python function.
2 Comments
Just a nitpick, but
length=len(line)
myrevision = line[11:length-1]
could be replaced by the shorter and simpler:
myrevision = line[11:-1]
Or, the whole function could be replaced by the even shorter but not simpler:
def getrevision():
return ([l[11:-1] for l in open('/proc/cpuinfo','r').readlines() if l[:8]=="Revision"]+['0000'])[0]
JOOSTETO , you trying to save on code lines ?
it better to be human readable