DHT22 Temperature Humidity Sensor
DHT22 Temperature Humidity Sensor
DHT22 Temperature Humidity Sensor
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
Graph 1
5|P a ge
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