Get started with Sense HAT

By Russell Barnes. Posted

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:

$ sudo apt-get install sense-hat
$ sudo pip-3.2 install pillow

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:

$ sudo reboot

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:

$ sudo idle3 &

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.

from sense_hat import SenseHat

sense = SenseHat()

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:

sense.show_message(“Hello World!”)

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:

sense.set_pixel(0, 0, [0, 0, 255])

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:

sense.get_temperature()
sense.get_pressure()
sense.get_humidity()

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 (getorientation().values() and get.accelerometerraw().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.

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.