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:
from sense_hat import SenseHat import time sense = SenseHat() number = [ 0,1,1,1, # Zero 0,1,0,1, 0,1,0,1, 0,1,1,1, 0,0,1,0, # One 0,1,1,0, 0,0,1,0, 0,1,1,1, 0,1,1,1, # Two 0,0,1,1, 0,1,1,0, 0,1,1,1, 0,1,1,1, # Three 0,0,1,1, 0,0,1,1, 0,1,1,1, 0,1,0,1, # Four 0,1,1,1, 0,0,0,1, 0,0,0,1, 0,1,1,1, # Five 0,1,1,0, 0,0,1,1, 0,1,1,1, 0,1,0,0, # Six 0,1,1,1, 0,1,0,1, 0,1,1,1, 0,1,1,1, # Seven 0,0,0,1, 0,0,1,0, 0,1,0,0, 0,1,1,1, # Eight 0,1,1,1, 0,1,1,1, 0,1,1,1, 0,1,1,1, # Nine 0,1,0,1, 0,1,1,1, 0,0,0,1 ] celcius_color = [255,0,0] # Red fahrenheit_color = [0,255,0] # Green negative_celcius_color = [0,255,255] # Cyan negative_fahrenheit_color = [0,0,255] # Blue empty = [0,0,0] # Black display = [ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 ] while True: celcius = int(round(sense.get_temperature())) fahrenheit = int(round(1.8 * celcius + 32)) if celcius < 0: celcius = abs(celcius) celcius_color = negative_celcius_color if fahrenheit < 0: fahrenheit = abs(fahrenheit) fahrenheit_color = negative_fahrenheit_color # Map digits to the display array pixel_offset = 0 index = 0 for index_loop in range(0, 4): for counter_loop in range(0, 4): display[index] = number[int(celcius/10)*16+pixel_offset] display[index+4] = number[int(celcius%10)*16+pixel_offset] display[index+32] = number[int(fahrenheit/10)*16+pixel_offset] display[index+36] = number[int(fahrenheit%10)*16+pixel_offset] pixel_offset = pixel_offset + 1 index = index + 1 index = index + 4 # Color the temperatures for index in range(0, 64): if display[index]: if index < 32: display[index] = celcius_color else: display[index] = fahrenheit_color else: display[index] = empty # Display the temperatures sense.low_light = True # Optional sense.set_pixels(display) time.sleep(1)
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.
Be the first to comment on "How to create a Raspberry Pi thermometer using the Sense HAT"