Fun with a Sense-Hat

Just before the Raspberry Pi 3 came out I brought myself a Sense HAT – after all who doesn’t like lots of flashing LEDs. I also brought a Pibow case and some nylon spacers.

Installing the Sense HAT is quite straight forward, and it fits neatly inside the Pibow case, though I did find that I needed to shorten the nylon spacers. They may need to be 11mm long to support a HAT normally but when using a Pibow case the mounting holes are covered by the third layer so they need to be shortened. One of the advantages of using nylon spacers is that this is easily done with a sharp chisel (and an old magazine, which I used to protect my desk!).

Unfortunately though once you have put the lid on you can’t access the joystick, to fix this little problem I’ll probably get a diffuser layer and then try to (very carefully) drill a hole in it later.

If you are using a minimal install then in order to be able to do very much you also need to install python and the additional Sense HAT python library. As usual to do this you need to be running as a super user.

$ su
Password:
#

OR

$ sudo -i
Password:
#

Then you need to install the additional packages needed by the Sense HAT.

# apt-get install sense-hat python-sense-hat raspberrypi-bootloader
Reading package lists… Done
Building dependency tree      
Reading state information… Done
The following extra packages will be installed:
  dh-python libblas-common libblas3 libfreetype6 libgfortran3 libjbig0
  libjpeg62-turbo liblapack3 liblcms2-2 libmpdec2 libpng12-0 libpython-stdlib
  libpython2.7-minimal libpython2.7-stdlib libpython3-stdlib
  libpython3.4-minimal libpython3.4-stdlib librtimulib-dev librtimulib7
  libsqlite3-0 libtiff5 libwebp5 libwebpdemux1 libwebpmux1 mime-support python
  python-minimal python-numpy python-pil python-rtimulib python2.7
  python2.7-minimal python3 python3-minimal python3-numpy python3-pil
  python3-rtimulib python3-sense-hat python3.4 python3.4-minimal
Suggested packages:
  liblcms2-utils python-doc python-tk gcc gfortran python-dev python-nose
  python-numpy-dbg python-numpy-doc python-pil-doc python-pil-dbg
  python2.7-doc binutils binfmt-support python3-doc python3-tk python3-venv
  python3-dev python3-nose python3-numpy-dbg python3-pil-dbg python3.4-venv
  python3.4-doc
Recommended packages:
  file librtimulib-utils
The following packages will be REMOVED:
  libraspberrypi-bin libraspberrypi0 raspberrypi-bootloader-nokernel
The following NEW packages will be installed:
  dh-python libblas-common libblas3 libfreetype6 libgfortran3 libjbig0
  libjpeg62-turbo liblapack3 liblcms2-2 libmpdec2 libpng12-0 libpython-stdlib
  libpython2.7-minimal libpython2.7-stdlib libpython3-stdlib
  libpython3.4-minimal libpython3.4-stdlib librtimulib-dev librtimulib7
  libsqlite3-0 libtiff5 libwebp5 libwebpdemux1 libwebpmux1 mime-support python
  python-minimal python-numpy python-pil python-rtimulib python-sense-hat
  python2.7 python2.7-minimal python3 python3-minimal python3-numpy
  python3-pil python3-rtimulib python3-sense-hat python3.4 python3.4-minimal
  raspberrypi-bootloader sense-hat
0 upgraded, 43 newly installed, 3 to remove and 0 not upgraded.
Need to get 47.1 MB of archives.
After this operation, 166 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
  :
  :
  :
Setting up python3-sense-hat (2.1.0-1) …
Setting up sense-hat (1.2) …
Enabling I2C…
Processing triggers for libc-bin (2.19-18+deb8u3) …
#

When all that is done you can test it using one of the Python examples.

# ls /usr/src/sense-hat/examples/python-sense-hat
colour_cycle.py  pygame_joystick.py  rotation.py        space_invader.py
compass.py       rainbow.py          space_invader.png  text_scroll.py
# /usr/src/sense-hat/examples/python-sense-hat/colour_cycle.py

Or you could try the one of the scripts below, the first just every pixel to a random colour then waits briefly before updating the display again, while the second displays a rather pretty scrolling rainbow. Both show dim the display to avoid blinding yourself; demonstrate how to store the pixel data in a list of lists which allow the display to be updated using a single call to ‘set_pixels’ method which updates all the LEDs at the same time; and how to handle Ctrl-C gracefully with an event handler that resets each pixel on the display when the user presses Ctrl_C to exit.

#!/usr/bin/python
#
# pi-sense-blinken-leds.py
#
# Sets  every  pixel on the Sense HAT to a random colour - Also shows  how  to
# handle Ctrl-C gracefully with an event handler that resets each pixel on the
# display when the user presses Ctrl_C to exit.
#
# This  program  is  free software: you can redistribute it and/or  modify  it
# under  the terms of the GNU General Public License as published by the  Free
# Software  Foundation,  either version 3 of the License, or (at your  option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but  WITHOUT
# ANY  WARRANTY;  without  even  the implied warranty  of  MERCHANTABILITY  or
# FITNESS  FOR A PARTICULAR PURPOSE.  See the GNU General Public  License  for
# more details.
#
# You  should  have  received a copy of the GNU General  Public License  along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import sense_hat
import random
import time
import signal

# Signal hangler to trap Ctrl_C events.
def signal_handler(signal, frame):
  _sense.clear([0, 0, 0])
  raise SystemExit

# Instantiate sense object before setting up signal handler as the 
# signal handler uses it to clear all the pixels.
_sense = sense_hat.SenseHat() 

# Set up signal handler to catch Ctrl-C.
signal.signal(signal.SIGINT, signal_handler)

# Dim the display
_sense.low_light = True

# Instantiate list of lists (to hold pixel values)
_pixels = [[0, 0, 0] for _range in range(64)]

while True:
  for _count in range (64):
    _red = random.randint(1, 192)
    _blue = random.randint(1, 192)
    _green = random.randint(1, 192)
    # Set the pixel colour using the random RGB (red, green, blue) values.
    _pixels[_count] = [_red , _blue, _green]

  # Update the display.
  _sense.set_pixels(_pixels)

  # Pause briefly, then do it all again!
  time.sleep(0.2) 

Note – This script above has been rewritten since I first posted this article so as using the ‘set_pixels’ makes the LEDs flicker less than when updated individually.

#!/usr/bin/python
#
# pi-sense-rainbow.py
#
# Paints a scrolling rainbow of colours on the sense-hat display. Based on the
# sense-hat example script 'colour_cycle.py'.
#
# This  program  is  free software: you can redistribute it and/or  modify  it
# under  the terms of the GNU General Public License as published by the  Free
# Software  Foundation,  either version 3 of the License, or (at your  option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but  WITHOUT
# ANY  WARRANTY;  without  even  the implied warranty  of  MERCHANTABILITY  or
# FITNESS  FOR A PARTICULAR PURPOSE.  See the GNU General Public  License  for
# more details.
#
# You  should  have  received a copy of the GNU General  Public License  along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import sense_hat
import random
import time
import signal

# Signal handler to trap Ctrl_C events.
def signal_handler(signal, frame):

  _sense.clear([0, 0, 0])
  raise SystemExit

def next_colour(_colour):
  if (_colour[0] == 192 and _colour[1] < 192 and _colour[2] == 0):
    _colour[1] += 48
  if (_colour[1] == 192 and _colour[0] > 0 and _colour[2] == 0):
    _colour[0] -= 48
  if (_colour[1] == 192 and _colour[2] < 192 and _colour[0] == 0):
    _colour[2] += 48
  if (_colour[2] == 192 and _colour[1] > 0 and _colour[0] == 0):
    _colour[1] -= 48
  if (_colour[2] == 192 and _colour[0] < 192 and _colour[1] == 0):
    _colour[0] += 48
  if (_colour[0] == 192 and _colour[2] > 0 and _colour[1] == 0):
    _colour[2] -= 48
  return _colour

# Instantiate sense object before setting up signal handler as the 
# signal handler uses it to clear all the pixels.
_sense = sense_hat.SenseHat() 

# Set up signal handler to catch Ctrl-C.
signal.signal(signal.SIGINT, signal_handler)

# Dim the display.
_sense.low_light = True

# Rotate the display so it is the 'right' way up.
_sense.set_rotation(0)

# Instantiate list of lists (to hold pixel values).
_pixels = [[0, 0, 0] for _range in range(64)]

_colour = [192, 0, 0]
while True:
  for _count in range(56,64):
    _pixels[_count] = [_colour[0], _colour[1], _colour[2]]

  for _count in range(56):
    _pixels[_count] = _pixels[_count + 8]

  _colour = next_colour(_colour)

  # Update the display.
  _sense.set_pixels(_pixels)

  # Pause briefly, then do it all again!
  time.sleep(0.04) 

Enjoy the sparkles!


Raspberry Pi is a trademark of the Raspberry Pi Foundation

This entry was posted in Hardware, Linux, Programming, Raspbian and tagged , , . Bookmark the permalink.

1 Response to Fun with a Sense-Hat

  1. James Wilcox says:

    Came here because I searched for “Sense HAT” and “blinding” after plugging in my Pi 3 about a foot away from my face in a dark, dark room.

    I’m finally seeing normally now, about 12 hours later. Thanks for the ideas in the post! I’m new to programming and the world of Pi, so this is a great way to show off how it works to my nine year old.

    mike632t: I know what you mean! If you have a Pibow case then you can get a diffuser layer.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.