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.
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:
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:
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.
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!
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.
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.
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
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
Figure 4
Start IDLE from the “Menu” button. You can select either “Python 2” or “Python 3.” Let’s choose Python 3.
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).
1 2 3 |
from sense_hat import SenseHat sense = SenseHat() sense.show_message("Hello my name is Tim Peake") |
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.
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.
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.
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
1 2 3 4 |
from sense_hat import SenseHat sense = SenseHat() while True: sense.show_message("Astro Pi is awesome!!", scroll_speed=0.05, text_colour=[255,255,0], back_colour=[0,0,255]) |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from sense_hat import SenseHat import time sense = SenseHat() sense.show_letter("O",text_colour=[255, 0, 0]) time.sleep(1) sense.show_letter("M",text_colour=[0, 0, 255]) time.sleep(1) sense.show_letter("G",text_colour=[0, 255, 0]) time.sleep(1) sense.show_letter("!",text_colour=[0, 0, 0], back_colour=[255, 255, 255]) time.sleep(1) sense.clear() |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
from sense_hat import SenseHat import time import random sense = SenseHat() r = random.randint(0,255) g = random.randint(0,255) b = random.randint(0,255) sense.show_letter("O",text_colour=[r, g, b]) time.sleep(1) r = random.randint(0,255) g = random.randint(0,255) b = random.randint(0,255) sense.show_letter("M",text_colour=[r, g, b]) time.sleep(1) r = random.randint(0,255) g = random.randint(0,255) b = random.randint(0,255) sense.show_letter("G",text_colour=[r, g, b]) time.sleep(1) sense.show_letter("!", text_colour=[0, 0, 0], back_colour=[255, 255, 255]) time.sleep(1) sense.clear() |
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.
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!
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
1 2 3 4 5 6 |
from sense_hat import SenseHat sense = SenseHat() sense.set_pixel(0, 2, [0, 0, 255]) sense.set_pixel(7, 4, [255, 0, 0]) |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
from sense_hat import SenseHat sense = SenseHat() sense.clear() sense.set_pixel(2, 2, [0, 0, 255]) sense.set_pixel(4, 2, [0, 0, 255]) sense.set_pixel(3, 4, [100, 0, 0]) sense.set_pixel(1, 5, [255, 0, 0]) sense.set_pixel(2, 6, [255, 0, 0]) sense.set_pixel(3, 6, [255, 0, 0]) sense.set_pixel(4, 6, [255, 0, 0]) sense.set_pixel(5, 5, [255, 0, 0]) |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from sense_hat import SenseHat sense = SenseHat() r = [255,0,0] o = [255,127,0] y = [255,255,0] g = [0,255,0] b = [0,0,255] i = [75,0,130] v = [159,0,255] e = [0,0,0] image = [ e,e,e,e,e,e,e,e, e,e,e,r,r,e,e,e, e,r,r,o,o,r,r,e, r,o,o,y,y,o,o,r, o,y,y,g,g,y,y,o, y,g,g,b,b,g,g,y, b,b,b,i,i,b,b,b, b,i,i,v,v,i,i,b ] sense.set_pixels(image) |
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.
Figure 11
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!
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!