As explained in my unboxing post, I wasn't originally planning to use the Sense HAT in my project. Though the more I read about it, the more I wanted to experiment with it anyway. And since all my other electronics gear is in boxes, waiting for next week's house move, now's the perfect time to see what this HAT has to offer!
Sense HAT
The Sense HAT, is as the name implies, loaded with sensors. In this paragraph I'll cover which sensors are available and how to fetch their data using the Python reference API. The HAT also has display capabilities using LEDs and input using a joystick.
The Python module for the Sense HAT seemed to be installed by default in my case. If it isn't, you can install it using following command:
pi@raspberrypi:~ $ sudo apt-get install sense-hat
Also, make sure I2C is enabled (can be done using "raspi-config").
Sensor Data
The Sense HAT has a lot of sensors on board. Let's see how to retrieve this data
Temperature, Humidity & Pressure
By default, the temperature is retrieved from the humidity sensor. It is however possible to retrieve it from the pressure sensor instead.
The example below illustrates how to retrieve the data from the humidity and pressor sensor:
#!/usr/bin/env python # -*- coding: utf-8 -*- from sense_hat import SenseHat from time import sleep sh = SenseHat() try: while True: th = sh.get_temperature() tp = sh.get_temperature_from_pressure() p = sh.get_pressure() h = sh.get_humidity() th = round( th, 1 ) tp = round( tp, 1 ) p = round( p, 1 ) h = round( h, 1 ) print( "Temp (H) = %s°C Temp (P) = %s°C Prsr = %smb Hmdt = %s%%" %(th,tp,p,h) ) sleep( 1 ) except KeyboardInterrupt: print( "Exiting..." );
And the output should be similar to this:
Note that my HAT seems to be suffering from an issue, resulting in low humidity and even negative temperatures (it's really not that cold in Belgium ...). More about this in the last paragraph.
Accelerometer, Gyroscope & Magnetometer
Getting the orientation for all 3 axis can be done with a single call: "get_orientation". The data is returned in degrees. Note that no delays should be added in the function, as the sensor relies on multiple measurements to calculate the values. Adding delay will result in incorrect values.
#!/usr/bin/env python # -*- coding: utf-8 -*- from sense_hat import SenseHat from time import sleep sh = SenseHat() try: while True: pitch, roll, yaw = sh.get_orientation().values() pitch = round( pitch, 1 ) roll = round( roll, 1 ) yaw = round( yaw, 1 ) print( "Pitch = %s° Roll = %s° Yaw = %s°" %(pitch, roll, yaw) ) except KeyboardInterrupt: print( "Exiting..." );
The output:
The magnetometer can be used for compass applications:
#!/usr/bin/env python # -*- coding: utf-8 -*- from sense_hat import SenseHat from time import sleep sh = SenseHat() try: while True: north = sh.get_compass() north = round( north, 1 ) print( "North = %s°" %(north) ) except KeyboardInterrupt: print( "Exiting..." );
Unfortunately, those results seemed unreliable (or I was testing it incorrectly), as when turning the board around it's Z axis, the north would remain between 120° and 150° while I would expect it to go from 0° to 360°.
Turns out a calibration file is provided by default, which should cover most cases. Not working as expected, I deleted it, and ran the calibration steps described here.
The result was better, as I was able to cover the entire range of values:
North = 49.2° North = 49.2° ... North = 358.7° North = 358.8° ... North = 12.0° North = 12.1°
Finally, the acceleration data can be retrieved as follows:
#!/usr/bin/env python # -*- coding: utf-8 -*- from sense_hat import SenseHat from time import sleep sh = SenseHat() try: while True: x, y, z = sh.get_accelerometer_raw().values() x = round( x, 0 ) y = round( y, 0 ) z = round( z, 0 ) print( "X = %s Y = %s Z = %s" %(x, y, z) ) except KeyboardInterrupt: print( "Exiting..." );
In below example, I shook the Pi in the Y direction. You can see the values (in Gs) go from positive, to negative and back to positive, representing the shaking motion:
Joystick
To be able to scroll between menus and trigger actions, a 5 button joystick is foreseen.
The buttons of the joystick are mapped to keyboard keys: cursors and return key. Using code, it is possible to catch those events and trigger actions accordingly.
#!/usr/bin/env python import pygame pygame.init() pygame.display.set_mode((640, 480)) try: while True: for event in pygame.event.get(): print(event.key) except KeyboardInterrupt: print( "Exiting..." );
Pressing the joystick's buttons results in the following output.
There are 5 different numbers, repeated twice. The repetition represent the "press" and "release" actions. The actual numbers are key codes. A listing can be found here, the sequence above is then decoded as "left", "up", "right", "down", "enter".
LED Matrix
The Sense HAT has an onboard 8x8 RGB LED matrix which can be used to display images or text.
Text
In the code below, I created an example program demonstrating various attributes of the "show_message" function. It is possible to define the colour of the text and background, the scroll speed and the rotation.
#!/usr/bin/env python from sense_hat import SenseHat from time import sleep sh = SenseHat() try: sh.set_rotation(0) sh.show_message("test") sleep( 1 ) sh.set_rotation(90) sh.show_message("test", text_colour=[255, 0, 0]) sleep( 1 ) sh.set_rotation(180) sh.show_message("test", text_colour=[255, 255, 0], back_colour=[0, 0, 255]) sleep( 1 ) sh.set_rotation(270) sh.show_message("test", scroll_speed=0.5) sleep( 1 ) except KeyboardInterrupt: print( "Exiting..." );
The code above results in following animation:
Shape
Drawing shapes on the LED matrix, is done by defining an array of color values, representing each LED. In below example, the "wink" array defines a winking smiley face. The background is defined by the array "O" as black (no color), the drawing by X as blue.
#!/usr/bin/env python from sense_hat import SenseHat from time import sleep sh = SenseHat() X = [0, 0, 255] O = [0, 0, 0] wink = [ O, X, O, O, O, O, O, O, X, O, X, O, O, X, X, X, O, X, O, O, O, O, O, O, O, O, O, O, X, O, O, O, O, O, O, X, X, O, O, O, O, O, O, O, O, O, O, O, O, X, O, O, O, O, X, O, O, O, X, X, X, X, O, O ] try: sh.set_pixels(wink) except KeyboardInterrupt: print( "Exiting..." );
It's possible to define multiple arrays of shapes and colour and combine them, as demonstrated below:
Issue ?
There seems to be an issue with some of the Sense HATs used in this challenge.
The problem first surfaced when vish created a discussion about his HAT reporting negative temperature and humidity. Using the same code, mine reported correct values. The next day though, I was suddenly suffering from the same negative temperatures. Doing some searching online, I came across this thread on the official Raspberry Pi forums, in which it is described as a hardware issue, which has been resolved in the factory in the mean time. Some faulty units could still be in stock at some distributors though.
Could this be the same issue?
In case the humidity is not important for your application, there is no blocking issue, as the temperature can be retrieved from the pressure sensor instead.
Navigate to the next or previous post using the arrows. |
Top Comments