Reading Temperature & Humidity with DHT22 (AM2302) sensor module, Arduino mega

Reading Temperature & Humidity with DHT22 (AM2302) sensor module, Arduino mega

This new tutorial centers on reading temperature and humidity with AM2302 (DHT22) sensor module interfaced to an Arduino board. I’ve chosen the DHT22 over DHT11 because it has a wider range of measurement, 0 to 100% for humidity and -40°C to +125°C for temperature. Also it has a digital output (Single-bus) that provides higher data accuracy.

I have also included the DHT library to make it easier for the user. Download the library and unzip to a folder, the folder should contain three files and a folder:

  • examples folder
  • DHT.cpp
  • DHT.h
  • README.txt

copy the DHT folder to arduino installation folder\library and that is all.

 

You can also check out:

 

The materials for these tutorials are all listed below and available at our STORE:

 

Circuit Diagram

am2302_bb - steinacoz

The LED is connected to pin D10 of the Arduino, the out pin of the AM2302 (DHT22) is connected to pin D4 of Arduino, a resistor of value between 1k – 10k ohms is connected between the sensors VCC pin and out pin and finally, the sensor’s VCC pin is connected to the Arduino’s 5volts pin.

NOTE:

Some of these sensors have four pins but the AM2302 version comes with only three. The connection is quite the same.

Copy these lines of codes and upload to your Arduino board:

/*
 * code by Ugwumsinachi Nnadi for Steinacoz.com/blog,
 * All the components for these tutorial can be found in our store section steinacoz.com/shop
 */

#include "DHT.h"

#define DHTPIN 4     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define led 10

int threshold_Hum = 60;
int threshold_Temp = 30;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600); 
  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' 
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  if(h > threshold_Hum || t > threshold_Temp) {
      digitalWrite(led, HIGH);
  } else {
     digitalWrite(led, LOW); 
  }
  
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C ");

}

 

from the code, i added a threshold values to be 30 for temperature and 60 for humdity. The LED lights up whenever

this values are exceeded. The serial monitor outputs the data from the sensor as shown below:

 

am2302_serial - steinacoz
am2302_serial – steinacoz

Feel free to add your comments below or visit our social media outlets for more tutorials like these.

Leave a Reply

Your email address will not be published. Required fields are marked *