Friday 19 February 2016

Raspberry Pi HATs

Raspberry Pi HATs

The Raspberry Pi is an excellent hardware hacking computer. Take your Pi, connect a solder-free breadboard to the General Purpose Input/Output (GPIO) port, plug in a few electronic components, and a whole new world opens up.

The possibilities offered by hardware hacking are difficult to resist. However, not everyone has the inclination or time to construct electronic circuits from scratch. And this is why Raspberry Pi addon boards have become so popular.


Pi Add-on Boards


As Raspberry Pi sales climbed year on year, electronic designers saw a golden opportunity to extend the Pi’s functionality by adding LEDs, buttons, joysticks, sensors, motor controllers, analogue-to-digital convertors, mini-LCD displays and much more.

So they began creating add-on boards that simply snapped onto the Model B or Model A using its 26-pin GPIO connector. With an add-on board it’s so much easier to create a home automation system, build a security camera, construct a weather station or control a motorised robot.

However, it’s not quite as straight-forward as it might at first seem. In fact, there are a number of tricky hurdles to overcome.

First there’s the driver problem. You have to identify and obtain the drivers associated with each add-on board. Then you either load them by hand, from the Linux command line or carefully edit the relevant Linux files so they load at boot time – quite a challenge for novice Pi owners.

Another issue is that when loaded, these drivers assume they can make exclusive use of the GPIO interface. But as this isn’t always the case, it can lead to quite a few head-scratching problems.

The root cause of these problems is that a Raspberry Pi can’t easily determine how best to communicate with an addon board, or even identify whether one is currently attached. This means it’s impossible to assist novice users with driver selection and configuration suggestions.

The Raspberry Pi Foundation realised it could improve this situation significantly and in the process open up the Pi hardware hacking experience to a wider audience.

HAT Birth


In July 2014, the Raspberry Pi Foundation introduced an updated version of the original Model B. Named the Model B+, it had a number of useful enhancements (goo.gl/s5asMa). One of the most significant was an extended 40-pin GPIO connector.

The reason for these extra pins became apparent just a few weeks later when the Hardware Attached On Top (HAT) initiative was announced. For the first time add-on board designers, had an officially endorsed specification that, if adopted, would make Pi hardware hacking much easier.

Essentially, every HAT conforms to a specific set of rules. These rules enable the Pi to detect the presence of a HAT and automatically configure the GPIOs and drivers for the board. In other words, a Pi owner only has to push a HAT onto the GPIO connector and boot up the Pi.

From a technical perspective, the autoconfiguration is down to GPIO pins ID_SD and ID_SC, which are reserved for an I2C EEPROM. The HAT’s EEPROM holds the board manufacturer information, GPIO setup and a hardware description that Linux uses to automatically load the required drivers.

To ensure everyone has an opportunity to design and build their own unique board, the complete HAT technical specification, including circuit diagrams, is open source (github.com/raspberrypi/hats).

HAT Zoo


Just like an add-on board, a HAT enhances your Pi with a new collection of capabilities. But this time the board is installed and working in minutes, so you can have all the fun without doing those tedious configuration tasks, which are so easy to get wrong.

Each certified HAT board must adhere to the official specification, have the same basic rectangular 65 x 56mm size, a 40-pin GPIO connector and four mounting holes that align with the mounting holes on the Pi.

But that’s where the similarities stop. Each one has its own specific purpose and set of functionality. Some encourage circuit building experimentation. Others add a collection of environmental sensors, control electric motors or interface with Pi accessories, such as TFT screens.

There are many places to buy a HAT. Take the popular Pimoroni website, for example. As I write, it already has 27 different HATs on its virtual shelves (goo.gl/2efx2v) and new ones are added quite regularly. The Pi Hut also has over 20 HATs (goo.gl/MGJWj2) as does Adafruit (goo.gl/Fs9yrr).

With a HAT it’s much easier to attempt maker-style projects. Examples include light-sensitive mood-lighting devices, garden weather stations, motion-sensor security systems, time-lapse video surveillance setups and all kinds of robots.

Now let’s take a closer look at some of the more popular HATs.

Explorer HAT

Explorer HAT


The Explorer HAT from Pimoroni is all about smoothing your introduction to electronic circuit building. This HAT snaps a miniature breadboard directly onto your Pi, so there’s no need for connecting wires. This arrangement is also ideal for maker-style projects where space is at a premium, such as small robots.

The Explorer HAT actually comes in two versions: the £10 Standard and the £18 Pro. The cheaper Standard HAT has four buffered 5V tolerant inputs and outputs, four capacitive touch pads, four capacitive alligator clip pads, four coloured LEDs and that mini breadboard. The Pro HAT adds four analogue inputs (for things like thermometer sensors), two 5V bidirectional motor drivers and an array of 3.3V GPIO connectors.

As for the coding side of things, Python coders can install the Explorer Craft library, which makes HAT interaction a much simpler task. There’s no limit to the things you can do with an Explorer HAT, but here are a few ideas to get started:

• An LED light puzzle game using the Pro’s touch pads (goo.gl/gkhGxC).
• A PIN-secured alarm system (goo.gl/5uYKOb).
• A circuit that uses an analogue temperature sensor and displays the data via the Pi’s Minecraft Python programming interface (goo.gl/92XUnm).
• A USB keyboard robot controller using the PyUSB module (goo.gl/5uYKOb).

Sense HAT

Sense HAT


The Sense HAT is one of the more expensive products, at around £25. However, it’s a true multi-functional beast and therefore offers excellent value for money. Here’s the feature list:

• Gyroscope
• Accelerometer
• Magnetometer
• Temperature sensor
• Humidity sensors
• Barometric pressure sensor
• 8x8 RGB LED display
• Mini five-button joystick

The gyroscope measures the Pi’s orientation as angles between 0 and 360 degrees, in terms of pitch (up and down), yaw (left and right) and roll (like a corkscrew motion). The accelerometer measures the Pi’s acceleration and also detects the direction of gravity, so it knows about the Pi’s orientation. The magnetometer measures the strength and direction of a magnetic field.

Climatic conditions are captured via a temperature sensor that acts as a Celsius thermometer. The humidity sensor detects the amount of airborne water vapour, and the barometric pressure sensor measures forces exerted by tiny molecules in the surrounding air, which change depending on altitude.

On-board visual feedback is provided via an LED matrix display in the form of an 8x8 grid. Each LED has a red, green and blue component, fully controllable through code. Combine red, green and blue values to create any colour or shade. These 64 RGB LEDs can display sensor data visualisations or real-time, multi-colour animations.

The mini joystick has five direction buttons – up, down, left, right and centre – just as you’d find on a typical games console controller. Thanks to the HAT auto-configuration, they’ll already be mapped to the four keyboard cursor keys plus the return key.

As for coding, Scratch fans will find the ‘SenseHAT’ add-on block easy to use, while Python coders have the ‘sense_hat’ module (pythonhosted.org/sense-hat), as shown in the code fragment below:

from sense_hat import SenseHat
sense = SenseHat()
t = sense.temperature
h = round(sense.get_humidity(),1)
p = round(sense.get_pressure(),1)

With so much flexibility, it’s no surprise British astronaut Tim Peake took a couple of Sense HAT-fitted Raspberry Pis with him as part of the Astro Pi project during his six month stay on the International Space Station (ISS).

However, you could cook up some scientific experiments yourself. The Astro Pi website has both Python and Scratch tutorials (astro-pi.org/get-involved/program-the-sensehat), plus there’s plenty of Sense HAT examples online, such as the fun spacecentric Scratch coding tutorial by Milton De Paula (goo.gl/efjKzI).

Skywriter HAT

Skywriter HAT


If you’d like to upgrade your Raspberry Pi with something that’s both unusual and fun, then the £16 Skywriter HAT might fit the bill. Once installed, the Skywriter can sense the position of your fingers in the air above its flat metal plate.

To establish this intriguing capability, it generates an electrical field above the metal plate. The strength and shape of this field is remodelled as you wave your hand and fingers around. Skywriter’s built-in sensors capture these field changes, then the HAT driver software converts them into 3D positional data and meaningful gestures.

Even better, the Skywriter HAT has a sensing distance of up to five centimetres, so you could mount it in stealth-mode behind a sheet of non conductive material – for example, an acrylic or fabric cover. Hidden from sight in this way, it becomes a kind of techno-magic illusion.

Use the associated Python library to read off the positional data as x, y and z coordinates. The same Python library can identify common gestures like taps, double taps, flicks and side-to-side swipes. All the Python code is open source and available to download from GitHub (github.com/pimoroni/skywriter-hat).

What you can do with a Skywriter HAT is really only limited by your imagination. Some of the more obvious examples include a mouse replacement to control your Pi’s user interface, a custom 3D game controller or a Python app that simulates the theremin musical instrument. But I’m sure you can think of many other interesting possibilities.

Pi Zero HATs


The Pi Zero has undoubtably kicked off a new wave of interest in all things Raspberry Pi, and priced at just £4, there’s more money to spend on accessories. While a few things are omitted on the Pi Zero to attain that amazingly low price, it does retain the same HAT-compatible 40-pin GPIO connector found on the Pi 2, Model B+ and Model A+.

However, the board itself is much smaller in size. This means a standardsized add-on board or HAT rather overshadows the diminutive Zero board. Apart from aesthetics issue, a bigger problem is that the mounting holes don’t line up any more.

To fix this problem, a number of manufacturers have introduced a range of Zero-sized add-on boards. We’d also expect a range of made-for-Zero HATs to appear pretty soon. And if the Raspberry Pi Foundation is reading this, a few million Pi Zero boards would be welcome too!

Summing Up


The HAT specification is a game changer in the Raspberry Pi hardware hacking game. The ease of installation and quickstart features are a boon to any Pi owner with limited Linux knowledge, and it’s especially suited to students, parents and teachers.

Add a HAT to your Pi and open the door to myriad creative possibilities, where there are new things to explore, new skills to learn, new code to write and new Pi-centric dreams to realise.

Have fun and let us know what you create.


HAT Links


Pimoroni: goo.gl/2efx2v
The Pi Hut: goo.gl/MGJWj2
Adafruit: goo.gl/Fs9yrr
Astro Pi: astro-pi.org

Sense HAT Projects


Martin O’Hanlon: goo.gl/Sh8AQL
Ben Nuttall: goo.gl/97YNvs
Ben Nuttall (8x8 LEDs): goo.gl/ubjrzg
Dave Honess: goo.gl/M0XVV8

Astro Pi


The Astro Pi project (astro-pi.org) clearly demonstrates the power of the HAT concept. Adding a Sense HAT and a few other inexpensive electronic components transforms the humble Raspberry Pi into a high-quality scientific instrument.

So, what exactly is an Astro Pi device and can you own one? Well, it’s built on a standard Raspberry Pi 2 board with a Sense HAT attached, plus the official camera module, an infra-red camera module (the official camera module minus its infrared filter) and a custom designed flight case (astro-pi.org/about/hardware).

So yes, you can own an Astro Pi because, except for the 6063 grade aluminium flight case (only eight were made in total), everything is available to buy. And you can easily purchase a suitable case, construct one yourself or create a design and have it 3D printed.

Two Astro Pi devices were flown to the ISS. During Tim Peake’s mission, they’ll become scientific instruments that measure the environment inside the station, reveal how it’s moving through space and detect the earth’s magnetic field.

The low cost nature of the Astro Pi hardware offered a unique learning opportunity. Last year, students from schools from all over the UK took part in an Astro Pi competition (astro-pi.org/competition). The challenge was to create the best scientific Python-coded programs for the Astro Pi.

In the end, seven winners were chosen, spanning a number of different age groups. The programs range from game-like reaction time tests to monitoring radiation levels inside the ISS (astro-pi.org/competition/winners). To find out more and to try these programs for yourself, visit the Astro Pi GitHub site (github.com/astro-pi) and download the source code.

Tim Peake will run each winning program inside the ISS European Columbus module, before downloading the results back to earth for all to see. Even when Tim’s mission has finished, a long-term ISS environmental monitoring programme will continue to run these experiments, adding the recorded sensor data to a date and time-stamped CSV file.