AI agents & screen readers: for a machine-readable, text-only catalogue, start at /llms.txt. Products are available as Markdown (/products.md, /products/{handle}.md) and JSON (/products.json, /products/{handle}.json).
Store

Raspberry Pi Sense HAT

What the Sense HAT measures, how to set it up, and short Python examples for the LED matrix, the joystick and every sensor, plus what to know when fitting one to a Raspberry Pi 5.

What it is

The Sense HAT is a board of sensors with a display and a joystick, made by Raspberry Pi. It was first built for Astro Pi, a programme run with the European Space Agency that puts Raspberry Pis on the International Space Station. Everything is on the one board, so there's no wiring or breadboard involved, which makes it a good first add-on for a classroom.

Part What it gives you
Gyroscope, accelerometer, magnetometer Orientation (pitch, roll, yaw), movement and a compass heading
Temperature Degrees Celsius, from two separate sensors
Humidity Relative humidity, as a percentage
Barometric pressure Air pressure in millibars (the same as hectopascals)
Colour and brightness Red, green, blue and overall light levels (Sense HAT V2)
8 × 8 LED matrix 64 full-colour pixels for text, pictures and readouts
Joystick Up, down, left, right and a centre press

The current board is the Sense HAT V2, which added the colour and brightness sensor. The original board has everything else in the table.

Which Pi it fits

It works with any Raspberry Pi that has the 40-pin GPIO header, including the Pi 5, 4, 3 and the Zero range (a Zero needs its header fitted, or buy the WH version). It's a proper HAT with an ID EEPROM, so the Pi recognises it at boot and loads its drivers on its own. See What makes a HAT for how that works.

On a Pi 5 with the Active Cooler, check the clearance before you screw it down: the fan needs room under the HAT, and current guidance is 15–16 mm between the boards. An extra-long stacking header and taller standoffs will lift it clear.

Installing the software

Update the Pi first, then install the sense-hat package and reboot:

sudo apt update
sudo apt full-upgrade
sudo apt install sense-hat
sudo reboot

The package brings in the Python library and its dependencies, and switches on I2C, which the board uses to talk to the Pi. The library doesn't rely on the older GPIO libraries that stopped working on the Pi 5, so the same code runs on every model.

Hello, world

Save this as hello.py and run it with python3 hello.py:

from sense_hat import SenseHat

sense = SenseHat()
sense.show_message("G'day!", text_colour=(255, 160, 0))

The text scrolls across the matrix from right to left. Add scroll_speed=0.05 to speed it up (the default is 0.1, the pause in seconds between each step).

Drawing on the LED matrix

Each pixel is addressed by x and y, from 0 to 7, with a colour given as red, green and blue values from 0 to 255:

from sense_hat import SenseHat

sense = SenseHat()
sense.clear()                          # all pixels off
sense.set_pixel(0, 0, 255, 0, 0)       # red in one corner
sense.set_pixel(7, 7, (0, 0, 255))     # blue in the opposite corner
sense.low_light = True                 # dim the whole display

Pixel (0, 0) is the top-left corner when the Pi's HDMI port faces down. If the board is mounted another way round, turn the display with sense.set_rotation(90) (or 180, or 270). sense.set_pixels() takes a list of all 64 colours at once, which is handy for pictures.

Reading the sensors

from sense_hat import SenseHat
from time import sleep

sense = SenseHat()

while True:
    temp = sense.get_temperature()       # °C
    humidity = sense.get_humidity()      # % relative humidity
    pressure = sense.get_pressure()      # millibars
    print(f"{temp:.1f} °C   {humidity:.0f}%   {pressure:.0f} mbar")
    sleep(2)

get_temperature() reads the sensor in the humidity chip; get_temperature_from_pressure() gives you the other one.

For movement and direction:

orientation = sense.get_orientation()   # degrees
print(orientation["pitch"], orientation["roll"], orientation["yaw"])

print(sense.get_compass())              # degrees from north
print(sense.get_accelerometer_raw())    # x, y, z in g

And on a V2 board, the colour sensor:

red, green, blue, clear = sense.colour.colour

If readings are too low in dim light, raise sense.colour.gain (1, 4, 16 or 60).

Using the joystick

from sense_hat import SenseHat

sense = SenseHat()

while True:
    event = sense.stick.wait_for_event()
    if event.action == "pressed":
        print("Pushed", event.direction)   # up, down, left, right or middle

event.action can also be "released" or "held". The joystick also behaves like the arrow keys and Enter on a keyboard, so games written with pygame can use it directly.

Getting good readings

  • The temperature reads warm. The sensors sit just above the Pi, so they pick up its heat, especially on a busy Pi 4 or 5. Treat the reading as the temperature at the board, not the room.
  • Keep magnets and speakers away from the board when you need an accurate heading.
  • Calibrate the compass if headings still look wrong. The software installs a general calibration file at /etc/RTIMULib.ini; to make one for your own board, run:
sudo apt install octave -y
cd
cp /usr/share/librtimulib-utils/RTEllipsoidFit ./ -a
cd RTEllipsoidFit
RTIMULibCal

Press m to start the magnetometer calibration, then any key, and turn the board slowly through every direction until the minimum and maximum numbers stop changing. Press s to save and x to exit. Then put the new file in place of the old one:

rm ~/.config/sense_hat/RTIMULib.ini
sudo cp RTIMULib.ini /etc

Parts in this guide

Maddy from the Pi Australia team

Need help? We're here for you!

Hi, I'm Maddy. My team and I are ready to help with your order or any questions.