← Back to projects
Full Sense HAT demo with Pi4J

FullDemo is a "tour of the Sense HAT" — a single script that walks through every major feature the Pi4J SenseHat driver exposes, one section at a time, printing what it does to the console so you can follow along.

Source code: projects/FullDemo/FullDemo.java

Use it as a diagnostic tool when setting up a new board, or as a reference to see how each Pi4J API is called.

What it demonstrates

The script runs through the following sections, in order:

  1. Version detection — calls hat.getVersion() to tell apart the original Sense HAT and the Sense HAT v2.
  2. LED matrix: clear + fill — uses hat.clear(), hat.fill(r, g, b) and hat.fill(Argb32.fromRgb(...)).
  3. LED matrix: setPixel — draws two diagonal lines using hat.setPixel(x, y, r, g, b) and hat.setPixel(x, y, argb).
  4. LED matrix: setPixels(int[]) — writes a gradient to the whole 64-pixel array in one call.
  5. LED matrix: setPixels(int[][]) — same idea, but with [R, G, B] triplets per pixel.
  6. LED matrix: getPixels snapshot — reads the current state of the display back with hat.getPixels().
  7. LED matrix: showLetter — shows a single character with optional foreground/background colors.
  8. LED matrix: showMessage variants — scrolls text with different speeds, colors and scroll directions (ScrollDirection.LEFT_TO_RIGHT).
  9. LED matrix: rotation + flips — cycles through Rotation.ROTATE_0..270, then flipHorizontal() and flipVertical().
  10. Humidity sensor (HTS221) — reads humidity and derived temperature.
  11. Pressure sensor (LPS25H) — reads pressure and derived temperature.
  12. Colour sensor (TCS3400) — only available on the Sense HAT v2; reads clear/R/G/B channels.
  13. IMU: accelerometer + gyroscope (LSM9DS1) — enables the IMU, then reads raw acceleration, gyroscope, and computed orientation in degrees and radians.
  14. IMU: magnetometer / compass — reads the compass heading and the raw magnetic field.
  15. All sensors — enumerates every Sensor known to the driver via hat.getAllSensors().
  16. Joystick: polling with getEvents() — for 5 seconds, prints every joystick event returned by hat.getEvents().
  17. Joystick: getController() state read — reads whether the center key is currently pressed via the underlying GameController.
  18. Joystick: listener — registers a Consumer<Event> with hat.addJoystickListener(...) for 5 seconds, then removes it.
  19. Joystick: waitForEvent() — blocks on the joystick for up to 10 seconds using a watchdog thread that interrupts the wait if nothing happens.
  20. Demo complete — scrolls a farewell message in magenta.

Why it's useful

  • It's the fastest way to check that a fresh Sense HAT is wired up correctly.
  • Every section is small enough to copy-paste as the starting point for your own project.
  • Running it against both a v1 and a v2 board makes the differences (like the missing colour sensor on v1) immediately obvious.

Running it

This script depends on a specific branch of pi4j-drivers (my igfasouza branch), so clone and install it locally first:

git clone -b igfasouza https://github.com/igfasouza/pi4j-drivers.git
cd pi4j-drivers
mvn install

Then run it with JBang on a Raspberry Pi with a Sense HAT attached:

jbang projects/FullDemo/FullDemo.java

Be ready to move the joystick a few times when the script asks — some sections will just sit and wait for input.