8

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.

  • 1
    I think this is possible with hats, since they contain an eprom used for setup. You can probably determine the rest with some shell scripting and looking at what is reported by dmesg, lsusb etc. You may also want to consider setting an environment variable that you change when you swap hardware. – Steve Robillard Dec 8 '15 at 22:53
  • @SteveRobillard, do all the hats come with an eprom? Are they not stackable? If so how are the then multiple eprom addressed separately? – Ghanima Dec 8 '15 at 23:04
  • @SteveRobillard Thank you for the suggestion. I've clarified my question to try to say why setting environment variables (and any other keyboard interaction) won't work. I hadn't heard of dmesg and lsusb. Googling them pointed me to Know your Raspberry Pi which looks good too. – Clare Macrae Dec 8 '15 at 23:04
  • @Ghanima I would be really happy to solve this for the one-HAT case, and not worrying about stacking them! – Clare Macrae Dec 8 '15 at 23:05
  • Good read: github.com/raspberrypi/hats – Ghanima Dec 8 '15 at 23:09
4

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: This no longer works, as of 2018-05-17

Unfortunately this mechanism no longer works - I'm investigating in https://github.com/claremacrae/raspi_hat_data/issues/1

  • 1
    Glad this works well for you! Thanks for the real life contents of the device tree. It certainly helps to understand the issue better. – Ghanima Dec 9 '15 at 9:26
3

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.

  • @steverobillard, sorry, could not resist. – Ghanima Dec 8 '15 at 23:38
  • 2
    No problem as long as the user gets an answer - that is all that matters. – Steve Robillard Dec 8 '15 at 23:55
  • Thank you very much both @SteveRobillard and Ghanima - this gave me some really good pointers. – Clare Macrae Dec 9 '15 at 9:11

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.