Reading humidity on the Sense HAT
Read the relative humidity from the Sense HAT HTS221 sensor and display it as a scrolling message on the LED matrix.
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:
- Reads the current humidity with
hat.getHumidity(). - Rounds it to one decimal place:
Math.round(humidity * 10.0) / 10.0. - Builds a message like
H-42.7%and callshat.showMessage(message, 70, textColor, backColor)to scroll it across the matrix in green on black. - 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