Raspberry Pi – Sense Hat Python

In this, the fourth part of my Raspberry Pi Computing series, we move onto the addition of accessories. Namely the add-on board called a “Sense Hat”. Don't worry though, if you don't have one the Raspbian OS we installed in Part 2 has an emulator program that is free to use and works the same way.

You may not have a Raspberry Pi, you can still follow along with this web browser emulator on any device: https://trinket.io/sense-hat

It will help you a lot if you have some experience of writing Python script because I'd like to move along briskly. If not, please consider trying my earlier walk-through first:

https://tinyurl.com/yb65cnqd

We start, once again at the Raspbian GUI desktop. If you will be using the emulator program, you can find it by:

Clicking on the Raspberry icon in the top left corner –> Hovering over “Programming” –> Selecting SenseHat Emulator. For convenience you may want to drag and drop it to the right of your display.

Next we shall open the “Thonny Python IDE” program. Usually it opens the last script you wrote, which if you've been following this series, might be “HelloWorld.py”.

In the taskbar of Thonny –> Select “File” –> Select “New”.

You could also use the keyboard shortcut “Ctrl + N”.

To help our new script and our Raspberry Pi to communicate with the Sense Hat or its emulator, we will need to request that Python use a “library”, this library allows us to use functions that wouldn't otherwise work.

For this walk-through I will be demonstrating code for the emulator because it doesn't require you to buy a real one. If you're going to use a real Sense Hat, please remember that whenever I use the term “sense_emu”, you should change this to “sense_hat”.

Click inside the Script Area and type the following:

from sense_emu import SenseHat

By typing “from” we call upon Python to search a library. “sense_emu” is the name of a library for the emulator. The “import” statement asks Python to access a module and that module is “SenseHat” which adds extra functions.

Now we shall add to our code, we'll give the SenseHat() function a simpler name, “sight”.

from sense_emu import SenseHat

sight = SenseHat()

sight.show_message("Show me the money!")

sight.clear(0, 0, 0)

Yeah that's right, I quoted 1996 film, 'Jerry Maguire'.

.show_message()” is a function of the SenseHat module. It lets the Python program know that any quotation marked text wrapped inside the brackets should be displayed on the 8x8 LED array.

The sight.clear(0, 0, 0) function clears the LED array of any pixels still illuminated. “0, 0, 0” refers to the RGB colour-code for black, which means 'off'.

Before running this script, you'll need to save it (shortcut “Ctrl + S”). Remember to include the suffix “.py” so your RPi knows it is reading a Python script. I will name mine MoneyMoney.py.

Back at the Thonny screen, whether you have the emulator or the real Sense Hat fitted, it's now time to see if we have any errors. As our script relies on other libraries and for some of you, a physical accessory, I think it wise to bug test before running. We can debug the script by pressing the button beside the green “Play” button (Ctrl+F5) If all is well, you can stop the debug script.

You can now press the green “Play” button (shortcut F5) and watch the results of your work so far. Believe me, seeing it on the real Sense Hat is very exciting, inspiring you with all the possibilities.

Once done, press the red “Stop” button in the taskbar (Ctrl + F2).

A quirky thing to note is that the final task of a python program is permanent until a new task supersedes it or the RPi is shutdown. Don't believe me? Try changing the last line in your script so it looks like: sight.clear(255, 0, 0) then save and play the script again. Your LED array will be red when finished, hit the “Stop” button... Still red? Yeah... Many a Raspberry Pi has fried that way.

In the script area, add a line of code below the first: import time

, line 4 can be changed to sight.show_message("Money!"), then on line 6 add the code: time.sleep(0.75). We've now added the ability to control delays and speed of our script.

Using parameters inside our .show_message functions, we can choose text colour, background colour, scrolling speed...

text_colour=(x, x, x)” Where “x” is replaced by colour code.

back_colour=(x, x, x)” Where “x” is replaced by colour code.

scroll_speed=(x)” Where “x” is replaced with speed in secs.

You can toy with these parameters however you please but for this tutorial we'll be adding three more lines of code.

Line 7. sight.show_message("Show me the", text_colour=(255, 255, 255))

Line 8. sight.show_message("money!", text_colour=(0, 255, 0), back_colour=(0, 150, 0))

Line 9. sight.clear(0, 0, 0)

Line 7 's text colour “255, 255, 255” is white. Line 8's text colour “0, 255, 0” is green with a background colour that is a darker shade of green. Line 9 clears the LED array, turning it off.

This will be the last time we run the MoneyMoney.py script, perform a debug check and amend any errors you may have had.

The libraries we added also tell the RPi which pins to power and which LEDs to illuminate. The SenseHat() function we renamed to “sight” chooses which LEDs are switched on automatically but in the next part we will be controlling each one manually with our script.

Why not take this time to try out your own code, change the LED text, it's colour and speeds.

For your reference ~ The array uses the RGB Colour Coding system so: “0, 0, 0” is black/off and “255, 255, 255” is white/on. Any numbers between that are achievable, “255, 0, 0” should be a red hue, “150, 0, 0” should be a dimmer shade of red. A combination of “255, 0, 255” appears magenta.

You can read my last post, basic Python coding here:

https://tinyurl.com/yb65cnqd

You can follow me on Twitter at the link below.

https://twitter.com/SASchofield52

Disclaimer: Images are my own, opinions are my own.