Using the RPi Sense HAT
Looking for all the main sensors you can think of in just one board? We will look at the RPi Sense HAT.
Comes in the Sense HAT!
The Sense HAT is an add-on board for Raspberry Pi, made especially for the Astro Pi mission. It launched to the International Space Station in December 2015 And is now available to buy.
The Sense HAT has an 8×8 RGB LED matrix, a five-button joystick and includes the following sensors:
- Gyroscope
- Accelerometer
- Magnetometer
- Temperature
- Barometric pressure
- Humidity
They’ve also created a Python library providing easy access to everything on the board.
Requirements:
- A Raspberry Pi with an SD card with Raspberry Pi OS installed
- Sense Hat with the libraries installed on the Pi
A first look at the HAT will be to take a look at the onboard LEDs. You should also take a look at the project the Raspberry Pi Foundation has created.
Setup
In this post we will assume you have already set up your Raspberry Pi. If you need any help with this, head over to our post.
First you need to install the Sense hat library in order to use it
sudo apt-get install sense-hat
Before installing the board onto your Pi first shut your Pi down. Simply place the female header of the sense hat over the male pins of your Raspberry Pi and gently press down.
Code samples
Going through the project it seems using these LEDs are quite easy.
from sense_hat import SenseHat
sense = SenseHat()
First we will import the SenseHat library and initialize our HAT.
r = 255
g = 255
b = 255
The variables r, g, and b represent the colors red, green, and blue. Their values specify how bright each color should be; each value can be between 0 and 255. In the above code, the maximum value for each color has been used, so the result is white.
You can also define all three RGB values of a color using a single line of code:
red = (255,0,0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
Here we assign variables to hold the colors blue and yellow, and assign them colors on the color wheel.
while True:
sense.show_message('Hello world!', text_colour=yellow, back_colour=blue, scroll_speed=0.05)
In the above code we created a loop so that we can update the colors periodically. We are scrolling the text “Hello world!” across the LEDs and giving is a speed to scroll at. Changing this variable will change the speed at which the text moves across the display.
sense.clear((r, g, b))
Finally we turn all the LEDs off again using the clear function.
Next we move on to the sensors.
https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat/8
This page will show everything that would be needed to read all the basic sensors.
Resources
https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat/8
https://projects.raspberrypi.org/en/projects?software%5B%5D=python&hardware%5B%5D=sense-hat