Home ยป Sense hat environmental sensor examples

Sense hat environmental sensor examples

by shedboy71

These are a few examples for the Sense Hat using the environmental sensors to get temperature, pressure and humidity

[codesyntax lang=”python”]

from sense_hat import SenseHat
sense = SenseHat()

while True:
    temp = sense.get_temperature()
    pres = sense.get_pressure()
    humi = sense.get_humidity()

    temp = round(temp, 1)
    pres = round(pres, 1)
    humi = round(humi, 1)

    msg = "Temperature = {0}, Pressure = {1}, Humidity = {2}".format(temp,pres,humi)

    sense.show_message(msg, scroll_speed=0.05)

[/codesyntax]

This example will display a differing background colour on the led matrix depending on whether the temperature is higher or lower than a certain value

[codesyntax lang=”python”]

from sense_hat import SenseHat
sense = SenseHat()

while True:
    temp = sense.get_temperature()
    temp = round(temp, 1)

    if temp < 35:
        bg = (0, 100, 0)  # green
    else:
        bg = (100, 0, 0)  # red

    msg = "Temperature = {0}".format(temp)
    sense.show_message(msg, scroll_speed=0.05, back_colour=bg)

[/codesyntax]

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