← Back to projects
Hello from Pi4J on the Sense HAT

Hello is the smallest useful Sense HAT script in this repo — a good starting point if you have never used Pi4J before. It just scrolls the text Hello from PI4J! endlessly on the 8×8 LED matrix.

Source code: projects/Hello/Hello.java

How it works

  1. A Pi4J context is created with Pi4J.newAutoContext(). The context handles the underlying I²C access to the Sense HAT.
  2. A SenseHat is opened in a try-with-resources block so it is properly closed on exit.
  3. The matrix is cleared, then hat.showMessage(message, 80, textColor, backColor) scrolls the string across the display with an 80 ms per-step scroll delay.
  4. The loop runs forever, so the message keeps scrolling until you kill the process.

Two colors are set up with Argb32.fromRgb(...): yellow for the text and black for the background.

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/Hello/Hello.java