There are several ways in which your can monitor the ambient temperature using a Raspberry Pi single-board computer, perhaps as part of a weather station setup. While you could use an external sensor connected to the Raspberry Pi’s GPIO pins, here we will explain how to monitor temperature with a Raspberry Pi equipped with a Sense HAT.
What Is the Sense HAT?
An officialRaspberry Pi HAT(Hardware Attached on Top) add-on board designed and produced by the Raspberry Pi company, the Sense HAT was originally created to be used by astronauts aboard the International Space Station. Since 2015, two Raspberry Pi computers equipped with a Sense HAT have been used in scientific experiments designed by schoolchildren who entered the ongoingAstro Pichallenge These two units have since been replaced by upgraded versions based on a Raspberry Pi 4 and equipped with a High Quality Camera.
While it lacks the special silver case designed for use in space, the standard Sense HAT board has exactly the same functionality. Compatible with anyRaspberry Pi modelwith a 40-pin GPIO header, it features an array of on-board sensors that enable it to monitor the surrounding environment and also detect its own orientation and movement. In addition, it has an 8x8 RGB LED matrix to display text, data, and images. There’s also a mini five-way joystick.

The full array of Sense HAT sensory functions is as follows:
Now that you’ve got the lowdown on what this multipurpose Raspberry Pi HAT can do, it’s time to get started with the project.

Step 1: Mount the Sense HAT
To connect the Sense HAT, first make sure that your Raspberry Pi is shut down and disconnected from the power. Then carefully push the Sense HAT (with its supplied black header extender fitted) onto the Raspberry Pi’s 40-pin GPIO header so that the Sense HAT board is positioned over the Raspberry Pi board. Make sure that all the pins line up correctly and both rows are connected. You may also use screw-in stand-offs to help secure it.
you may use any standard Raspberry Pi model that has a 40-pin GPIO header. One of thekey limitations of a Raspberry Pi 400, however, is that its GPIO header is located on the rear of the integrated keyboard. This means the Sense HAT will face rearwards, so you may want to use a GPIO extension cable to connect it.

Step 2: Set Up the Raspberry Pi
As with any other project, you should plug in a USB keyboard and mouse and thenconnect your Raspberry Pi to a monitor or TV. You should also have a microSD card inserted with the standard Raspberry Pi OS on it—if you haven’t done this already, check outhow to install an operating system on a Raspberry Pi. You are then ready to turn on the power.
Alternatively, you could use your Raspberry Pi with Sense HAT in headless mode, without a monitor connected andconnect to the Raspberry Pi remotely using SSHfrom another computer or device. If doing this, you won’t be able to use the Thonny Python IDE, buy can still edit programs using the nano text editor and run them from the command line.

The Sense HAT firmware should be installed by default. To double-check, open a Terminal window and enter:
Then, if the package has just been newly installed, reboot the Raspberry Pi:

Step 3: Start Programming in Python
While you can use the Raspberry Pi Sense HAT with the Scratch block-based programming language, we’ll use Python to read and display its sensor readings.
The Thonny IDE (integrated development environment) is a good way to do Python programming on a Raspberry Pi, as it has a lot of functionality including helpful debugging features. In Raspberry Pi OS’s desktop GUI, go toMenu(top left raspberry icon)> Programming > Thonny IDEto launch it.
Step 4: Take a Temperature Reading
In the main window of the Thonny IDE, enter the following lines of code:
The first line imports theSenseHatclass from thesense_hatPython library (which is pre-installed in Raspberry Pi OS). This is then assigned to thesensevariable. The third line clears the Sense HAT’s LED matrix.
We then take the temperature reading and print it to the Shell area of the Thonny IDE. This is in degrees Celsius, so you may well want to first convert it to Fahrenheit:
The temperature sensor reading will have several digits after the decimal point. So we’ll use theroundfunction to round it to a single decimal place:
Thesense.get_temperature()function reads the temperature sensor built into the humidity sensor. Alternatively, you could take a temperature reading from the pressure sensor withsense.get_temperature_from_pressure()or even take both readings and calculate a mean average (by adding them and dividing by two).
Step 5: Show the Temperature on the Sense HAT
Printing a single temperature reading to the Python Shell is a little dull, so instead let’s take a new reading regularly and show it on the Sense HAT’s RGB LED matrix. To display a scrolling text message, we use theshow_messagefunction. We’ll also use awhile: Trueloop to keep taking a new reading every 10 seconds—for which we use thesleepfunction from thetimelibrary.
Here’s the complete program:
Run this code and you’ll see each new temperature reading scrolling across the LED matrix. Try blowing on the Sense HAT to see if the temperature changes.
Temperature readings may be affected by heat transferred from the Raspberry Pi’s CPU just below, so an adjustment may be needed to obtain a more accurate figure. Another solution is to use a stacking header to raise the Sense HAT higher above the Raspberry Pi.
Use a Raspberry Pi to Monitor Temperature
While you could use a standalone temperature sensor instead for this project, the Sense HAT makes it easy to monitor the temperature with your Raspberry Pi. you’re able to also use it to take a host of other sensor readings, such as barometric pressure and relative humidity, and show them on its LED matrix.