1 year ago

Get started with Sense HAT

The exciting new Sense HAT sensor add-on for the Raspberry Pi is very easy to use once you know how. We’ll show you how, then.

This tutorial can be found in The MagPi 38

The Sense HAT is an amazing piece of kit and hopefully by now you’ve been able to snag yourself one.
While it’s definitely easy to use, there’s a few things you need to learn first to get it going as it’s reliant on programming it via Python scripts at the moment. The commands are very simple though, so we can show you in only a couple of pages how to start getting the most out of your new toy!

STEP-01 Installing the Sense HAT

Getting the Sense HAT ready to use is quite simple. Turn off your Raspberry Pi and make sure your Raspbian SD card is inserted if it isn’t already. Place the Sense HAT on the GPIO pins, carefully aligning them before pressing down firmly so that it properly attaches to the Raspberry Pi. Once that’s done, turn the Raspberry Pi back on. If it’s attached properly, the LEDs on the SenseHAT will light up in a rainbow pattern during boot time. When it gets to the desktop, the pattern might turn off but that’s normal.

STEP-02 Sense HAT library

While the Sense HAT is now accessible, if you’re using an older version of Raspbian it’s not fully functional as it is. You’ll need to download a custom script if you’re not on Raspbian jessie that will install the necessary libraries for Python to access it, and update some of the core kernel code so that it runs properly. You can download and install the update by opening the terminal and running these two commands:

After both commands have run their course (they might take a few minutes each to complete) you’ll need to reboot the Raspberry Pi. You might as well do this from the command line with:

STEP-03 Get started with Python

We can start controlling the Sense HAT by writing code in Python. As the Sense HAT is using the GPIO pins, we need to have administrative privileges to execute the scripts. This means opening IDLE using sudo in the command line with:

Open up a new window to start writing a Python script. Each Sense HAT script needs to begin with two lines to import the relevant Python module, and then a line to turn it into a variable that makes writing the code easier.

STEP-04 Text and pictures

The easiest thing to start off with is to get text scrolling along the LED display. The library for the Sense HAT will automatically turn a string of words into a scrolling banner across the matrix. A line like the following will create it:

You can control the scroll speed and colours as well. You can also manipulate the individual LEDs by telling the script which pixel to change, and give it an RGB colour variable with something like:

Put this in a variable set the top left corner pixel as blue

STEP-05 Environmental sensors
Using two of Sense HAT’s sensors, you can measure temperature (in degrees C), pressure (in millbars) and relative humidity (as a percentage). You can get the three individual measurements with:

You’ll have to attach them to a variable thought (e.g., t = sense.get_temperature()) so that you can print out the reading or display it on the LED matrix.

STEP-06 Motion sensing

The gyroscope, accelerometer and magnetometer can be used to detect motion in real time. This is a little trickier to use than the other parts, as it involves understanding how the three-dimensional values represent the orientation of the Raspberry Pi, and then how the accelerometer can be used to figure out the movement within space. Both the gyroscope and accelerometer functions (get_orientation().values() and get.accelerometer_raw().values() respectively) will return three values from which you can use however you please.

The motion sensor guide on GitHub will explain all you need to know on the motion sensors.

  • Stuart Weenig

    Is the library installation still required with Jessie?

  • Darren Cassidy

    I have a sense hat on a dedicated Pi. When I installed Jessie I didn’t need to re-install the library. So I think it’s now part of the distro. Hope this helps.

  • Updated!

  • Jerker Gustafson

    Got a sense hat and is absolutelly thrilled with the possibillities but when I attach the sense hat to the Pi, how do I get access to the GPIO pins 5, 6, 12, 13, 16, 20, 21 and 26 the best way? Basically it is the pins 29 through 40 I need to access.
    Is there a solution to access those pins in a convinient way?

  • Anonymous

    You can access the HAT using the python library. Did you read the sensors using neurofuzzy classification, a processor + sensors physics estimate, a mathematical interpolation, a calculated heatmap simulation or any of the above? Because the sensor churns -5,+5 degrees with an apparent Kelvin constant roundabout the actual temperature. You would need to pre-calibrate using a combination of the above in order to get the right temperature; I don’t think it’s a friendly sensor.