Pi Hardware detection

Have you ever been working on a Pi that is maybe remote, and wondered what board it is running on ?
Well you can actually work it out; but just in case its of any use to anyone, I bashed out this bash script (did you see what I did there…) to do exactly that;

#! /bin/bash
#
# Return the version of the Raspberry Pi we are running on
# Written by Andy Taylor
#

# Pull the Board revision from /proc/cpuinfo
boardRev=`grep 'Revision' /proc/cpuinfo | sed 's/.*: //'`

# Make the board revision human readable
case $boardRev in
[0002]*)
        raspberryVer="Model B Revision 1.0 (256MB)"
        ;;
[0003]*)
        raspberryVer="Model B Revision 1.0 + ECN0001 (no fuses, D14 removed) (256MB)"
        ;;
[0004]*)
        raspberryVer="Model B Revision 2.0 Mounting holes (256MB)"
        ;;
[0005]*)
        raspberryVer="Model B Revision 2.0 Mounting holes (256MB)"
        ;;
[0006]*)
        raspberryVer="Model B Revision 2.0 Mounting holes (256MB)"
        ;;
[0007]*)
        raspberryVer="Model A Mounting holes (256MB)"
        ;;
[0008]*)
        raspberryVer="Model A Mounting holes (256MB)"
        ;;
[0009]*)
        raspberryVer="Model A Mounting holes (256MB)"
        ;;
[000d]*)
        raspberryVer="Model B Revision 2.0 Mounting holes (512MB)"
        ;;
[000e]*)
        raspberryVer="Model B Revision 2.0 Mounting holes (512MB)"
        ;;
[000f]*)
        raspberryVer="Model B Revision 2.0 Mounting holes (512MB)"
        ;;
[0010]*)
        raspberryVer="Model B+ (512MB)"
        ;;
[0011]*)
        raspberryVer="Compute Module (512MB)"
        ;;
[0012]*)
        raspberryVer="Model A+ (256MB)"
        ;;
[a01041]*)
        raspberryVer="Pi 2 Model B (1GB) - Sony, UK"
        ;;
[a21041]*)
        raspberryVer="Pi 2 Model B (1GB) - Embest, China"
        ;;
[900092]*)
        raspberryVer="Pi Zero (512MB)"
        ;;
[a02082]*)
        raspberryVer="Pi 3 Model B (1GB) - Sony, UK"
        ;;
[a22082]*)
        raspberryVer="Pi 3 Model B (1GB) - Embest, China"
        ;;
*)
        raspberryVer="Unknown"
        ;;
esac

#Output
echo $raspberryVer
2 Likes

cool tool!

painful pun!

Nice! Love it…the script is not bad also :wink:

As suggested by @Jon I think this would be a useful addition to the emonPi part of Emoncms Admin server stats page. Also this is a better method to detect a pi rather than what we currently use ifconfig | grep b8:27:eb:' :

The ifconfig grep falls down when a Pi Model A is used!

Understood on the detection, similarly I can’t rely on this detection method either - because anything that is not a Pi will just be “unknown” at this point.

I will Improve the script and then come back to the Pi detection again.

Detection?

Find?

I will Improve the script and then come back to the Pi issue again. :grin:

Thx Bill, fingers moving too fast for spell check this afternoon, that will teach me for multi-tasking badly :wink:

Mine too! :slight_smile:

Is this not the behaviour we want? If “unknown” is returned then we assume the system is not a Pi therefore don’t display the Pi specific items such as type, CPU temp etc.

Hi Andy,

Your script IDs my Pi as a “Model B Revision 1.0 + ECN0001 (no fuses, D14 removed) (256MB)”

As you can see by the Revision : 3a21041 in my /proc/cpuinfo data, it’s a Pi 2.

Looks like grep is seeing the 3 in the string 3a21041, but not the entire string.
(the 0003 branch in your case construct matches the reported ID)

Adding an echo $boardRev immediately after boardRev=`grep ‘Revision’ /proc/cpuinfo | sed ‘s/.*: //’
shows the variable is indeed getting set to 3a21041.

Here’s a way to strip the leading characters (up to and including the space character immediately preceeding the ID sting) from boardRev without the need for a sub-shell call, i.e. sed:
(Ref: BashFAQ/100 - Greg's Wiki) and
BashFAQ/082 - Greg's Wiki

boardRev=$(grep 'Revision' /proc/cpuinfo)
boardRev=${boardRev##*: }

processor    : 0
model name    : ARMv7 Processor rev 5 (v7l)
BogoMIPS    : 38.40
Features    : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm 
CPU implementer    : 0x41
CPU architecture: 7
CPU variant    : 0x0
CPU part    : 0xc07
CPU revision    : 5

processor    : 1
model name    : ARMv7 Processor rev 5 (v7l)
BogoMIPS    : 38.40
Features    : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm 
CPU implementer    : 0x41
CPU architecture: 7
CPU variant    : 0x0
CPU part    : 0xc07
CPU revision    : 5

processor    : 2
model name    : ARMv7 Processor rev 5 (v7l)
BogoMIPS    : 38.40
Features    : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm 
CPU implementer    : 0x41
CPU architecture: 7
CPU variant    : 0x0
CPU part    : 0xc07
CPU revision    : 5

processor    : 3
model name    : ARMv7 Processor rev 5 (v7l)
BogoMIPS    : 38.40
Features    : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm 
CPU implementer    : 0x41
CPU architecture: 7
CPU variant    : 0x0
CPU part    : 0xc07
CPU revision    : 5

Hardware    : BCM2709
Revision    : 3a21041

Bill,

Very interesting, the revision your Pi lists - is not an official Pi revision number, are you running your Pi overclocked or over voltage?

I’ll re-work the script to take account of the issue you are seeing and re-post it shortly.

So after some clean up, and some thoughts about how to handle the many un-knowns;
I now have this revised version, this time I check for ARM CPU (vs anything else) and detect (more reliably) the Pi version.

I don’t have access to Banana / Orange / Beaglebone etc to add detection for those, so right now I assume anything that is ARM and not in my list is just unknown. I don’t even know if they show the revision line (something that is also not accounted for at this point).

As Glyn has already realised - yes this little exercise is all in aid of better Pi detection for the emonCMS system :slight_smile:

#! /bin/bash
#
# Return the version of the Raspberry Pi we are running on
# Written by Andy Taylor (MW0MWZ)
#

# Pull the CPU Model from /proc/cpuinfo
modelName=`grep 'model name' /proc/cpuinfo | sed 's/.*: //'`

if [[ $modelName == "ARM"* ]]
then
        # Pull the Board revision from /proc/cpuinfo
        boardRev=`grep 'Revision' /proc/cpuinfo | sed 's/.*: //'`

        # Make the board revision human readable
        case $boardRev in
        *0002) raspberryVer="Model B Revision 1.0 (256MB)";;
        *0003) raspberryVer="Model B Revision 1.0 + ECN0001 (no fuses, D14 removed) (256MB)";;
        *0004) raspberryVer="Model B Revision 2.0 Mounting holes (256MB)";;
        *0005) raspberryVer="Model B Revision 2.0 Mounting holes (256MB)";;
        *0006) raspberryVer="Model B Revision 2.0 Mounting holes (256MB)";;
        *0007) raspberryVer="Model A Mounting holes (256MB)";;
        *0008) raspberryVer="Model A Mounting holes (256MB)";;
        *0009) raspberryVer="Model A Mounting holes (256MB)";;
        *000d) raspberryVer="Model B Revision 2.0 Mounting holes (512MB)";;
        *000e) raspberryVer="Model B Revision 2.0 Mounting holes (512MB)";;
        *000f) raspberryVer="Model B Revision 2.0 Mounting holes (512MB)";;
        *0010) raspberryVer="Model B+ (512MB)";;
        *0011) raspberryVer="Compute Module (512MB)";;
        *0012) raspberryVer="Model A+ (256MB)";;
        *a01041) raspberryVer="Pi 2 Model B (1GB) - Sony, UK";;
        *a21041) raspberryVer="Pi 2 Model B (1GB) - Embest, China";;
        *900092) raspberryVer="Pi Zero (512MB)";;
        *a02082) raspberryVer="Pi 3 Model B (1GB) - Sony, UK";;
        *a22082) raspberryVer="Pi 3 Model B (1GB) - Embest, China";;
        *) raspberryVer="Unknown ARM based System";;
        esac

        echo $raspberryVer

else
        echo "Generic "`uname -p`" class computer"
fi

Linux is so versatile its great, and while the was a fun process - some times things can be achieved much more simply…

$cat /sys/firmware/devicetree/base/model 
Raspberry Pi 2 Model B Rev 1.1
3 Likes

No, no overclocking or overvoltage.

Here’s my config.txt FWIW:

gpu_mem=16
arm_freq=900
force_turbo=1
initial_turbo=60
disable_splash=1
hdmi_group=2
hdmi_mode=19
hdmi_force_hotplug=1
device_tree=

Nice find!

1 Like

Bill,

I don’t have the following lines set in my config;

arm_freq=900
force_turbo=1
initial_turbo=60

Also, when checking the current CPU frequency:

LOAD
[email protected]:~ $ sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
1200000
IDLE
[email protected]:~ $ sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
600000

My Pi 3 idles at 600MHz and hits 1.2GHz under load, my Pi 2 does similar things (although its not running the OpenEnergyMonitor image).

So you might not be “Overclocked” but depending on the kernel / system you are running on, you might not need to set those features.

The reason I ask about over volt - once you have over-volted the Pi just once, it registers that in the SOC, and I believe the version number is where that is set - as usual, quite prepared to be wrong about that :slight_smile:

Understandable, since the Pi 3 max clock freq is 1200 MHz. :wink:

Here’s one I didn’t know about until today:

When running lscpu or cat /proc/cpuinfo the reported processor ist (sic) ARMv7. To tell the RPi3B to start in ARMv8-mode you have to add a new line to config.txt:
arm_control=0x200
Ref: http://elinux.org/RPiconfig#Enable_ARMv8_on_RPi3B

Locking my Pi 2 at 900 MHz seems to make it a bit more responsive, while staying within spec’d maximums. All of my Pis are in enclosures, so to keep the operating temps reasonable, I’ve never overvolted or overclocked them.

I knew about setting the overvolt bit (and how that used to void your warranty), but I havent got a clue about where the version number is stored.

I suspect that only works on Raspbian images. I get a “no such file…” response on a non-rasbian built OS.