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.

6
  • 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. Commented Dec 8, 2015 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
    Commented Dec 8, 2015 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. Commented Dec 8, 2015 at 23:04
  • @Ghanima I would be really happy to solve this for the one-HAT case, and not worrying about stacking them! Commented Dec 8, 2015 at 23:05
  • Good read: github.com/raspberrypi/hats
    – Ghanima
    Commented Dec 8, 2015 at 23:09

2 Answers 2

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.

4
  • 2
    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
    Commented Dec 9, 2015 at 9:26
  • It still works! The issue you linked on github was just user error. Commented Jan 7, 2020 at 7:24
  • Thanks @PaulSlocum - I've updated this, and made the repo about this more obvious.... If you try it out on any HATs I didn't have data for, I'd really appreciate your adding it... Let me know if you need more info.... Commented Jan 7, 2020 at 11:54
  • I am getting exact same information from /sys/firmware/devicetree/base/hat if there is a hat on plugged into the RPi. Which one is recommended?
    – testuser
    Commented Apr 19, 2020 at 8:50
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.

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

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.