How To
How to Create a Raspberry Pi Thermometer Using Sense HAT?

The Raspberry Pi can do plenty of things on its own, but measuring the temperature is not one of those things. It can do this with the help of an Add-on device called Sense HAT, which gives the Raspberry Pi new ways to sense and measure the world around it. We have already made the Raspberry Pi Digital Clock in the previous project. Now you can use the Sense HAT to create the Raspberry Pi thermometer.

Related: How to Use the Raspberry Pi as a NAS box?
How to Create a Raspberry Pi Thermometer Using Sense HAT?
It is very simple to use the Sense HAT to determine the temperature, but now we are going to take things ahead here, and use the Sense HAT to display the temperature. Here we are using the same technique, that is used to create the digital numbers on the Sense HAT. The only difference is that we are displaying the temperature instead of time. The thermometer will support both the Fahrenheit and Celsius, including the negative degrees.
Step 1: To begin this project, the first thing you have to do is Install Raspbian OS on your Raspberry Pi.
Step 2: Install and update the Raspbian. Just ensure that you are working with the most up-to-date system possible.
sudo apt-get update
sudo apt-get upgrade

Step 3: Just ensure that you have the Sense HAT library. You should have this already, but it’s better to check once.
sudo apt-get install sense-hat
you will get the message telling you that you already have the latest version. If not, you can download it.
Step 4: Now you have to create the Python file. Use the following command to create a new Python file.
nano thermometer.py
Related: How to Setup Your Raspberry Pi as a VPN Router?

Now you are going to fill the new file with a long of code. The first part will set up the light patterns we need to create the digital numbers.
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)
The above code creates the digits we use, reads the current temperature, checks whether the temperature is below zero Celsius and Fahrenheit, and chooses the colour of the digits based on that. Finally, the code displays the temperature and updates it every second.
Step 5: Now you can start the thermometer. You can start the thermometer by running the following command:
python3 thermometer.py

And that’s all, You can exit the thermometer at any time by pressing Ctrl+C.
Thank you for reading this post.
-
How To2 weeks ago
How to Setup and Watch IPTV on VLC Media Player [2023]
-
How To4 years ago
Rufus for Linux | How to Create a Bootable USB for Linux Using Rufus
-
Chromecast2 years ago
How to Stream IPTV on Chromecast [2022]
-
How To2 years ago
F4M Tester Kodi Addon – Step by Step Installation Guide (2022)
-
How To2 years ago
Uranus Kodi Addon – Install using Blamo Griffin Repository
-
How To12 months ago
Rufus for Mac: Best 4 Alternatives to Create a Bootable USB Drive
-
How To1 year ago
Placenta Kodi Addon – Step by Step Installation Guide [2022]
-
How To3 years ago
Gurzil Kodi Addon – How to Install Gurzil Addon in Kodi?