Humidity vs Temperature and Comfort: DHT11 code

They say "It's not the heat, it's the humidity". Well, my late Mum said it anyway.

The chart here shows a colour scheme for the comfort as perceived by "the human body" (it doesn't say whose 8) ) for H vs T.

This sketch maps those colours into a 2D array, and extracts the colour code for the current H and T. It displays the results in the serial monitor, and sets an RGB LED to R, G, B or Y according to the chart. (Haven't hooked an actual LED up yet... )

// Load h vs t comfort chart into 2d array
// example: http://www.klingenburg-usa.com/news/hugo.html
// Read the humidity and temp from a dht11  
// Look up the comfort zone in the array
// Display results in serial monitor
// Set an RGB LED as Red, Blue, Green or Yellow ala chart


#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 5 

byte humid;
byte temp;
byte humidInd;
byte tempInd;
char comfColour;

// pins of the RGB LED..
byte LED_R = 2;
byte LED_G = 3;
byte LED_B = 6;

//fill the array based on the colour chart
int comf[10][11] = {
  'B','B','B','R','R','R','R','R','R','R','\0',  //high humid
  'B','B','Y','Y','R','R','R','R','R','R','\0',
  'B','B','Y','Y','Y','R','R','R','R','R','\0',
  'B','B','Y','G','G','Y','Y','R','R','R','\0',
  'B','B','Y','G','G','G','Y','R','R','R','\0',
  'B','B','Y','G','G','G','Y','Y','R','R','\0',
  'B','B','B','Y','Y','G','Y','Y','R','R','\0',
  'B','B','B','B','Y','Y','Y','Y','R','R','\0',
  'B','B','B','B','B','B','B','R','R','R','\0',
  'B','B','B','B','B','B','B','R','R','R','\0',  //low humid
}; // 


void setup(){

  pinMode(LED_R, OUTPUT);
  pinMode(LED_G, OUTPUT);
  pinMode(LED_B, OUTPUT);

  Serial.begin(9600);
  Serial.println("DHT COMFORT PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
  Serial.println("Humidity (%),\tTemperature (C),\tComfort");
}

void loop(){

  //get data
  int chk;
  chk = DHT.read(DHT11_PIN);    // READ DATA
  humid = DHT.humidity;
  temp = DHT.temperature;

  //calc indices into the 2d matrix
  humidInd =9- humid/10;

  if (temp%2==0)  //even temp
  {
    tempInd=(temp/2)-8;
  }
  else  //odd temp
  {
    tempInd=(temp/2)-7;
  }

  if (tempInd < 0) tempInd=0;
  if (tempInd > 9) tempInd=9;

  // get the current value from the array

  comfColour = comf[humidInd][tempInd];

  // DISPLAY DATA
  Serial.print(humid);
  Serial.print(",\t\t\t");
  Serial.print(temp);
  Serial.print(",\t\t");
  Serial.println(comfColour);

  //and set the led
  if (comfColour == 'R')
  {
    digitalWrite(LED_R, HIGH);
    digitalWrite(LED_G, LOW);
    digitalWrite(LED_B, LOW);
  }

  if (comfColour == 'G')
  {
    digitalWrite(LED_R, LOW);
    digitalWrite(LED_G, HIGH);
    digitalWrite(LED_B, LOW);
  }

  if (comfColour == 'B')
  {
    digitalWrite(LED_R, LOW);
    digitalWrite(LED_G, LOW);
    digitalWrite(LED_B, HIGH);
  }

  if (comfColour == 'Y')
  {
    digitalWrite(LED_R, HIGH);
    digitalWrite(LED_G, HIGH);
    digitalWrite(LED_B, LOW);
  }
  delay(10000);
}//loop

That's a great application!

Added a link to this thread on - Arduino Playground - DHTLib -

Note the array could be of the type byte, as that would half its footprint.
And you could put a compressed RGB value in every byte: three bits R, three bits Green, 2 bits blue. [RRRGGGBB] effectively giving 256 colors. yes that would be "a bit" artificial, but it would allow you to interpolate more smoothly between the values.

  • I do not understand the \0, as they are not needed

  • humidInd = 9 - (humid+5)/10; // rounding iso truncating.
    similar for temperature.

Thanks for the tips there Rob, I'll implement the rounding. Thanks for the link in the Playground too: fame at last whoop whoop.

I'm going to put this into my Arduino webserver too. That's tonight's work.... Probably print the phrase "This is your comfort level" in the appropriate colour.

I'm reluctant to implement too many colours. Who's to say we all feel the same comfort at the same H&T. I will probably go for splitting the yellow into yellow above the green on the red side, and yellow below by the blue: maybe a bright yellow vs a dark orange. Or something.

Actually I might change that chart for the humidex which puts a number to each H/T pair and then into a band.

I recall doing some humidex code ..... search .......

check - Humidex (Heat Index) Calculator - Exhibition / Gallery - Arduino Forum

Thanks Rob, I might use that. This is one of those cases where one should weigh up the pros and cons (time?, memory?) of lookup vs calc-on-the-fly, I guess.

Hi all,
how can'I make sure that the sensor of pression gives the good value?

Thanks in advance.
Regards.

Is pression, pressure?

No idea, and the DHT is a humidity and temperature sensor.

If you have trouble with a pressure sensor you are using, you should start a new thread with details of what type of sensor it is (link to datasheet always helps), your code, and a schematic of your circuit. Also explain clearly what's wrong.

JimboZA:
Thanks Rob, I might use that. This is one of those cases where one should weigh up the pros and cons (time?, memory?) of lookup vs calc-on-the-fly, I guess.

As the DHT sensor needs ~2 seconds between measurements one has time enough to calculate.
The good thing of formulas is that on can calculate it with quite some precision while for a lookup table one needs to interpolate.

What one at least could do is to check if the formula and the graph on the link you posted match ...
if not there is at least one of them wrong ...

good application :
i am has been used project about the temperature by dht sensor and i used opt and mosfet for control the res for temprrature and humidite but what is meaning
humidInd =9- humid/10;

if (temp%2==0) //even temp
{
tempInd=(temp/2)-8;
}
else //odd temp
{
tempInd=(temp/2)-7;
}

if (tempInd < 0) tempInd=0;
if (tempInd > 9) tempInd=9;

9,2,8 and 7 ..