Arduino tutorial 5# how to Measure humidity & Temperature with DHT22/DHT11 in Arduino.

Measure humidity & temperature with Arduino

Hi friends. In this tutorial we will see how to measure temperature & humidity with  DHT22/DHT11 in Arduino so lets get started. For this you will need

  1. Arduino,
  2. jumper wire,
  3. breadboard,
  4. 10K ohms resistor,
  5. DHT22 sensor.

There are two version of sensor. One is DHT11 & second is DHT22.

DHT11 is cheaper in cost and less accurate compare to DHT22.

DHT22 is more accurate and have larger range compare to DHT11.

In this tutorial we are using DHT22. Sensor have four pin. Pin 1 is for : 5V power, Pin 2 is for: Data , Pin 3: not used, Pin 4 is for: GND.

Circuit diagram for DHT22 with arduino
Circuit diagram for DHT22 with arduino

This is circuit diagram. 10k ohms resistor is used which is a pull-up resistor because Random noise can change the state to go HIGH or LOW level randomly. So, The pull-up resistor ensures that the wire is at a defined logic level even if no active devices are connected to it. Do connection as shown in diagram.

Go to github website the link is mentioned below in the description. And download the file. After downloading unzip it. Change the file name to DHT.

Delete this file

Delete these files. shown in right side of image. And copy the DHT folder & paste in Arduino library folder. Now open the example folder then DHT tester. Open DHT tester file you can directly upload the sketch.

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.

void setup() {
  Serial.println("DHTxx test!");


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

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(" *C ");
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(" *C ");
  Serial.println(" *F");

The purpose of the instruction “#include” is to include an external library in our project so we can use its functions. Define  DHT pin number that is pin 2. Define DHT type 22 or 11. Then initialize the library. Serial.begin (9600) it will create serial connection with Arduino. Serial.println will print hello massage when serial monitor is started. Dht.begin will start DHT. Lets going to loop function. We take measurement for humidity, temperature in Celsius & Fahrenheit using Float point h, t & f. “isnan” for  is not a number. This  is || OR Boolean operator.  If value of h,t or f is not a number it will print Failed to read from DHT sensor!. If you don’t want heat index you can delete it. Go to serial monitor now you can see the values of humidity, temperature in Celsius & Fahrenheit.

Components list to buy online:  (DHT22)   (Arduino)  (Breadboard)   (Jumper wire)  (Resistor)


For more videos subscribe the channel

Till than keep learning keep making. 🙂