22

Is there a way of determining whether the current Raspberry Pi is either a 2 Model B and 3 with Raspbian Jessie Lite 8.0?

This is because I have a particular bootstrap.sh written in Bash where it needs to set the attribute txpower for a Wi-Fi USB dongle (here, Raspberry Pi 2) using a Ralink RT5370 chipset driver.

I set the Wireless attributes using iwconfig (which, I know is deprecated, but it currently gets the job done, so I am not changing it).

Since, in Raspberry Pi 3 the internal Wi-Fi chipset is a bcm-based chipset which will not let the following command execute:

iwconfig wlan0 mode ad-hoc channel 6 essid myadhoc txpower 0dBm

With Raspberry Pi 3 the above mentioned command works just by removing dBm from the above mentioned command:

iwconfig wlan0 mode ad-hoc channel 6 essid myadhoc txpower 0

I would like to add a check whether the Raspberry Pi model is 2 or 3 using Bash.

Any hints?

In case, someone wants to go through the bootstrap.sh: Bootstrapping for TWIN

Notes

  • I checked that dBm is not required, also in the case of the Raspberry Pi 2 with the Ralink chipset hence for non-ambiguity one can use the same command for both the Raspberry Pis viz.

    iwconfig wlan0 mode ad-hoc channel 6 essid myadhoc txpower 0
    
  • It is interesting to note that for external Wi-Fi USB dongles one needs to perform the following (for Raspberry Pi 2):

    ifconfig wlan0 down
    iwconfig wlan0 mode ad-hoc channel 6 essid myadhoc txpower 0
    ifconfig wlan0 up
    

    while as for inbuilt Wi-Fi modules (Raspberry Pi 3) there is no need for ifconfig up and down. Just the straightforward iwconfig command works.

  • 4
    The best approach is to test for the thing you want to do instead of looking for models (how will the Raspberry Pi 5 look like?). In this case, see if there is a Ralink RT5370 Chipset and configure accordingly. Same way with the built in Wifi driver. – Thorbjørn Ravn Andersen Feb 9 '17 at 1:06
  • Related: raspberrypi.stackexchange.com/q/1826/19949 – Ghanima Feb 19 '17 at 23:36
29
cat /proc/device-tree/model

returns something like

Raspberry Pi 3 Model B Rev 1.2
21

By CPU Type

You could check the RPi version with the command, uname. The different RPi versions have different CPU architectures. The RPi 2 has an arm7, whereas the 3 has an arm8.

uname -m

By Hardware Revision

If you need to be more specific, you can check the revision entry from the output of cat /proc/cpuinfo. If you want to just exact the revision number, the following command should do it:

cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}'

Revision Numbers

This webpage has a handy chart that I've copied here.

enter image description here

  • 1
    I went through /proc/cpuinfo before. It seems like the best option, since uname -m does not tell much. – Shan-Desai Feb 8 '17 at 17:30
  • 1
    @Shan-Desai: not sure if you saw it, before the edit, but I just included a command that will extract just the revision information from the proc file. Hope that helps. – Jacobm001 Feb 8 '17 at 17:31
  • 1
    Yup I tried the one you mentioned. The comparison was between a Pi 2 Model v1.1 and Pi 3 Model B the funny thing however is that my Pi 3 still shows armv7l upon uname -m – Shan-Desai Feb 8 '17 at 17:37
  • 2
    You mean /proc/cpuinfo not /cpu/procinfo – immibis Feb 8 '17 at 22:52
  • uname will only list the target architecture linux was built for, and is unreliable for determining the CPU Type. Current RPI3 Raspbian runs in 32 bit mode. it will list armv7, if you use an AARCH64 (arm64) linux it will say armv8, if you use the old RPI1 raspbian it will say armv6. – crasic Sep 27 '17 at 0:19
5

There are many methods (of varying reliability) to determine this. One of the most complete and reliable is gpio -v which produces the following output.

gpio version: 2.44
Copyright (c) 2012-2017 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 3, Revision: 02, Memory: 1024MB, Maker: Embest 
  * Device tree is enabled.
  *--> Raspberry Pi 3 Model B Rev 1.2
  * This Raspberry Pi supports user-level GPIO access.

This could be done more elegantly by writing a simple program, using the functions provided by wiringpi. These are well documented, and the source is readily available.

The script in https://raspberrypi.stackexchange.com/a/85016/8697 shows comprehensive information anout your Pi and OS.

2

I created a bash script that will provide the model info based on the Revision.

If you make it better, please let me know.

#!/bin/bash
# which_pi.bash
# BASH Script to display Pi Hardware version based on info found in /proc/cpuinfo
# Andy Delgado - April 11, 2017
# Info gleaned from
# http://www.raspberrypi-spy.co.uk/2012/09/checking-your-raspberry-pi-board-version

REVCODE=$(sudo cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}' | sed 's/^ *//g' | sed 's/ *$//g')

if [ "$REVCODE" = "0002" ]; then
    PIMODEL="Raspberry Pi Model B Rev 1, 256 MB RAM"
fi

if [ "$REVCODE" = "0003" ]; then
    PIMODEL="Raspberry Pi Model B Rev 1 ECN0001, 256 MB RAM"
fi

if [ "$REVCODE" = "0004" ]; then
    PIMODEL="Raspberry Pi Model B Rev 2, 256 MB RAM"
fi

if [ "$REVCODE" = "0005" ]; then
    PIMODEL="Raspberry Pi Model B Rev 2, 256 MB RAM"
fi

if [ "$REVCODE" = "0006" ]; then
    PIMODEL="Raspberry Pi Model B Rev 2, 256 MB RAM"
fi

if [ "$REVCODE" = "0007" ]; then
    PIMODEL="Raspberry Pi Model A, 256 MB RAM"
fi

if [ "$REVCODE" = "0008" ]; then
    PIMODEL="Raspberry Pi Model A, 256 MB RAM"
fi

if [ "$REVCODE" = "0009" ]; then
    PIMODEL="Raspberry Pi Model A, 256 MB RAM"
fi

if [ "$REVCODE" = "000d" ]; then
    PIMODEL="Raspberry Pi Model B Rev 2, 512 MB RAM"
fi

if [ "$REVCODE" = "000e" ]; then
    PIMODEL="Raspberry Pi Model B Rev 2, 512 MB RAM"
fi

if [ "$REVCODE" = "000f" ]; then
    PIMODEL="Raspberry Pi Model B Rev 2, 512 MB RAM"
fi

if [ "$REVCODE" = "0010" ]; then
    PIMODEL="Raspberry Pi Model B+, 512 MB RAM"
fi

if [ "$REVCODE" = "0013" ]; then
    PIMODEL="Raspberry Pi Model B+, 512 MB RAM"
fi

if [ "$REVCODE" = "900032" ]; then
    PIMODEL="Raspberry Pi Model B+, 512 MB RAM"
fi

if [ "$REVCODE" = "0011" ]; then
    PIMODEL="Raspberry Pi Compute Module, 512 MB RAM"
fi

if [ "$REVCODE" = "0014" ]; then
    PIMODEL="Raspberry Pi Compute Module, 512 MB RAM"
fi

if [ "$REVCODE" = "0012" ]; then
    PIMODEL="Raspberry Pi Model A+, 256 MB RAM"
fi

if [ "$REVCODE" = "0015" ]; then
    PIMODEL="Raspberry Pi Model A+, 256 MB or 512 MB RAM"
fi

if [ "$REVCODE" = "a01041" ]; then
    PIMODEL="Raspberry Pi 2 Model B v1.1, 1 GB RAM"
fi

if [ "$REVCODE" = "a21041" ]; then
    # a21041 (Embest, China)
    PIMODEL="Raspberry Pi 2 Model B v1.1, 1 GB RAM"
fi

if [ "$REVCODE" = "a22042" ]; then
    PIMODEL="Raspberry Pi 2 Model B v1.2, 1 GB RAM"
fi

if [ "$REVCODE" = "90092" ]; then
    PIMODEL="Raspberry Pi Zero v1.2, 512 MB RAM"
fi

if [ "$REVCODE" = "90093" ]; then
    PIMODEL="Raspberry Pi Zero v1.3, 512 MB RAM"
fi

if [ "$REVCODE" = "0x9000C1" ]; then
    PIMODEL="Raspberry Pi Zero W, 512 MB RAM"
fi

if [ "$REVCODE" = "a02082" ]; then
    PIMODEL="Raspberry Pi 3 Model B, 1 GB RAM"
fi

if [ "$REVCODE" = "a22082" ]; then
    PIMODEL="Raspberry Pi 3 Model B, 1 GB RAM"
fi

echo "$PIMODEL ($REVCODE)"
  • An else if structure would be far more efficient. – Jacobm001 Apr 11 '17 at 23:23
  • 4
    A switch statement would be better. – Shan-Desai Apr 12 '17 at 8:39
2

I don't have enough rep to comment on @Andy Delgado reply but heres a different version of his code using some newer bash features.

function check_pi_version() {
  local -r REVCODE=$(awk '/Revision/ {print $3}' /proc/cpuinfo)
  local -rA REVISIONS=(
    [0002]="Model B Rev 1, 256 MB RAM"
    [0003]="Model B Rev 1 ECN0001, 256 MB RAM"
    [0004]="Model B Rev 2, 256 MB RAM"
    [0005]="Model B Rev 2, 256 MB RAM"
    [0006]="Model B Rev 2, 256 MB RAM"
    [0007]="Model A, 256 MB RAM"
    [0008]="Model A, 256 MB RAM"
    [0009]="Model A, 256 MB RAM"
    [000d]="Model B Rev 2, 512 MB RAM"
    [000e]="Model B Rev 2, 512 MB RAM"
    [000f]="Model B Rev 2, 512 MB RAM"
    [0010]="Model B+, 512 MB RAM"
    [0013]="Model B+, 512 MB RAM"
    [900032]="Model B+, 512 MB RAM"
    [0011]="Compute Module, 512 MB RAM"
    [0014]="Compute Module, 512 MB RAM"
    [0012]="Model A+, 256 MB RAM"
    [0015]="Model A+, 256 MB or 512 MB RAM"
    [a01041]="2 Model B v1.1, 1 GB RAM"
    [a21041]="2 Model B v1.1, 1 GB RAM"
    [a22042]="2 Model B v1.2, 1 GB RAM"
    [90092]="Zero v1.2, 512 MB RAM"
    [90093]="Zero v1.3, 512 MB RAM"
    [0x9000C1]="Zero W, 512 MB RAM"
    [a02082]="3 Model B, 1 GB RAM"
    [a22082]="3 Model B, 1 GB RAM"
  )

  echo "Raspberry Pi ${REVISIONS[${REVCODE}]} (${REVCODE})"
}

Aside: REVISIONS is defined inside of a function since I use it over ssh i.e. ssh some-host "$(typeset -f); check_pi_version"

-2

Simple way : dmesg | grep "Machine model:"

  • Strange, this doesn't output anything when I try it! – goldilocks Apr 13 '17 at 11:00
  • @goldilocks Displays [Tue Apr 11 15:59:32 2017] Machine model: Raspberry Pi 3 Model B Rev 1.2 on my Pi. May not be the most robust method. – Milliways Apr 13 '17 at 12:21
  • @Milliways The reason it doesn't do this for me is the system has been up too long. That's from boot, and dmesg is a circular buffer. Hence, this is a flawed methodology. – goldilocks Apr 13 '17 at 12:22
  • @goldilocks Presumably the OP only wants to know after boot. It is unlikely to change ;-) There are almost certainly better methods of solving OP problem. – Milliways Apr 13 '17 at 12:33
  • the best solution in my opinion. works perfectly on Raspberry Pi 2 and 3, running Raspbian. I have tested it on a Pi that runs since 8 days. The other solutions in this thread required either new tools (gpio) or you have to map the cpu revision code to a look-up table (and maintain it). that's the only command which tells you exactly the name i.e. Machine model: Raspberry Pi 2 Model B Rev 1.1 - without root too. – Eugen Jul 31 '17 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.