=DHT22 Temperature and Humidity Sensor= The DHT-22 is a low cost humidity and temperature sensor with a single wire digital interface. The sensor is calibrated and doesn\\\'t require extra components so you can get right to measuring relative humidity and temperature. [[http://dlnmh9ip6v2uc.cloudfront.net/images/products/10167-01_i_ma.jpg]] Features: * 3.3-6V Input * 1-1.5mA measuring current * 40-50 uA standby current * Humidity from 0-100% RH * -40 - 80 degrees C temperature range * +-2% RH accuracy * +-0.5 degrees C ***(same as RHT03 www.humiditycn.com)*** Available as http://www.sparkfun.com/products/10167 | DHT22(pin) | Function | mbed | | 1 - VDD | VDD-power supply (3.3-6V) | (Vout) 5V | | 2 - DATA | Single pin signal | p15 | | 3 - NULL | | | | 4 - GND | Connect to ground | GND | == Library == <<library /users/hwkit/libraries/DHT22/latest/docs/DHT22_8h_source.html>> &lt;<library users=\"\" hwkit=\"\" libraries=\"\" dht22=\"\" latest=\"\" docs=\"\" dht22_8cpp_source.html=\"\">&gt;</library>

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DHT22.cpp Source File

DHT22.cpp

00001 /*
00002     DHT22.cpp - DHT22 sensor library
00003     Developed by HO WING KIT
00004 
00005     This library is free software; you can redistribute it and / or
00006     modify it under the terms of the GNU Leser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRENTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PATRICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018 
00019 
00020     Humidity and Temperature Sensor DHT22 info found at
00021     http://www.sparkfun.com/products/10167
00022     same as RHT03 http://www.humiditycn.com    
00023 
00024     Version 0.1: 8-Jan-2011 by Ho Wing Kit
00025                  Beata test
00026     
00027 */
00028 
00029 #include "DHT22.h"
00030 
00031 
00032 // This should be 40, but the sensor is adding an extra bit at the start
00033 #define DHT22_DATA_BIT_COUNT 41
00034                            // debug
00035 Serial pc(USBTX, USBRX);   // Tx, Rx Using USB Virtual Serial Port
00036                            // Read Data From /etc/ttyACM* (linux port)
00037 
00038 DHT22::DHT22(PinName Data) {
00039 
00040     _data = Data;                // Set Data Pin
00041     _lastReadTime = time(NULL);
00042     _lastHumidity = 0;
00043     _lastTemperature = DHT22_ERROR_VALUE;
00044 }
00045 
00046 DHT22::~DHT22() {
00047 }
00048 
00049 DHT22_ERROR DHT22::readData() {
00050     int i, retryCount;
00051     int currentTemperature=0;
00052     int currentHumidity=0;
00053     unsigned int checkSum = 0, csPart1, csPart2, csPart3, csPart4;
00054     unsigned int bitTimes[DHT22_DATA_BIT_COUNT];
00055     
00056     time_t currentTime = time(NULL);
00057 
00058     DigitalInOut  DATA(_data);
00059    
00060        
00061     for (i = 0; i < DHT22_DATA_BIT_COUNT; i++) {
00062         bitTimes[i] = 0;
00063     }
00064     
00065     if (int(currentTime - _lastReadTime) < 2) {        
00066         pc.printf("DHT22 Error Too Quick, wiat...");
00067         return DHT_ERROR_TOOQUICK;
00068     }   
00069     retryCount = 0;
00070     // Pin needs to start HIGH, wait unit it is HIGH with a timeout
00071     do {
00072         if (retryCount > 125) {
00073             pc.printf("DHT22 Bus busy! ");
00074             return DHT_BUS_HUNG;
00075         }
00076         retryCount ++;
00077         wait_us(2);
00078     } while (DATA==0);   // exit on DHT22 retrun 'High' Signal within 250us
00079     
00080     // Send the activate pulse
00081     // Step 1: MCU send out start signal to DHT22 and DHT22 send
00082     //         response signal to MCU.
00083     // If always signal high-voltage-level, it means DHT22 is not 
00084     // working properly, plesee check the electrical connection status.
00085     //
00086     DATA = 0;        // MCU send out start signal to DHT22
00087     wait_us(1100);   // 1.1 ms
00088     DATA = 1;        // MCU pull up 
00089     wait_us(30);     // 30 us
00090     // Find the start of the ACK Pulse
00091     retryCount = 0;
00092     do {
00093         if (retryCount > 40)  {// (Spec is 80 us, 40*2 == 80us
00094             pc.printf("DHT22 is not present! ");
00095             return DHT_ERROR_NOT_PRESENT;
00096         }
00097         retryCount ++;
00098         wait_us(2);
00099     } while (DATA==1);   // Exit on DHT22 pull low within 80us
00100     
00101     // Find the last of the ACK Pulse
00102     retryCount = 0;
00103     do {
00104         if (retryCount > 40) {// (Spec is 80 us, 40 * 2 == 100us)
00105             pc.printf("DHT22 error timeout for receiveing last ack signal! ");
00106             return DHT_ERROR_ACK_TOO_LONG;
00107         }
00108         retryCount++;
00109         wait_us(2);
00110     } while (DATA==0); // Exit on DHT22 pull high within 80us
00111 
00112     // Reading the 40 bit data stream
00113     // Step 2: DHT22 send data to MCU
00114     //         Start bit -> low volage within 50us
00115     //         0         -> high volage within 26-28 us
00116     //         1         -> high volage within 70us
00117     // 
00118          
00119     for (i=0; i < DHT22_DATA_BIT_COUNT; i++) {
00120         retryCount = 0;
00121         do {                       // Getting start bit signal 
00122             if (retryCount > 25) { // spec is 50 u, 25*2 = 50 us
00123                 pc.printf("DHT22 sync timeout error! ");
00124                 return DHT_ERROR_SYNC_TIMEOUT;
00125             }
00126             retryCount ++;
00127             wait_us(2);
00128         } while (DATA==0);   // Exit on high volage within 50us
00129         // Measure the width of the data pulse
00130         retryCount = 0;
00131         do {
00132             if (retryCount > 40) { // spec is 80us, 50*2 == 100us
00133                 pc.printf("DHT22 ERROR DATA TIMEOUT\n");
00134                 return DHT_ERROR_DATA_TIMEOUT;
00135             }
00136             retryCount++;
00137             wait_us(2);
00138         } while (DATA==1);        // Exit on low volage below 80us
00139         bitTimes[i] = retryCount; // Assign bitTimes in us
00140     }
00141 
00142     // Now bitTimes have the number of retries (us *2)
00143     // that were needed to find the end of each data bit
00144     // Spec: 0 is 26 to 28 us
00145     // Spec: 1 is 70 us
00146     // bitTimes[x] <= 14 is a 0 (14x2us = 28us)
00147     // bitTimes[x] > 15  is a 1 (15x2us = 30us)
00148     // Note: the bits are offset by one from the data sheet, not sure why
00149     currentHumidity   = 0;
00150     currentTemperature = 0;
00151     checkSum           = 0;
00152     // First 16 bit is Humidity
00153     for (i=0; i<16; i++) {
00154         if (bitTimes[i] > 14) {
00155             pc.printf("%d: bit time is %d us. ", i, bitTimes[i]);
00156             currentHumidity |= ( 1 << (15-i));
00157         }
00158     }
00159     
00160     // Second 16 bit is Temperature 
00161     for (i=0; i<16; i ++) {
00162         if (bitTimes[i+16] > 14) {
00163             currentTemperature |= (1 <<(15-i));
00164         }
00165     }
00166 
00167     // Last 8 bit is Checksum
00168     for (i=0; i<8; i++) {
00169         if (bitTimes[i+32] > 14) {
00170             checkSum |= (1 << (7-i));
00171         }
00172     }
00173    
00174     _lastHumidity = (float(currentHumidity) / 10.0);
00175     
00176     // if first bit of currentTemperature is 1, it is negative value.
00177     
00178     if ((currentTemperature &= 0x8000)==0x8000) {        
00179         _lastTemperature = (float(currentTemperature & 0x7FFF) / 10.0) * -1.0;
00180     } else {
00181         _lastTemperature = float(currentTemperature) / 10.0;
00182     }
00183 
00184     // Calculate Check Sum
00185     //           
00186     csPart1 = currentHumidity >> 8;
00187     csPart2 = currentHumidity & 0xFF;
00188     csPart3 = currentTemperature >> 8;
00189     csPart4 = currentTemperature & 0xFF;
00190     if (checkSum == ((csPart1 + csPart2 + csPart3 + csPart4) & 0xFF)) {
00191         pc.printf("Calculate check sum is %d.",(csPart1 + csPart2 + csPart3 + csPart4) & 0xFF);
00192         pc.printf("Reading check sum is %d.",checkSum);
00193         _lastReadTime = currentTime;
00194         pc.printf("OK-->Temperature:%d, Humidity:%d\n", _lastTemperature, _lastHumidity);
00195         return DHT_ERROR_NONE;
00196     }
00197     pc.printf("DHT22 Checksum error!");
00198     return DHT_ERROR_CHECKSUM;
00199 }
00200 
00201 float DHT22::getTemperatureC() {
00202     return _lastTemperature;
00203 }
00204 
00205 float DHT22::getHumidity() {
00206     return _lastHumidity;
00207 }