Introducing Raspberry Pi HATs

One of the brilliant things about the Raspberry Pi has always been the ability to attach physical hardware to the Raspberry Pi’s GPIO (General Purpose Input/Output) connector. There are so many third party add-on boards that attach to the Raspberry Pi and extend its functionality, such as motor controllers, LEDs, buttons, sensors, microcontrollers, LCDs, ADCs, and DACs. Someone has almost certainly created an add-on board that makes it usable with the Raspberry Pi.
On the Raspberry Pi models A and B, the GPIO connector has 26 pins. Users attaching an add-board to the model A or B Pi usually have to work out which drivers are required for their specific board, and then edit the relevant Linux files to make them load at boot time before the board is usable, or load them by hand from the command line. The Raspberry Pi has no knowledge of whether it has a board attached or not, and the various drivers, when loaded, will simply assume that they can make exclusive use of the GPIO interface. Most of the time this all works, but it can be a bit challenging for new users. Linux drivers blindly assuming GPIO pins are available can also occasionally cause confusion.

<img src="raspberry_hat.png" alt="raspberry_hat.png">

The Raspberry Pi B+ has been designed specifically with add-on boards in mind and then there are HATs. Hardware Attached on Top (HAT) is an add-on board for Raspberry Pi B+ that conforms to a specific set of rules that will make life easier for users. A significant feature of HATs is the inclusion of a system that allows the B+ to identify a connected HAT and automatically configure the GPIOs and drivers for the board, making life for the end user much easier.
Before any further, it is worth noting that there are obviously a lot of add-on boards designed for the original model A and B boards, which interface to the original 26 way GPIO header. The first 26 pins of the B+ GPIO header are identical to those of the original models, so most existing boards will still work.
In a nutshell a HAT is a rectangular board (65x56mm) that has four mounting holes in the (nicely rounded) corners that align with the mounting holes on the B+, has a 40W GPIO header and supports the special autoconfiguration system that allows automatic GPIO setup and driver setup. The HAT should start working the instant it’s connected. Raspberry Pi achieves this using two specific GPIO pins found on the Model A+, B+, and 2, ID_SD and ID_SC, pins number 27 and 28. These pins are reserved to connect to an EEPROM (Electrically Erasable Programmable Read-Only Memory) chip on the HAT. The EEPROM chip contains code in a .dtf file, known as a ‘device tree’, that outlines all the chips and hardware on the HAT. When a HAT is connected to a Raspberry Pi, the Pi detects it, reads the .dtf file, and difascovers what hardware is attached. Raspbian then loads the required drivers for the hardware automatically, sets up the GPIO pins accordingly, and the HAT is ready to use.
The HAT must contain an EEPROM that details the hardware contained on the board. This allows the Pi to identify a connected HAT and automatically configure the GPIOs and drivers for the board. HATs first arrived with the launch of the Raspberry Pi Model B+ in 2014. Since then, there have been some amazing and wonderful HATs, such as theUnicorn HAT, Piano HAT, Sense HAT, and Explorer HAT.