← Back to projects
Reading barometric pressure on the Sense HAT

Pressure reads the atmospheric (barometric) pressure from the Sense HAT and scrolls it across the LED matrix every 5 seconds.

Source code: projects/Pressure/Pressure.java

How it works

The Sense HAT ships with an LPS25H barometric pressure sensor (LPS22HB on the v2 board). Pi4J's SenseHat driver exposes it via hat.getPressure(), which returns the value in hectopascals (hPa).

The main loop:

  1. Calls hat.getPressure() to get the current reading.
  2. Rounds it to one decimal place.
  3. Builds a message like 1013.2 hPa and scrolls it with hat.showMessage(...) in yellow on black.
  4. Sleeps 5 seconds and repeats forever.

Typical values at sea level are around 1013 hPa; the reading drops as you go up in altitude, which makes this example a great starting point for a weather station or an altimeter demo.

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