The device is itself a hybrid, containing a chip, a capacitive humidity sensor and a thermistor. It is actually cheaper to order these things attached to a small circuit board (breakout board) intended for hobbyists - especially as they include the recommended resistor and capacitor, and are thus ready for plug in and go.
While the device sports four pins, only three of them are in use.
Unlike most devices which produce an analog output, or those that are accessed using I2C or SPI data buses, these little boxes of tricks have a single-wire serial interface that is, frankly, difficult to use.
The Internet maker community to the rescue! I was originally going to use a PIC microcontroller to turn the DHT-11 into an I2C device, but I found that there is a library available for the Arduino (since that is what I am currently using).
A few false starts, and I have access to the world of Relative Humidity and Temperature measurement. From that, it is possible to calculate Dew Point (the temperature at which that atmospheric moisture would begin to condense) and Heat Index (the apparent temperature of the atmosphere). Neither of these numbers are easy to convert using a calculator although they only take a few lines of code in a program.
Before I deal with the software, there are a couple of caveats regarding the DHT series of sensors -
The capacitive humidity sensor absorbs moisture rapidly, but will give it up fairly slowly, so breathing into the device, getting it wet or taking it into a steamy kitchen is going to have an effect that lasts a couple of hours.
The DHT-11 is integer only (which I didn't realise), but the data capture library is compatible with the whole of the DHT range (DHT-11, -21, -22, -33 & -44), which means that they are plug compatible with both software and hardware. Upgrade to the higher precision devices is therefore quite simple.
The DHT-xx series are rather slow devices, and so should not be interrogated too frequently - this is taken care of quite naturally by the device library, as it delays other program activity until the data has been read (blocking device).
The latest edition of the library is available from Rob Tillaart on GitHub -
The source code for the wrapper which I am using, based on Rob's examples and other contributions, follows:
// dhtwrap.h
//
// ####################################################################
// ####################################################################
// ##
// ## dhtwrap.ino (dhtwrap.h)
// ## version 1.00.00
// ## date 12-Aug-2015
// ## author Alysson Rowan (AlyssonR)
// ## BASED on the work of Ron Tillaart
// ##
// ## alyssonrowan @ gmail.com
// ##
// ####################################################################
// ####################################################################
// ##
// ## This program is provided on an as-is basis without warranty.
// ## No claim is made as toward operability or fitness for purpose
// ## whatsoever. No liability can be accepted for any loss or damages
// ## howsoever caused in respect of this software.
// ##
// ## This software is made freely available under the terms of the
// ## GNU General Public License V2. In short, you are allowed to do
// ## anything with this program except sell it and hide the
// ## source code.
// ##
// ## In addition, this program is "postcard ware" – if you find it
// ## useful then please send me an e-mail and tell me about your
// ## application.
// ##
// ## Your comments and suggestions are much appreciated.
// ##
// ####################################################################
// ####################################################################
// ####################################################################
// ####################################################################
// ####################################################################
// ####################################################################
// ##
// ## DEPENDENCIES:
// ## -------------
// ##
// ## DHTlibrary by Rob Tillaart -
// ## Latest version is available at:
// ## https://github.com/RobTillaart/Arduino
// ##
// ## Library files at:
// ## https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTlib
// ##
// ## File tested with DHT library version 0.1.20
#include
#define DHTLIB_ERROR_DEVICE -9 // - additional error code
dht DHT;
double DHTtemperature; // Return values from DHTxx device.
double DHThumidity;
double DHTdewpoint;
double DHTheatindex;
int DHTerror;
void DHTGet(int DHTmodel, int DHTpin ) {
// Exit error codes:
// DHTerror = 0 : No error
// DHTerror = -1 : Checksum error
// DHTerror = -2 : Timeout
// DHTerror = -3 : Connection error
// DHTerror = -4 : ACK-L error
// DHTerror = -5 : ACK-H error
// DHTerror = -9 : Invalid device selection
// DHTerror = -999 : Unspecified Error
int chk;
switch (DHTmodel) { // Get data from the DHTxx module - using the appropriate read function.
case 11:
chk = DHT.read11(DHTpin);
break;
case 21:
chk = DHT.read21(DHTpin);
break;
case 22:
chk = DHT.read11(DHTpin);
break;
case 33:
chk = DHT.read33(DHTpin);
break;
case 44:
chk = DHT.read44(DHTpin);
break;
default:
chk = DHTLIB_ERROR_DEVICE;
break;
}
DHTtemperature = -999.0; // Set default (ERROR values, rather than end up with NaN
DHThumidity = -999.0;
DHTdewpoint = -999.0;
DHTheatindex = -999.0;
switch (chk) { // Error test and set the return data
case DHTLIB_OK:
DHTtemperature = DHT.temperature;
DHThumidity = DHT.humidity;
DHTdewpoint = dewPoint(DHT.temperature, DHT.humidity);
DHTheatindex = heatIndex(DHT.temperature, DHT.humidity);
DHTerror = DHTLIB_OK;
break;
case DHTLIB_ERROR_CHECKSUM:
DHTerror = DHTLIB_ERROR_CHECKSUM;
break;
case DHTLIB_ERROR_TIMEOUT:
DHTerror = DHTLIB_ERROR_TIMEOUT;
break;
case DHTLIB_ERROR_DEVICE:
DHTerror = DHTLIB_ERROR_DEVICE;
break;
default:
DHTerror = chk;
break;
}
}
// dewPoint function NOAA
// reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
// reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
// Thanks to Rob Tillaart who came up with this.
double dewPoint(double celsius, double humidity)
{
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
// heatIndex function NOAA
// reference (1) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
// Thanks to Rob Tillaart who came up with this faster version.
double heatIndex(double celsius, double humidity)
{
double farenheit = (celsius * 1.8) + 32;
double farenheitsq = farenheit * farenheit;
double humiditysq = humidity * humidity;
double heatindex = celsius;
if ((farenheit >= 80) and (humidity >= 40)) {
//Heat index is only valid for temperatures 80F upwards AND for R.H. 40% upwards
double c1 = -42.379, c2 = 2.049015, c3 = 10.14333, c4 = -0.224755, c5 = -6.83783e-3 ;
double c6 = -5.481717e-2, c7 = 1.22874e-3, c8 = 8.5282e-4, c9 = -1.99e-6 ;
double A = (( c5 * farenheit) + c2) * farenheit + c1;
double B = (((c7 * farenheit) + c4) * farenheit + c3) * humidity;
double C = (((c9 * farenheit) + c8) * farenheit + c6) * humidity * humidity;
heatindex = A + B + C;
heatindex = (heatindex - 32) /1.8; // We are using Celsius throughout.
}
return heatindex;
}
DHTGet(deviceSuffix, DigitalPinNumber);
then the temperature, relative humidity, dew point and heat index will be placed in the variables
DHTtemperature, DHThumidity, DHTdewpoint and DHTheatindex respectively. The error status will be in DHTerror, and will be zero if there is no error.
All temperatures are expressed in degrees Celsius, the relative humidity is expressed as a percentage.
No comments:
Post a Comment
Comments and suggestions are warmly welcomed.