Вы находитесь на странице: 1из 10

How

to Measure Temperature and Humidity with the DHT22


Jacob Zells 3/30/12

Measuring moisture is often necessary in military, commercial, and even residential applications. Having an accurate measurement of moisture related to the current temperature can be determined quite easily through the DHT22 sensor and the Arduino Developer Board. 1|P a ge

Index
Introduction.3 Specifications....3 Objective...3 Parts Required.....4 Getting Started....4 Understanding how the DHT22 functions....5 Programming the Arduino......6 Conclusion...6 References.....6 Appendix.....7

2|P a ge

Introduction
The DHT22 is a relatively cheap and accurate moisture and temperature sensor. With its small size and low power consumption, it can be used in many different applications ranging from outdoor residential measurement to internal chamber measurements. Combined with an Arduino Developer Board, the sensor is capable at taking a new measurement every two seconds.

Specifications
Humidity Power Supply Output Internal Sensor Measuring Range Accuracy Sampling Rate Temperature 3.3-6V Digital Polymer capacitor DS18B20 0 100% RH -40 125 C 2 5% 0.5 C 0.5 Hz

Objective
The objective of this tutorial is to provide the reader with a basic understanding of how to connect the DHT22 sensor to the Arduino Developer Board and program it to read both humidity and temperature. It is assumed the reader has a basic understanding of the C++ programming language along with the ability to read wiring schematics. This tutorial provides a guide the reader can use to connect and control similar sensors.

3|P a ge

Parts Required
Part DHT22 Arduino (complete with PSU and I/O cable) 10K Resistor (Brown Black Orange Gold) Protoboard Jumper wire (3 length) Quantity 1 1 1 1 4

Getting Started
Since the DHT22 comes equipped with four pins which can be easily placed into a protoboard, connecting the sensor is trivial. With the sensor placed in the protoboard as shown in Figure 1, connect pin1 (vdd-red) to the 5V port on the Arduino with a jumper wire while the Arduino is powered off. Connect pin2 (data-green) of the DHT22 to pin1 on the Arduino with another jumper wire. Also, connect the 10K resistor from pin2 of the DHT22 to pin1 of the DHT22. This provides a pull-up on the data line. Finally, connect pin4 (gnd-black) of the DHT22 to the Gnd port on the Arduino. You now should have 3 pins on the DHT22 connected. Pin3 remains unconnected as it is not used.

Figure 1

4|P a ge

Understanding how the DHT22 functions


As shown below in Graph 1, the Arduino sends an active low start signal for a minimum of 500us then pulls the voltage up and waits for a response from the DHT22 sensor. The sensor then drops the signal low for 80us, pulls it back up for another 80us then drops back low for 50us for data transmission. Once the 50us passes, the voltage is pulled up for either 26us or 70us meaning 0 or 1 respectively. The signal then drops low for another 50us declaring that the next bit is coming. The total amount of bits sent is 40 which reflects both the humidity and temperature measurements. DATA = 8 bits of integral RH data + 8 bits of decimal RH data + 8 bits of integral temperature data + 8 bits of decimal temperature data + 8 bit check-sum Data collection should take longer than 1.7 seconds, or an average of 2 seconds (Liu). Once the data is collected and stored in the Arduino, it can then be displayed on your computer screen and the process can begin again to provide another measurement.

Graph 1

5|P a ge

Programming the Adruino


Programming the Arduino to work with the DHT22 is fairly straight forward, but requires some understanding. Using the code found in the appendix or at the website found in the references section (2), the Arduino can be programmed to function with the DHT22 with almost no effort. However, if the reader is using a DHT11 instead of DHT22, two lines of the code must be fixed. Place two slaches (//) in front of the following line to allow the line to be skipped
#define DHTTYPE DHT22 // DHT 22 (AM2302)

Use this line by removing the two slashed in front of it.


//#define DHTTYPE DHT11 // DHT 11

Once these steps are taken (or skipped if not using the DHT11), the reader can build and compile the Arduino Developer Board then run the program. Use the tester file found in the references (2) to read the data recorded on the Arduino and see your sensor working in action. The output should be displayed in a new window with both Humidity and Temperature.

Conclusion
The reader should now have a working moisture/temperature sensor and an understanding of the fundamentals of how it works with the Arduino Developer Board. Key concepts include wiring a sensor to the Arduino Developer Board, understanding the data collection and transmission sequence of the DHT22 and understanding the C++ library for sensor interaction. With these concepts at hand, implementation of other basic sensors can be combined with the Arduino or other similar boards to provide other sensing capabilities.

References
1) Liu, Thomas. "Digital-output relative humidity & temperature sensor/module AM2303." Adafruit. N.p., n.d. Web. 30 Mar 2012. <http://www.adafruit.com/datasheets/DHT22.pdf>. 2) "Arduino library for DHT11DHT22, etc Temp & Humidity Sensors." Github. N.p., n.d. Web. 30 Mar 2012. <https://github.com/adafruit/DHT-sensor-library>.

6|P a ge

Appendix
DHT.cpp
/* DHT library MIT license written by Adafruit Industries */ #include "DHT.h" DHT::DHT(uint8_t pin, uint8_t type) { _pin = pin; _type = type; firstreading = true; } void DHT::begin(void) { // set up the pins! pinMode(_pin, INPUT); digitalWrite(_pin, HIGH); _lastreadtime = 0; } //boolean S == Scale. True == Farenheit; False == Celcius float DHT::readTemperature(bool S) { float f; if (read()) { switch (_type) { case DHT11: f = data[2]; if(S) f = convertCtoF(f); return f; case DHT22: case DHT21: f = data[2] & 0x7F; f *= 256; f += data[3]; f /= 10; if (data[2] & 0x80) f *= -1; if(S) f = convertCtoF(f); return f; } } Serial.print("Read fail"); return NAN; } float DHT::convertCtoF(float c) { return c * 9 / 5 + 32; } float DHT::readHumidity(void) { float f; if (read()) {

7|P a ge

switch (_type) { case DHT11: f = data[0]; return f; case DHT22: case DHT21: f = data[0]; f *= 256; f += data[1]; f /= 10; return f; } } Serial.print("Read fail"); return NAN; }

boolean DHT::read(void) { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; unsigned long currenttime; // pull the pin high and wait 250 milliseconds digitalWrite(_pin, HIGH); delay(250); currenttime = millis(); if (currenttime < _lastreadtime) { // ie there was a rollover _lastreadtime = 0; } if (!firstreading && ((currenttime - _lastreadtime) < 2000)) { return true; // return last correct measurement //delay(2000 - (currenttime - _lastreadtime)); } firstreading = false; /* Serial.print("Currtime: "); Serial.print(currenttime); Serial.print(" Lasttime: "); Serial.print(_lastreadtime); */ _lastreadtime = millis(); data[0] = data[1] = data[2] = data[3] = data[4] = 0; // now pull it low for ~20 milliseconds pinMode(_pin, OUTPUT); digitalWrite(_pin, LOW); delay(20); cli(); digitalWrite(_pin, HIGH); delayMicroseconds(40); pinMode(_pin, INPUT); // read in timings for ( i=0; i< MAXTIMINGS; i++) { counter = 0; while (digitalRead(_pin) == laststate) { counter++; delayMicroseconds(1); if (counter == 255) { break;

8|P a ge

} } laststate = digitalRead(_pin); if (counter == 255) break; // ignore first 3 transitions if ((i >= 4) && (i%2 == 0)) { // shove each bit into the storage bytes data[j/8] <<= 1; if (counter > 6) data[j/8] |= 1; j++; } } sei(); /* Serial.println(j, DEC); Serial.print(data[0], HEX); Serial.print(", "); Serial.print(data[1], HEX); Serial.print(", "); Serial.print(data[2], HEX); Serial.print(", "); Serial.print(data[3], HEX); Serial.print(", "); Serial.print(data[4], HEX); Serial.print(" =? "); Serial.println(data[0] + data[1] + data[2] + data[3], HEX); */ // check we read 40 bits and that the checksum matches if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) { return true; }

return false; }

9|P a ge

DHT.h
#if ARDUINO >= 100 #include "Arduino.h" #else #include "WProgram.h" #endif /* DHT library MIT license written by Adafruit Industries */ // how many timing transitions we need to keep track of. 2 * number bits + extra #define MAXTIMINGS 85 #define #define #define #define DHT11 11 DHT22 22 DHT21 21 AM2301 21

class DHT { private: uint8_t data[6]; uint8_t _pin, _type; boolean read(void); unsigned long _lastreadtime; boolean firstreading; public: DHT(uint8_t pin, uint8_t type); void begin(void); float readTemperature(bool S=false); float convertCtoF(float); float readHumidity(void); };

10 | P a g e

Вам также может понравиться