DHT11 and DHT22 temperature and humidity, on Arduino

Wire either sensor to an Arduino, read temperature and humidity over serial, and know which of the two to buy — they are not interchangeable.

5 min readUpdated 3 Sept 2026
ShareWhatsApp

The DHT is the sensor almost every weather-station and greenhouse project starts with. It is cheap, it needs one pin, and it gives you temperature and humidity from a single part — which is why it turns up in more student projects than any other sensor.

It is also slow, low-precision and easy to wire in a way that almost works. All three of those are worth knowing before you build around it.

Which one to buy

The blue one and the white one are not the same sensor.
DHT11 (blue)DHT22 / AM2302 (white)
Temperature range0 to 50 °C-40 to 80 °C
Temperature accuracy+/- 2 °C+/- 0.5 °C
Humidity range20 to 80 % RH0 to 100 % RH
Humidity accuracy+/- 5 %+/- 2 %
Readings per second10.5 — one every two seconds
PriceThe cheaper oneRoughly twice as much

Same wiring, same library, one line of code different. The DHT11 cannot read below 0 °C at all, so anything involving a fridge, a cold store or a hill station needs the DHT22.

What you need

  • An Arduino Uno, Nano or Mega — any of them
  • A DHT11 or DHT22 sensor
  • A 10 kΩ resistor, unless your sensor is on a small breakout board that already has one
  • Three jumper wires

Wiring

A DHT11 wired to an Arduino Uno: VCC to 5V, data to pin 2 with a 10k resistor up to 5V, and GND to ground.
The bare four-pin sensor. The third pin from the left is not connected to anything.
DHT pinGoes toNote
1 — VCC5V3.3 V also works, but readings drift a little
2 — DATAPin 2And through 10 kΩ up to 5V
3nothingGenuinely unused on the four-pin part
4 — GNDGND

Pin 1 is the one nearest the corner of the grille when the grille faces you. A three-pin module is usually marked + / OUT / - and needs no resistor.

The library

The sensor speaks a timing-critical one-wire protocol of its own, and reading it by hand is not a useful exercise. Use Adafruit's library — in the Arduino IDE, Sketch → Include Library → Manage Libraries, search for DHT sensor library, and install it. It will offer to pull in Adafruit Unified Sensor as well; say yes, or the examples will not compile.

The sketch

dht_read.inoArduino C++ · 33 lines
#include "DHT.h"

#define DHTPIN  2       // the sensor's data pin
#define DHTTYPE DHT11   // change to DHT22 for the white one

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  // The DHT is slow. Ask it faster than once every two seconds and it hands
  // back the previous reading again rather than a fresh one.
  delay(2000);

  float humidity = dht.readHumidity();
  float celsius  = dht.readTemperature();

  // A failed read returns NaN, not zero. Checking for it is what stops the
  // serial monitor filling with 0.00 while you believe the sensor works.
  if (isnan(humidity) || isnan(celsius)) {
    Serial.println("No reading - check the data pin and the pull-up resistor");
    return;
  }

  Serial.print("Humidity ");
  Serial.print(humidity);
  Serial.print(" %   Temperature ");
  Serial.print(celsius);
  Serial.println(" C");
}
Open the serial monitor at 9600 baud. A reading every two seconds.

How the code works

  1. 1

    DHTTYPE picks the sensor

    One line is the entire difference between the two parts. Set it wrong and you get readings that are plausible but incorrect, which is worse than getting none.

  2. 2

    delay(2000) is a requirement, not politeness

    The sensor takes about two seconds to make a measurement. Poll it faster and the library returns its cached value, so your graph looks smooth and is lying to you.

  3. 3

    isnan() catches the failure

    A failed read is Not a Number, and NaN printed with Serial.print appears as nan — but NaN used in arithmetic silently poisons everything downstream. Test for it before you use the value, every time.

When it does not work

What you seeCauseFix
No reading every timeNo pull-up, or DATA on the wrong pinFit the 10 kΩ resistor. Check DHTPIN matches the wire.
Readings for a while, then nothingPolled too fast, or a long unshielded data wireKeep to two seconds. Keep the wire under about 20 cm.
Humidity always 0 or always 100DHT11 asked for a value outside 20–80 % RHIt cannot read there. Use a DHT22.
Temperature 2–3 °C too highThe sensor is sitting on the board, warmed by the regulatorMove it away on wires. This is real and it is the commonest complaint.
Nothing at all in the serial monitorBaud rate mismatchThe monitor must be set to 9600 too.

Where to take it

  • Add an LCD or OLED and it is a room monitor with no computer attached.
  • Add an ESP32 or ESP8266 and it logs to a phone — the same sensor, the same library.
  • Add a relay and a threshold and it is a greenhouse or an incubator controller, which is a full final-year project.
  • Log to an SD card with a timestamp and you have data for the results chapter of a report, which is the part most projects are thin on.

What you need for this

Everything above works with parts from anywhere. These are what we stock, if it is easier.

Read next

Written by us for projectkit.in. If you spot something wrong, tell us — a reference page that is confidently wrong is worse than no page, and we would rather hear it from you than from a viva panel. Back to the Knowledge Base.

WhatsApp