Home ยป A simple Raspberry Pi thermometer using the Sense HAT

A simple Raspberry Pi thermometer using the Sense HAT

by shedboy71

In this article we will display the temperature in Celsius and Fahrenheit on our sense hat

Getting started

First of all it does not do any harm to update your software, lets do that first

sudo apt-get update
sudo apt-get upgrade

You should have the sense hat already installed if you have completed one of our previous examples or have a new installation of Raspberry. Saying that there is no harm in installing it – if its already there the worst that happens is a message is displayed.

sudo apt-get install sense-hat

Parts Required

Name Link
Raspberry Pi 4 Aliexpress product link

Amazon. com link

Ebay search

Sense hat AliExpress Product link

Amazon link

ebay link

Python code

Using the latest version of Raspbian my current method is to start the Mu editor, copy in your code and save this with a easy to remember name like therm.py and then run the example. Much easier than using nano. Anyway here is the code for this example

# Write your code here :-)
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
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))

    # 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)

 

A lot of code there but as you can see the main chunk of code at the start defines the way each number will look, we then read the temperature suing the get_temperature function. the rest of the code just displays the temperature on the sense hat.

Summary

There you go another example for your sense hat

Links

We have uploaded the example code to our GitHub repo

https://github.com/getelectronics/PIBits/blob/master/python/therm.py

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More