Unit D - Sensors & Buses

18. DHT11 / DHT22 Temperature & Humidity

Use a library-based digital environmental sensor.

Estimated time 3 hours

Learning outcomes

  • Explain what temperature and relative humidity the DHT reports
  • Compare DHT11 and DHT22 specs and constructor types
  • Wire a bare sensor (with pull-up) or a 3-pin module
  • Read with the Adafruit DHT library and reject failed readings with isnan

Parts and preparation

Uno, DHT11 or DHT22 sensor/module, 4.7-10 k ohm pull-up for a bare sensor and jumpers. Install both Adafruit libraries listed in the Libraries box.

Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.

Libraries for this lesson

In Arduino IDE 2 open Tools → Manage Libraries…. Search by the Library Manager name and install the package by the exact author below. Similar names from other authors can use different APIs and will break the example.

IncludeLibrary Manager nameAuthorInstall note
DHT.hDHT sensor libraryAdafruitInstall via Library Manager.
(dependency)Adafruit Unified SensorAdafruitRequired dependency of the DHT sensor library. Install via Library Manager if prompted or if compile fails.
DHT11 / DHT22 Temperature & Humidity instructional connection diagram

What a DHT reports

A DHT family sensor measures air temperature (C) and relative humidity (%RH). It is a digital single-wire device: VCC, DATA and GND (plus NC on many bare 4-pin packages). It is not an analogue ADC sensor and not an I2C device.

Inside the package, sensing elements and a small controller exchange a timed bit protocol on DATA. Use a library for the first practical - hand-timing that protocol is easy to get wrong.

Three cards showing temperature in Celsius, humidity in percent RH, and a single-wire VCC DATA GND interface
Temperature and %RH over one DATA wire. Use a library for the bit protocol.

DHT11 vs DHT22

DHT11 is the cheap course default: modest range and coarse steps (about 1 C and 1 %RH). DHT22 (also sold as AM2302) covers a wider range with finer resolution and better accuracy, at higher cost.

Wiring is the same idea for both. The Adafruit constructor type must match the part you fitted: DHT11 or DHT22. A mismatch often produces failed reads or nonsense values.

Side-by-side comparison of DHT11 and DHT22 ranges, resolution and constructor type constants
Same wiring pattern; change DHT11 to DHT22 in code when you swap sensors.
ItemDHT11DHT22
ConstructorDHT11DHT22
Typical useLearning / rough checksBetter accuracy projects
ResolutionCoarseFiner
Read intervalAbout 2 s or slowerAbout 2 s or slower

Bare sensor vs module wiring

A common bare 4-pin DHT, viewed from the front grill, is often VCC, DATA, NC, GND left to right. Pin 3 is not connected. You must add a 4.7-10 k ohm pull-up from DATA to VCC.

Many 3-pin modules already include the pull-up and label VCC / DATA / GND on the silkscreen. Pin order is not universal - always check the board text before powering.

Bare four-pin DHT with VCC DATA NC GND and required pull-up contrasted with a three-pin module that often includes the pull-up
Course: DATA to D2, VCC to 5 V, GND shared. Bare parts need the DATA pull-up.
ConnectionCourse practice
VCC5 V
DATAD2
GNDUno GND
Bare pull-up4.7-10 k ohm DATA to VCC
Bare pin 3NC - leave open

Sampling interval

DHT sensors need recovery time between readings. Reading much faster than about once every two seconds often returns failed or stale data. The worked sketch uses delay(2000) before each read.

Timeline of DHT reads separated by waits of at least two seconds
Wait at least about 2 seconds between reads.

Libraries and isnan

Install DHT sensor library by Adafruit and the Adafruit Unified Sensor dependency. Call dht.begin() in setup, then readHumidity() and readTemperature() in loop.

A failed read returns NaN (not a number). Always check isnan on both values before printing, logging or driving an LCD. The extension challenge is to keep the previous LCD values when a read fails.

Flow from DHT read functions through an isnan guard to either fail handling or printing valid values
Guard with isnan before you trust the floats.
#include <DHT.h>
const byte dhtPin = 2;
DHT dht(dhtPin, DHT11);  // use DHT22 if that is the part fitted

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

void loop() {
  delay(2000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("DHT read failed");
    return;
  }
  Serial.print(t, 1);
  Serial.print(" C  ");
  Serial.print(h, 1);
  Serial.println(" %RH");
}

Wiring and safe build sequence

Breadboard wiring for lesson 18: DHT11 / DHT22 Temperature & Humidity
Breadboard layout for this lesson. Match colours and pins before powering the circuit. Click the image for a larger view.
  1. VCC -> 5 V
  2. GND -> GND
  3. DATA -> D2
  4. Bare sensor: 4.7-10 k ohm from DATA to VCC
  5. Pin 3 of a common bare four-pin package is not connected
Power rule: switch off before moving wires. Arduino I/O pins are control signals; high-current loads require a driver and suitable external supply.
#include <DHT.h>

const byte dhtPin = 2;
DHT dht(dhtPin, DHT11);  // use DHT22 if that is the part fitted

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

void loop() {
  delay(2000);
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("DHT read failed");
    return;
  }
  Serial.print(temperature, 1);
  Serial.print(" C  ");
  Serial.print(humidity, 1);
  Serial.println(" %RH");
}

How the code works

  1. isnan detects the library's failed floating-point reading.
  2. The two-second delay respects sensor timing.
  3. Change DHT11 to DHT22 in the constructor when you fit a DHT22 / AM2302.

Test and record evidence

Expected result: Serial reports plausible temperature and relative humidity every two seconds.

Practical evidence checklist

Common faults and checks
  • Verify sensor orientation and module pin labels.
  • Install both required Adafruit libraries.
  • Add/check the data pull-up for a bare sensor.
  • Confirm the constructor type matches DHT11 or DHT22.
  • Slow the loop if reads fail when polled too quickly.
Extension challenge: Display both values on the I2C LCD and retain the previous display when a read fails.

Check your understanding

Q1. Why check isnan?

Show answer

A failed DHT read returns a not-a-number value.

Q2. How frequently should a DHT11 be read?

Show answer

About once every two seconds or slower.

Q3. What must you change in code when swapping DHT11 for DHT22?

Show answer

The type constant in the DHT constructor (DHT22).

Q4. Why does a bare DHT need a pull-up on DATA?

Show answer

DATA is an open single-wire line; without a pull-up the level is undefined when idle.