60

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.

5

7 Answers 7

109
cat /proc/device-tree/model

returns something like

Raspberry Pi 3 Model B Rev 1.2
1
  • 3
    This still works perfectly in March 2022. It returns Raspberry Pi Zero 2 W Rev 1.0 on my, well, Pi Zero 2 W.
    – appel
    Commented Mar 7, 2022 at 17:46
23

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

7
  • 1
    I went through /proc/cpuinfo before. It seems like the best option, since uname -m does not tell much.
    – Shan-Desai
    Commented Feb 8, 2017 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
    Commented Feb 8, 2017 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
    Commented Feb 8, 2017 at 17:37
  • 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
    Commented Sep 27, 2017 at 0:19
  • A little update the Foundation has all the current revision codes listed here raspberrypi.org/documentation/hardware/raspberrypi/… including those that are newer than this answer.
    – Ghanima
    Commented Mar 17, 2018 at 20:41
11

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 about your Pi and OS.

3

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 "$(declare -f); check_pi_version"

1
  • There is an error with the leading 0 in 0008 and 0009 as it is interpreted as an octal number I think. Commented Oct 5, 2021 at 19:00
3

You could also use tool called pinout

enter image description here

1
  • Whilst this might be a great tool, your answer doesn't meet the guidelines for what an answer requires. Please update it with details on how to install the tool and the command to run to get this response. Thanks. Commented Dec 24, 2021 at 12:39
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)"
2
  • An else if structure would be far more efficient.
    – Jacobm001
    Commented Apr 11, 2017 at 23:23
  • 4
    A switch statement would be better.
    – Shan-Desai
    Commented Apr 12, 2017 at 8:39
-2

Simple way : dmesg | grep "Machine model:"

5
  • Strange, this doesn't output anything when I try it!
    – goldilocks
    Commented Apr 13, 2017 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
    Commented Apr 13, 2017 at 12:21
  • 1
    @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
    Commented Apr 13, 2017 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
    Commented Apr 13, 2017 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
    Commented Jul 31, 2017 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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