How to create a Raspberry Pi thermometer using the Sense HAT

The Raspberry Pi can do a lot on its own, but measuring the temperature is not one of those things. So when the Raspberry Pi Foundation teamed up with the European Space Agency to send a Raspberry Pi to the International Space Station, they developed the Sense HAT, an add-on device that gives the Raspberry Pi new ways to sense and measure the world around it. You don’t have to be headed into orbit to find a use for the Sense HAT, though – you can use it to make a Raspberry Pi digital clock, as we’ve done in the past, or you can use it to create a Raspberry Pi thermometer – as we’re about to do here.

How to create a Raspberry Pi thermometer using the Sense Hat

It’s not too hard to use the Sense HAT to determine the temperature, but we’re going to take things a step further here and use the Sense HAT to display the temperature, too. This project builds on what we learned making the Raspberry Pi digital clock – we’ll use the same technique to create digital numbers, only this time, we’ll be displaying the temperature instead of the time. Our thermometer will support both Fahrenheit and Celsius, including negative degrees.

Sure, it’s not a mission to the ISS, but it’s a pretty cool project all the same. To get started, grab a Raspberry Pi and a Sense HAT, install the Raspbian operating system, and meet us at Step 1.

Step 1: Install and update Raspbian

Let’s make sure we’re working with the most up-to-date system possible.

sudo apt-get update
sudo apt-get upgrade

Step 2: Make sure you have the Sense HAT library

You should have this already, but it never hurts to be cautious.

sudo apt-get install sense-hat

With any luck, you’ll get a message telling you that you already have the newest version. If not, you’ll download it. Foolproof stuff, right?

Step 3: Create a Python file

Use this command to create a new Python file.

nano thermometer.py

Now we’re going to fill our new file with a very long chunk of code. The first part will set up the light patterns we need to create our digital numbers. The part after that is the guts of our thermometer program. Get ready, because here we go:

Still with me? Let’s recap what we just did: this code creates the digits we use, reads the current temperature, checks whether the temperature is below zero Celsius and Fahrenheit, and chooses the color of the digits based on that. Finally, the code displays the temperature and updates it every second.

Step 4: Start the thermometer

The hard part is over. We can now start our thermometer by running the command:

python3 thermometer.py

And that’s that! You can exit the thermometer at any time by pressing Ctrl+C.