The Adafruit CLUE has a temperature and humidity sensor that reads the temperature in degrees Celsius and the relative humidity in percent. To learn more about relative humidity, check Wikipedia.

With CircuitPython, it's easy to build a color-coded temperature and humidity monitor with customisable ranges and an optional audible alarm. Set the min and max temperature and humidity, optionally enable the audible alarm and you're all set to know when your environment is outside your comfort zone!

Remember that you must have the necessary libraries installed. Verify that your lib folder matches the list found on the CLUE CircuitPython Libraries page before continuing.

You'll want to set the min_temperature and max_temperature to your desired range in degrees Celsius, and the min_humidity and max_humidity to your desired range in percent. If you want the alarm to sound when the temperature or humidity is outside the specified range, enable it by setting alarm_enable = True. Then everything is ready to go!

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Monitor customisable temperature and humidity ranges, with an optional audible alarm tone."""
from adafruit_clue import clue

# Set desired temperature range in degrees Celsius.
min_temperature = 24
max_temperature = 30

# Set desired humidity range in percent.
min_humidity = 20
max_humidity = 65

# Set to true to enable audible alarm tone.
alarm_enable = False

clue_display = clue.simple_text_display(text_scale=3, colors=(clue.WHITE,))

clue_display[0].text = "Temperature &"
clue_display[1].text = "Humidity"

while True:
    alarm = False

    temperature = clue.temperature
    humidity = clue.humidity

    clue_display[3].text = "Temp: {:.1f} C".format(temperature)
    clue_display[5].text = "Humi: {:.1f} %".format(humidity)

    if temperature < min_temperature:
        clue_display[3].color = clue.BLUE
        alarm = True
    elif temperature > max_temperature:
        clue_display[3].color = clue.RED
        alarm = True
        clue_display[3].color = clue.WHITE

    if humidity < min_humidity:
        clue_display[5].color = clue.BLUE
        alarm = True
    elif humidity > max_humidity:
        clue_display[5].color = clue.RED
        alarm = True
        clue_display[5].color = clue.WHITE

    if alarm and alarm_enable:

Let's take a look at the code.

First you set the temperature and humidity ranges that you'd like to monitor, and optionally enable the audible alarm. Next you set up the text display to prepare to add the lines of text. We scale it to 3 and set the initial color of all the text white.

Note that colors expects a tuple. Tuples in Python come in parentheses () with comma separators. If you have two values, a tuple would look like (1.0, 3.14) Since we have only one value, we need to have it print out like (1.0,) note the parentheses around the number, and the comma after the number. Therefore if you are only providing a single color for all lines of text, you must include the extra comma after the single color to make it a single member tuple, e.g. colors=(clue.WHITE,).

Then we add the first two lines of text which are the title.

Inside the loop, we set the alarm variable to False. We'll use this variable throughout the program to determine whether the alarm should be going off. Next, we create a variable to hold the temperature data, and one to hold the humidity data. Then we create the next two lines of text to show the temperature and humidity data. Notice that we skip line 2 and line 4 - this is a feature to allow you to space out text on the display without creating empty lines.

Now we have two if blocks that look very similar, one for temperature and one for humidity. First we check to see if the temperature is less than the earlier-set minimum temperature, and if it is, we change the text to blue and set alarm to True. Then we check to see if the temperature is greater than the earlier-set maximum temperature, and if it is, change the text to red and set alarm to True. Otherwise, we keep the text white. Then we repeat the same steps for humidity.

Finally, we have one last if block. This block checks to see if BOTH alarm and alarm_enable are True. If so, play a 2000Hz tone which functions as the audible alarm. Otherwise, no tone is played.

That's what goes into making a temperature and humidity monitor using CircuitPython and CLUE!

Temp below the desired range, humidity above the desired range!
Note: The temperature will read higher than ambient temperature because of the backlight on the CLUE display.

This guide was first published on Feb 08, 2020. It was last updated on Feb 08, 2020.

This page (CLUE Temperature and Humidity Monitor) was last updated on May 13, 2021.

Text editor powered by tinymce.