The Sense HAT Add-On Board For Raspberry Pi – Operating The LED Display

The Sense HAT Add-On Board For Raspberry Pi – Operating The LED Display

sense hat

sense hat

This is the Sense HAT, an add-on board for Raspberry Pi! Ever since I saw it on the official Raspberry Pi site, I’ve been wanting to try it. At long last, it’s here! It was developed for the Astro Pi project so it’s full of interesting features that can presumably be used in outer space!

We’re going to master using this board referring to the tutorial on the official site. In the first article in this series, we’re going to play with the LED display on the Sense HAT while studying the use of “IDLE” in the Python development environment.

What is the Sense HAT?

Sense HAT – Raspberry Pi

The Sense HAT is an add-on board for Raspberry Pi, made especially for the Astro Pi mission – it’s going to the International Space Station in December 2015 – and is now available to buy.

The Sense HAT has an 8×8 RGB LED matrix, a five-button joystick and includes the following sensors:

  • Gyroscope
  • Accelerometer
  • Magnetometer
  • Temperature
  • Barometric pressure
  • Humidity

We’ve also created a Python library providing easy access to everything on the board.

The above is from the official Raspberry Pi site.

The Sense HAT is an add-on board designed to work with the Raspberry Pi in the Astro Pi on the space station.  It has an 8×8 full-color LED display, a joystick, and 6 different sensors.

The Sense HAT already has a Python library available, so it should be easy to get started with it.

Raspberry Pi Sense Hat – Adafruit

The Raspberry Pi Sense HAT is attached on top of the Raspberry Pi via the 40 GPIO pins to create an ‘Astro Pi’. The Sense HAT has several integrated circuit based sensors can be used for many different types of experiments, applications, and even games. And it’s being used in conjunction with the Raspberry Pi Foundation to perform science experiments aboard the International Space Station (ISS)!

The sensors enable you to read:

  • Orientation (yaw, pitch & roll) via an accelerometer, 3D gyroscope, and magnetometer
  • Pressure
  • Humidity
  • Temperature

With regard to mounting the Sense HAT on the Pi, I ran across the following additional detail on the element14 page.

To mount the Sense HAT securely it is advisable to use a set of standoffs (or spacers-11 mm) and mounting screws (M2.6)

I tried mounting the Sense HAT on just the GPIO pins. While it seemed like the side with the 40 GPIO pins was sturdy enough, the other side felt really flimsy. It feels like it would be dangerous to use the joystick with it mounted like that.

sense hat

Figure 1

Since I didn’t want to wait for the parts to arrive, I decided to make them myself on a 3D printer.

While the spacers themselves are easy enough to make, I wouldn’t really recommend this approach. But desperate times call for desperate measures. Let’s begin!

sense hat

Figure 2

When I plugged in the Raspberry Pi, the white part on the Sense HAT lit up with all sorts of colors!

It looks like it uses a lot of power during startup. So be careful of plugging or unplugging it during startup as this could possibly lead to power shortages.

Installing the Python Library

For this article, our main board is a Raspberry Pi 3 and the OS is Raspbian Jessie (5/27/2016 version).

Raspberry Pi Sense HAT Software Installation – Raspberry Pi Learning Resources

Connect your Sense HAT and boot up the Pi. First update and upgrade your system by entering the following commands into a Terminal window (while connected to the Internet):

sudo apt-get update

sudo apt-get upgrade

Then install the Sense HAT software package:

sudo apt-get install sense-hat

sudo pip3 install pillow

Finally, reboot the Pi to complete the installation:

sudo reboot

 

The 5/27/2016 version of Raspbian that I’m using appears to have it preinstalled.

sense hat

Figure 3

When I ran the install command, it said that the newest version was already installed.  However, if you are using an older version of the OS, it may not be. Run the below command just to be safe.

Install command:

sudo apt-get install sense-hat

Getting started with the Sense HAT

Worksheet – Getting Started with the Sense HAT | Raspberry Pi Learning Resources

We’ll be following the above tutorial on the official Raspberry Pi site throughout the rest of the article.

This tutorial makes use of the Python integrated development environment called IDLE.

25.5. IDLE – Python 3.5.1 Documentation

 

sense hat

Figure 4

Start IDLE from the “Menu” button.  You can select either “Python 2” or “Python 3.”  Let’s choose Python 3.

sense hat

Figure 5

Picture 5 shows the initial screen.  Following the “>>>” in line 4, you can begin entering your program. You can copy and paste but it executes the code one line at a time. (We’ll discuss the steps to copy and paste multiple lines later).

 

 

 

Once I enter the third line and press the “Enter” key, characters begin to show across the LED display on the Sense HAT!

The show_message function makes the character string pass as an argument displaying across the LED display.

show_message

Scrolls a text message from right to left across the LED matrix and at the specified speed, in the specified colour and background colour.

Show_message has 3 parameters.

“scroll_speed” sets the speed at which the characters scroll across the screen. The default is 0.1 and the larger it gets, the slower the characters scroll.

As the names suggest, “text_colour” is the color of the characters and “back_colour” is the background color displayed. These can each be set using RGB values between 0-255. Just like with a full color LED, the default is white characters (255, 255, 255) and black background (0, 0, 0).

sense.show_message(“sample text”, scroll_speed=0.05, text_colour=[255,255,0], back_colour=[0,0,255])

For example, when you run the above line of code, the speed is set to 0.05; the character color to yellow;  and the background to blue.

When the characters finish scrolling across the screen, the background color remains the same, so use the “clear” function to reset the screen to the default.

clear

Sets the entire LED matrix to a single colour, defaults to blank / off.

sense.clear()

For the remainder of the article, it will much more convenient to be able to copy and paste multiple lines of code. So, let’s change the IDLE settings.

Open the “Options-> Configure IDLE” menu and select the configuration screen.

 

sense hat

Figure 6

At the top of the “General” tab, you will find the “Startup Preferences.”  Here, you can change which editor appears at startup. The default selected is the “Open Shell Window”  on the right. When you change this to “Open Edit Window,” you can now enter multiple lines of code into the editor, as the name suggests.

sense hat

Figure 7

The setting changes will be reflected from the next startup. Also, whenever a Python file is opened from “File->Open,” this window is displayed if the “Open Shell Window” option is not selected.

 

If you look closely at this window, you will see that the menu options have changed slightly.  Instead of “Shell” and “Debug,” we now have “Format” and “Run.”  As the name suggests, “Format” has options related to editing the contents of the editor window (e.g. indentation, commenting out, etc).  Similarly, the “Run” drop-down has options related to running the code. To run code, you either select Run->Run Module from the menu or else press F5 key on the keyboard. In order to run the code, you need to make sure to first save the file. Otherwise, you’ll see a pop-up telling you to.

sense hat

Figure 8

When you run code, IDLE automatically opens a shell window, as shown in Figure 8. If a shell window is already open, focus will shift to that window and code execution will start. Now that we’ve got the environment set up to our liking, let’s run some of the code from the tutorial!

/home/pi/sensehat/while.py

The above code demonstrates a while loop. If run like this, it results in an infinite loop.  Closing the shell window will terminate the program. Also, typing Ctrl + C in the LX terminal will also terminate the program.

Next, let’s look at some code using the “show_letter” function.

/home/pi/omg.py

 

show_letter

Displays a single text character on the LED matrix.

show_letter function displays the specified character on the display. The number of seconds to display character is specified using the sleep function. This is very similar to Sonic Pi.

/home/pi/random_omg.py

 

This code uses the random function to change the displayed character color to a random color. This is accomplished by using this function to randomly pick a number between 0-255 for each of the RGB values.

Drawing a picture

The LED display can do more than just displaying characters. The color of each LED can be set individually. This means you can draw a picture on the display!

sense hat

Figure 9: From the official Raspberry Pi site

LEDs are identified using coordinates, starting from the one in the upper left (0,0). For example, if you want to light up LEDs as shown in Figure 9, you can enter the following code.

/home/pi/sensehat/set_pixel.py


set_pixel

Sets an individual LED matrix pixel at the specified X-Y coordinate to the specified colour.

The parameters required for this set_pixel function are the X coordinate, Y coordinate, and color (RGB) separated by commas.

/home/pi/sensehat/simple_image.py

 

sense hat

Figure 10


I made a slight modification to the tutorial source code. I added the “clear” in line 3. The set_pixel function only sets the color of the LED at the specified coordinates so all the other LEDs will remain the same as before (previous state).
So, when using set_pixel, go ahead and include clear function as well. Better safe than sorry.

With that said, doing things this way is a little bit tiresome. Thankfully, we also have the set_pixels function! It has the plural “s” at the end of the function name, which means we can set all of the LED values at once.

 

set_pixels

Updates the entire LED matrix based on a 64 length list of pixel values.

/home/pi/sensehat/rainbow.py

Lines 5-12 store RGB values in preset variables. The “image” variable defined in line 14 holds the individual values for all 64 LEDs in a 2 dimensional array where each letter maps to a set of RGB values from the options defined in lined 5-12. The values are written out as 8 x 8 to match the layout of the LED display and to make it more readable.

Basically, the set_pixels function sets the color for all the LEDs in the display.  Because this function lets you set the values for the unlit parts of the display as well (the variables set to “e” in the above code), you can use it without having to also use the “clear” function when you’re working with the LED display.

sense hat

Figure 11

Changing the Orientation

You can also control the “orientation” of the display on the Sense HAT.

set_rotation

If you’re using the Pi upside down or sideways you can use this function to correct the orientation of the image being shown.

The set_rotation function allows you to change the angle of rotation. The default is 0 degrees, but you can change it in 90 degree increments.

flip_h

Flips the image on the LED matrix horizontally.

flip_v

Flips the image on the LED matrix vertically.

In addition to changing the orientation angle, you can also invert the image on the display.  Use the flp_h function to flip the image horizontally or flip_v to flip vertically.

 

 

 

This video demonstrates what can be done with the LED display when you really put your imagination to work. Using the functions we studied above, you can easily make a program that does stuff like this!

Conclusion

In this first article working with the Sense HAT, we achieved our goal of working with the LED display.

I think its appeal lies in that it’s very easy to work with. All you do is plug it into the GPIO port. Since it doesn’t require any wiring, even a beginner in electronics can easily get started with it.

There are many useful functions already defined for the LED display. There is a great degree of freedom in working with it even though it’s just an 8×8, 64 LED grid.  Since the LEDs are full color, the display can do much more than just display information. It could be incorporated into all sorts of fun projects. I imagine you could even make a game with it!