← Back to projects
Reading humidity on the Sense HAT

Humidity reads the relative humidity from the Sense HAT and scrolls the value across the LED matrix every 5 seconds.

Source code: projects/Humidity/Humidity.java

How it works

The Sense HAT includes an HTS221 humidity and temperature sensor over I²C. Pi4J's SenseHat driver already talks to it — you can just call hat.getHumidity() and get the value in %RH.

The main loop:

  1. Reads the current humidity with hat.getHumidity().
  2. Rounds it to one decimal place: Math.round(humidity * 10.0) / 10.0.
  3. Builds a message like H-42.7% and calls hat.showMessage(message, 70, textColor, backColor) to scroll it across the matrix in green on black.
  4. Sleeps 5 seconds and repeats.

Because the LED matrix generates a bit of heat, the reading is close to but not identical to the ambient value — see the ApproxTemp example for how to apply a calibration offset.

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