9

I am setting up some Raspberry Pis for use in demos to colleagues, in order to show off capabilities of the Pi itself, and of some of the add-on boards that are available.

I would really like to be able to maintain one cron-job/start-up script for all the possible demos I'm setting up, and have that script detect what HAT or older GPIO board is plugged in, so that I can trigger an appropriate demo script/program.

For example,

  • If a SenseHAT is attached, then auto-start a Sense HAT Marble Maze
  • If a Display-O-Tron HAT is attached, then auto-run the menu example
  • If a PiTFT screen is attached, then auto-start a photo slide show

Is there any way to run Python, sh or similar, to detect which of some common GPIO boards is connected to a Pi?

Things I considered:

  • Using information from Pinout.xyz to detect the pins in use, but that sounds like a lot of work.

Note that my intention is to auto-detect the configuration, without needing to attach a keyboard, ssh in, or similar - hence the wish for auto-detection, and why something like setting an environment variable wouldn't help.

goldilocks
  • 60,325
  • 17
  • 117
  • 234
Clare Macrae
  • 285
  • 2
  • 11

2 Answers2

6

Having read through the very helpful earlier comments and answers from Steve Robillard and Ghanima:

/proc/device-tree/hat/product

If /proc/device-tree/hat/product exists, you have a HAT attached and loaded - and that file contains the name of HAT.

Here are some sample outputs:

Display-o-Tron HAT

cat /proc/device-tree/hat/product
Display-o-Tron HAT

Names and content of all the files in /proc/device-tree/hat/ for this device:

name: hat
product: Display-o-Tron HAT
product_id: 0x0007
product_ver: 0x0001
uuid: 666dfe9b-9d78-4825-bbfe-1697048fc6cd
vendor: Pimoroni Ltd.

Adafruit PiTFT Plus - 3.5 inch

cat /proc/device-tree/hat/product
Adafruit PiTFT Plus - 3.5 inch Resistive

Names and content of all the files in /proc/device-tree/hat/ for this device:

name: hat
product: Adafruit PiTFT Plus - 3.5 inch Resistive Touch
product_id: 0x0000
product_ver: 0x0000
uuid: 684cdc28-d27f-4065-9d11-bb3f3463786d
vendor: Adafruit Industries

Update: 2018-05-17

Some devices are marketed as HATs but don't have an EEPROM with a valid data blob, in which case, there is no way to detect the "HAT" type, as it doesn't match the HAT specification.

Update: 2020-01-07

I created a github repo with a script to obtain the data for the HATs I had access to, and to record those which are not really HATs:

https://github.com/claremacrae/raspi_hat_data

Pull requests are welcome - just run the copy_hat_data.sh script in that repo.

Clare Macrae
  • 285
  • 2
  • 11
4

Ripping off SteveRobillards excellent comment:

Use the HAT's I2C EEPROM

The Raspberry Pi Blog points for HAT specification to GitHub where both documentation and software tools (eepromutils) for manipulation are available.

README.md:

The ID EEPROM contains data that identifies the board, tells the B+ how the GPIOs need to be set up and what hardware is on the board. This allows the add-on board to be automatically identified and set up by the Pi software at boot time including loading all the necessary drivers.

Information of the HAT is reflected in the device tree /proc/device-tree/hat that could be read from user-space in any of the mentioned ways (python, sh, ...).

Note that there are no stacked HATs* (per Raspberry Pi Blog):

Stackable HATs featured in the specification discussion – but eventually it was thrown out due to the large increase in complexity of autoconfig and potential for user error.

So at any given time only one HAT will be connected, have its EEPROM read out, and made its information available at the device tree.


* It is possible to make stackable hats if they are of the same type and thus do not require multiple and differing identification, e.g. the Adafruit 16-Channel PWM/Servo HAT for Raspberry Pi.
Ghanima
  • 15,958
  • 17
  • 65
  • 125