Sense Hat Pictures (Raspberry Pi)

After making rainbow displays on the sense hat my son realised that the Raspberry Pi sense hat’s LED display could be used to output art including a creeper from Minecraft.

Note: This is inspired by https://magpi.raspberrypi.org/articles/make-a-sense-hat-rainbow-display-for-your-window

A sense hat Minecraft Creeper
A sense hat Minecraft Creeper

The sense hat has an 8×8 display of individual LEDs which can be programmed to display individual colours. For this project we used Python but Scratch is available if Python is not your thing.

The Python code to output a Creeper is as follows:

from sense_hat import SenseHat

sense =  SenseHat()

r = (255,0,0)

o = (255,128,0)

y = (255,255,0)

g = (0,255,0)

c = (0,255,255)

b = (0,0,255)

p = (255,0,255)

n = (255,128,128)

w = (255,255,255)

k = (0,0,0)

creeper = [

    g,g,g,g,g,g,g,g,

    g,b,b,g,g,b,b,g,

    g,b,b,g,g,b,b,g,

    g,g,g,b,b,g,g,g,

    g,g,b,b,b,b,g,g,

    g,g,r,r,r,r,g,g,

    g,g,r,g,g,r,g,g,

    g,g,r,g,g,r,g,g,

    ]

sense.set_pixels(creeper)

Looking at the code a little deeper:

from sense_hat import SenseHat

sense =  SenseHat()

This two lines import the Python SenseHat module from the sense_hat library, and then tell Python that when we write sense we want to call on the SenseHat module.

r = (255,0,0)

o = (255,128,0)

y = (255,255,0)

g = (0,255,0)

c = (0,255,255)

b = (0,0,255)

p = (255,0,255)

n = (255,128,128)

w = (255,255,255)

k = (0,0,0)

These lines define variables (r, o, y, etc…) to represent any colours we may want to use. Instead of the letters we could also use the colour names (e.g. red, orange, yellow, etc…). The colours are then defined using their RGB (Red, Green, Blue) values. So r (red) is 255, 0, 0. b (blue) is 0, 0, 255 and g (green) is 0, 255, 0. The other colours are a mixture of the values – the range is 0 to 255 for red, green and blue.

0, 0, 0 is clear / off.

creeper = [

    g,g,g,g,g,g,g,g,

    g,b,b,g,g,b,b,g,

    g,b,b,g,g,b,b,g,

    g,g,g,b,b,g,g,g,

    g,g,b,b,b,b,g,g,

    g,g,r,r,r,r,g,g,

    g,g,r,g,g,r,g,g,

    g,g,r,g,g,r,g,g,

    ]

A list variable is then created, and it is called “creeper“. Again this variable name could be anything, but it is best to name it something related to the data it will contain.

As noted earlier, the sense hat is an 8×8 display and the list we use has 8 columns with 8 rows with each cell representing a LED on the sense hat.

geektechstuff_creeper_python_sense_hat_excel
Using a spreadsheet to simulate the sense hat LED display

To design what is being outputted I recommended to my son to sketch out what he wanted to display, then write down what colour should go into each cell. Another way to do this is with the fill option in a spreadsheet program.

Finally we use:

sense.set_pixels(creeper)

to ask Python to set the pixels on the Sense Hat to the same as the creeper list. Multiple lists could be created in the same Python program and then called individually to change what the Sense Hat is showing (e.g. with a sleep command in between each sense.set_pixels() command.

geektechstuff_creeper_python_sense_hat
Python to display a Minecraft Creeper on the Raspberry Pi Sense Hat