← Home

Getting Started with the Raspberry Pi Sense HAT

What is the Sense HAT?

The Raspberry Pi Sense HAT is an add-on board originally designed for the Astro Pi mission — Raspberry Pi computers running on board the International Space Station. It includes:

  • An 8×8 RGB LED matrix
  • A 5-button mini joystick
  • Sensors for temperature, humidity, pressure
  • An inertial measurement unit (IMU): accelerometer, gyroscope, magnetometer

There are two versions of the board: the original Sense HAT and the newer Sense HAT v2, which uses different sensor chips.

What is Pi4J?

Pi4J is a friendly Java library for accessing GPIO, I²C, SPI, PWM and other low-level interfaces on the Raspberry Pi. It lets you talk to hardware like the Sense HAT from a normal Java application.

Hardware setup

  1. Plug the Sense HAT onto the 40-pin GPIO header of your Raspberry Pi.
  2. Boot Raspberry Pi OS (a recent 64-bit release is recommended).
  3. Enable I²C in raspi-configInterface Options.
  4. Verify the board is detected: i2cdetect -y 1.

Software prerequisites

  • Java 21+ (Java 25 is used by several examples here).
  • JBang — the easiest way to run the single-file Java examples in this repo.
  • Maven — if you want to build the full project or install pi4j-drivers locally.

Your first Sense HAT program with Pi4J

The simplest way to try Pi4J with the Sense HAT is via the SenseHat driver shipped with pi4j-drivers. A minimal JBang script looks like:

///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 21
//DEPS com.pi4j:pi4j-core:5.0.0-SNAPSHOT
//DEPS com.pi4j:pi4j-plugin-ffm:5.0.0-SNAPSHOT
//DEPS com.pi4j:pi4j-drivers-igfasouza:1.1.1-SNAPSHOT

import com.pi4j.Pi4J;
import com.pi4j.drivers.hat.raspberry.SenseHat;

public class Hello {
    public static void main(String[] args) {
        var pi4j = Pi4J.newAutoContext();
        var hat  = new SenseHat(pi4j);
        hat.getLedMatrix().setPixel(0, 0, 0xFF0000);
    }
}

Run it on the Pi with jbang Hello.java. Browse the projects for more complete examples.

Astro Pi

Astro Pi is a joint programme between the Raspberry Pi Foundation and the European Space Agency (ESA) that lets young people run their code on Raspberry Pi computers aboard the International Space Station. The Sense HAT is the primary hardware they interact with.

Community & further reading