Close

Adding sensors: Temperature and Humidity

A project log for MakeTime

Arduino-compatible development platform whose primary function is a clock

mihaicuciucmihai.cuciuc 10/10/2021 at 12:090 Comments

On the back of the PCB there is an expansion header that can be populated to add functionality. Adding an SHT31 allows MakeTime to measure temperature and humidity.

The display is divided into 3 sections.

 - On the left, the temperature is shown on 10 LEDs. The first LED turns on above 16 degrees Celsius and the last one above 34 degrees Celsius.

 - On the right, humidity is shown with 10% granularity.

 - At the top a single LED turns Green/Yellow/Red to indicate comfort level, loosely based on the information in this article https://www.azosensors.com/Article.aspx?ArticleID=487


Arduino sketch

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"


#define LED_PIN    9
#define LED_COUNT 24

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_SHT31 sht31 = Adafruit_SHT31();

void setup()
{
  strip.begin();
  strip.show();

  if (! sht31.begin(0x44))   // Set to 0x45 for alternate i2c addr
  {
    strip.setPixelColor(12, 0x00800000);
    strip.show();
    while (1) delay(1);
  }
}

void loop()
{
  float t = sht31.readTemperature();
  float h = sht31.readHumidity();
  uint32_t color;
  uint8_t r, b, i;

  if (! isnan(t))
  {
    r = round((t - 15) / 2);
  } else { 
    r = 0;
  }
  if (! isnan(h))
  {
    b = round(h / 10);
  } else { 
    b = 0;
  }
  
  strip.clear();

  // Draw temperature
  for (i = 0; i < 10; i++)
  {
    color = 0x00040404;
    if (r > i) color += 0x00200000;
    strip.setPixelColor(i + 13, color);
  }

  // Draw humidity
  for (i = 0; i < 10; i++)
  {
    color = 0x00040404;
    if (b > i) color += 0x00000020;
    strip.setPixelColor(11 - i, color);
  }

  // Comfort level:
  // https://www.azosensors.com/Article.aspx?ArticleID=487
  // T in [20 - 26 deg] and RH in [30 - 70 %] - Green
  // T in [19 - 27 deg] and RH in [30 - 70 %] - Yellow
  // T in [20 - 26 deg] and RH in [20 - 80 %] - Yellow
  // else - Red
  color = 0;
  if (t >= 20 && t <= 26 && h >= 30 && h <= 70) color = 0x00002000;
  else if (t >= 19 && t <= 27 && h >= 30 && h <= 70) color = 0x00202000;
  else if (t >= 20 && t <= 26 && h >= 20 && h <= 80) color = 0x00202000;
  else color = 0x00200000;

  strip.setPixelColor(0, color);

  strip.show();

  delay(0.5);
}

Discussions