Spirit level for SenseHAT

Spirit level project

I just got myself an early Christmas present – a SenseHAT for the RaspberryPi. This is the same gizmo Tim Peake will be using on the ISS to run AstroPi experiments designed by school children, so I have the added excitement of having some SPACE HARDWARE on my Pi.

It has a super-bright 8×8 RGB LED matrix, a joystick and various sensors: temperature (limited use as it’s on the board), plus more usefully, sensors for air pressure, humidity, a compass and a gyroscope. It’s really easy to program in Python with the libraries provided by the Raspberry Pi Foundation, and I had text scrolling across the screen within a couple of minutes of plugging it in for the first time.

The gyroscope caught my imagination when I set it up, and my first real project was a simple spirit level. It lights up red when it’s not level, green when horizontal, blue when vertical – and when you put it flat you get a white display with a ‘bubble’ in the middle. It would a great extension to code a ‘bubble’ that moves round the screen as you tilt it.

It’s also powered off a USB battery stick so you can use it in places where the mains don’t reach.

Spirit level project
Not level – red display

Spirit level project
Horizontally level

Spirit level project
Vertically level

Spirit level project
Flat on a level surface

Here’s how it works (though the video doesn’t do justice to the lovely display due to its brightness and strobing:

And here’s the Python code, most of which is taken up with the simple images. Tweaked thanks to David Honess, who pointed out problems with .value() returning things in unexpected orders!

from sense_hat import SenseHat

sense = SenseHat()

r = [255, 0, 0]
g = [0, 255, 0]
b = [0, 0, 255]
w = [255,255,255]
z = [0, 0, 0]

redimage = [
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
]

greenimage = [
w,w,w,w,w,w,w,w,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
]

blueimage = [
w,w,w,w,w,w,w,w,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
]

whiteimage = [
w,w,w,w,w,w,w,w,
w,w,w,w,w,w,w,w,
w,w,w,w,w,w,w,w,
w,w,w,z,z,w,w,w,
w,w,w,z,z,w,w,w,
w,w,w,w,w,w,w,w,
w,w,w,w,w,w,w,w,
w,w,w,w,w,w,w,w,
]

sense.set_pixels(redimage)

while True:
    raw = sense.accel_raw
    x = raw["x"]
    y = raw["y"]
    z = raw["z"]
    print (x,y,z)

    if (-0.02 < x < 0.02) and (-0.02 < y < 0.02) and (0.98 < z < 1.02):
        sense.set_pixels(whiteimage)
    elif (-0.02 < x < 0.02) and (-0.90 > y > -1.1):
        sense.set_pixels(greenimage)
    elif (-0.02 < y < 0.02) and (-0.90 > x > -1.1):
        sense.set_pixels(blueimage)
    else:
        sense.set_pixels(redimage)

This is the original version of the code, which is unreliable as it reads the variables in a random order:

from sense_hat import SenseHat

sense = SenseHat()

r = [255, 0, 0]
g = [0, 255, 0]
b = [0, 0, 255]
w = [255,255,255]
z = [0, 0, 0]

redimage = [
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
r,r,r,r,r,r,r,r,
]

greenimage = [
w,w,w,w,w,w,w,w,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
g,g,g,g,g,g,g,g,
]

blueimage = [
w,w,w,w,w,w,w,w,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
b,b,b,b,b,b,b,b,
]

whiteimage = [
w,w,w,w,w,w,w,w,
w,w,w,w,w,w,w,w,
w,w,w,w,w,w,w,w,
w,w,w,z,z,w,w,w,
w,w,w,z,z,w,w,w,
w,w,w,w,w,w,w,w,
w,w,w,w,w,w,w,w,
w,w,w,w,w,w,w,w,
]

sense.set_pixels(redimage)

while True:
    pitch, roll, yaw = sense.get_orientation().values()
#   print (pitch, roll, yaw)
    if (pitch < 3 or pitch > 355) and (yaw < 3 or yaw > 355):
        sense.set_pixels(whiteimage)
    elif pitch < 0.5 or pitch > 359.5:
        sense.set_pixels(greenimage)
    elif yaw < 5 or yaw > 355:
        sense.set_pixels(blueimage)
    else:
        sense.set_pixels(redimage)
This entry was posted in operating systems, Raspberry Pi and tagged , , . Bookmark the permalink.

Leave a Reply