Raspberry Pi – Sense HAT – DISPLAY TEMPERATURE TO LED MATRIX

DISPLAY TEMPERATURE TO LED MATRIX
An experiment from last Thursday night’s Digital Technologies rotations required the temperature to be displayed onto the Sense HAT LED matrix.
Here’s the code that was used.

# PYTHON TEST CODE FOR THE SENSE HAT
# DISPLAY TEMPERATURE TO LED MATRIX
# API Reference : https://pythonhosted.org/sense-hat/api/

from sense_hat import SenseHat
sense = SenseHat()

# Set up colours as variables
blue = (0,0,255)
yellow = (255,255,0)
red = (255,0,0)
green = (0,128,0)

while True:
                sense.clear() # clear arguments
                # Get the current temperature in degrees Celsius from the humidity sensor.
                temp = round(sense.get_temperature())
                # Prepare message to be displayed on LED Matrix
                message = 'Temperature is %d' %(temp)
                print(message)  # displays temperature to console for testing
                sense.set_rotation(180) # Rotate screen by 180 degrees 
                # Scrolls a text message from right to left across the LED matrix
                sense.show_message(message,scroll_speed=0.05, text_colour=blue, back_colour=yellow)


Expanded code to use more of the sensors

# PYTHON TEST CODE FOR THE SENSE HAT
# DISPLAY TEMPERATURE TO LED MATRIX
# API Reference : https://pythonhosted.org/sense-hat/api/
# Introduction : https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat/

from sense_hat import SenseHat
sense = SenseHat()
from time import sleep

# Set up colours as variables
blue = (0,0,255)
yellow = (255,255,0)
red = (255,0,0)
green = (0,128,0)
black = (0,0,0)

while True:
                sense.clear() # clear arguments
                sense.set_rotation(180) # Rotate screen by 180 degrees
                temp = round(sense.get_temperature())
                pressure = sense.get_pressure()  
                humidity = round(sense.get_humidity())         
                # Prepare message to be displayed on LED Matrix
                message = 'Temperature is %d Celsius %(temp)
                print(message)  # displays temperature to console for testing
                # Set font colour based on temperature range
                if temp > 10.0 and temp < 25.0:
                       font = green
                else:
                       font = red
                # Scrolls a text message from right to left across the LED matrix
                sense.show_message(message,scroll_speed=0.05, text_colour=font, back_colour=black)
                # Change message to pressure
                message = 'Pressure is %d' %(pressure)
                sense.show_message(message,scroll_speed=0.05, text_colour=green, back_colour=black)
                # Change message to humidity
                message = 'Humidity is %d' %(humidity)
                sense.show_message(message,scroll_speed=0.05, text_colour=blue, back_colour=black)

                sleep(5)

This entry was posted in 2019. Bookmark the permalink.

1 Response to Raspberry Pi – Sense HAT – DISPLAY TEMPERATURE TO LED MATRIX

  1. Pingback: Pi with sense hat + Arduino with a DS18B20 sensor | Visible Procrastinations

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.