Technical Article

Raspberry Pi Sensors: Sense HAT Temperature, Pressure, and Humidity

September 17, 2023 by Michael Levanduski

Explore the sensors used to obtain temperature, pressure, and humidity for industrial applications on this value-packed, compact add-on for the Raspberry Pi.

In previous articles, we have introduced the Raspberry Pi platform and, using the Sense HAT, we learned about setup and how to display a message on the 8 x 8 LED board. In this article, we’ll look at how to use the more common temperature, humidity, and pressure sensors. In doing so, it will also be a great opportunity to get hands-on experience with calling Python classes and methods in scripting. The interaction with a physical system will help better solidify the concepts and make them more tangible, a common challenge in the world of learning a programming language.

 

Sense HAT Emulator

If you do not have access to a Raspberry Pi or a Sense HAT, no need to worry! You can still follow along with a Sense HAT emulator created by a partnership with trinket.io and the Raspberry Pi Foundation.

The tool has a scripting window on the left-hand side, with a “main.py” file active. This file is what drives the simulated output on the Sense HAT model on the right-hand side of the window. The right-hand side of the window can also accept inputs to the Sense HAT. There are sliders that can be toggled to change the environment surrounding the virtual Sense HAT. Mirroring the scripting on the physical device will follow the same process as outlined in the ‘Showing a Message’ section of the Sense HAT article.

 

The sense HAT emulator space

Figure 1. The Sense HAT emulator window. Image used courtesy of Trinket

 

Temperature Sensor

The Sense HAT has two embedded temperature sensors, one each contained via the pressure and humidity sensors, respectively. There are three class methods that can be used to obtain the temperature:

  • get_temperature
  • get_temperature_from_humidity
  • get_temperature_from_pressure

The simplest method, get_temperature, simply points to the get_temperature_from_humidity method, so we can think of the humidity sensor as the default sensor for pressure. We certainly aren’t limited by options!

 

Three methods of obtaining temperature data

Figure 2. Visual of the three methods that can be used to measure temperature. Image provided by the author

 

Calling these methods is performed as shown in the below script. You can plug this into the main.py file on the left-hand side of the trinket.io emulator if you are following along. The temperature slider bar on the right will control the current environment and should match the reading of the sensors. It’s important that we create an instance variable called sense that builds a sense HAT object from the SenseHat() class within the sense_hat Python module. This module is maintained by the Raspberry Pi foundation and significantly reduces the programming required.

 

from sense_hat import SenseHat

sense = SenseHat()

temp = sense.get_temperature()
print("Temperature (humidity sensor reference) %s °C" %(temp))

temp2 = sense.get_temperature_from_humidity()
print("Temperature (humidity sensor) %s °C" %(temp2))

temp3 = sense.get_temperature_from_humidity()
print("Temperature (pressure sensor) %s °C" %(temp3))

 

The output should appear as below:

 

Temperature data from emulator

Figure 3. The output of the temperature sensor script in the Trinket emulator. Image used courtesy of Trinket

 

In the simulator, all three temperatures are the same. I’m willing to bet that on the real device, there is some variation. Sure enough, running the same Python code in a file named “temp.py” on the physical system proves that assumption.

 

Temperature data from real Raspberry Pi system

Figure 4. Real-world temperature data output from two different sensor sources. Image provided by the author

 

This output shows that, in reality, there is a slight variance between the humidity and pressure sensors’ readouts for temperature. Furthermore, the get_temperature() method yields the same result as the humidity sensor readout, proving it is simply a reference.

 

Pressure Sensor

Next in line is the pressure sensor. Gathering the environment pressure is less complex with only a single method, get_pressure(), available. It will output or return the current pressure in Millibars. Running the below code in the emulator will show what the pressure slider is set to. Feel free to change the pressure slider value and re-run the script to see if the reading changes.

 

from sense_hat import SenseHat

sense = SenseHat()

pressure = sense.get_pressure()
print("Pressure: %s mbar" %(pressure))

 

Running this in the emulator yields the following:

 

Pressure sensor data from emulator

Figure 5. The output of the pressure sensor script in the Trinket emulator. Image used courtesy of Trinket

 

Humidity Sensor

Last but certainly not least is the humidity sensor. As with pressure, there is only one method available that returns the percentage of relative humidity.

 

from sense_hat import SenseHat

sense = SenseHat()

humidity = sense.get_humidity()
print("Humidity: %s %%rH" % humidity)

 

This code above yields:

 

Humidity sensor data from eumlator

Figure 6. The output of the humidity sensor script in the Trinket emulator. Image used courtesy of Trinket

 

Conclusion

With very little code, the Sense HAT module empowers users to quickly obtain common parameter values monitored in industrial applications. These one-line methods can be used within more complex scripts that meet individualized business needs. Further articles will explore a few more beneficial use cases; this is simply a primer to become familiar with gathering data from the sensor module.