DHT22 Temperature and Humidity Sensor User Information

SETTING UP AND USING THE DHT22 (AM2302)

The DHT22 is a digital temperature and humidity sensor - also know as AM2302.

It works easily with Arduino, Raspberrry Pi, ESP8266 and other platforms to create
monitoring and data collection projects.

SETTING UP
The DHT22 has 4 pins but only three are used.
Pin 1 is for power which with most applications will be 5v - the specification is from 3v to 6v but in testing, I've found that below 5v the data output can be unreliable. Most boards it is likely to be used with offer both 3.3v and 5v.
Pin 2 is the Data pin - this needs to pulled up with a 10k resistor connected to the 5v positive line.
Connect it to the data input pin of your Arduino, ESP8266 or other board.
Pin 3 is not used
Pin 4 connects to Ground

Approximate Dimensions: 25mm H 15mm W 8mm D

GET YOUR DHT22 SENSOR HERE

PROGRAMMING
The DHT22 is slow to start, taking several seconds to give out meaningful information, so it's a good idea to build in a startup delay of at least a few seconds to your programme, whichever platform you are using. It's also helpful to catch any errors at the start or during operation - in the Arduino you can do this by using 'isnan' ('is not a number') along with a floating point variable. For data display, I usually use an integer (whole number) for the temperature readings.

Combining this sensor with real time clock and sd card modules will give you a record of temperature and humidity over a period of time that can then be analysed. Using it with WiFi (eg with ESP8266) or other communication methods will let you monitor a remote location, such as a greenhouse or industrial space.

Mortimer Rhind-Tutt

Here is some sample code for Arduino which you can copy and paste to get started with the DHT22:

/*

  WWW.Q26.CO.UK

  TEMPERATURE AND HUMIDITY using DHT22 SENSOR

  Basic set up and data collection (Mortimer Rhind-Tutt 10/05/2019)

***************************************************************

  Connect a DHT22 sensor
  This sketch can also be used with ESP8266
  to add WifI and monitor data remotely.

  Operation:
  Open the serial monitor in Arduino IDE to see the data.
  In the setup code a delay is introduced to let the sensor start up,
  which can take a few seconds.

  The variables for reading the data are set as 'float' which if not recognised
  by 'isnan' (is not a number) will trigger a message to wait a bit longer.

  After that, the loop runs, the sensor is read and an integer (whole number)
  version of temperature and humidity data is displayed in the serial window
  and refreshed every 5 seconds.
***************************************************************************************************/

/*

  CONNECT SENSOR:
  PIN 2 DHT22 Data to Digital Pin 2 on your board (you can use any pin...)
  10K Pullup resistor from Data Pin (PIN 2) to Vcc (5v)
  PIN 1 to Vcc (5v)
  PIN 4 to Gnd
  PIN 3 is not used - leave unconnected

**********************************************************/

// Include Library for Sensor
#include "DHT.h"

//Specify the sensor type DHT22
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

// Set up the data input pin on your board
const int DHTPin = 2;   //change this if you use a different input pin

// Initialize DHT sensor
DHT dht(DHTPin, DHTTYPE);


// Variables for Temperature and Humidity
float t = 0;
float h = 0;

/**********************************************/

void setup() {

  // Open serial port
  Serial.begin(9600);
  delay(10);

  Serial.println();
  Serial.println("**********************");
  Serial.println("Sensor Starting");

  dht.begin();
  delay(5000);

  h = dht.readHumidity();
  t = dht.readTemperature();
  if (isnan(h) || isnan(t)) //check for errors on startup
  {
    Serial.println("INITIALISING");
    delay(5000);
  }
}
/**********************************************/

void loop() {

  h = dht.readHumidity();
  t = dht.readTemperature();
  if (isnan(h) || isnan(t)) //Check for errors and delay if necessary
  {
    Serial.println("DATA ERROR");
    delay(5000);
  }

  h = dht.readHumidity();
  t = dht.readTemperature();

  Serial.println((String)"Humidity: " + int(h) + " %");
  Serial.println((String)"Temperature: " + int(t) + " %");

  delay(5000);

}

/**********************************************/

//End