DHT sensors/What is in the box/04. The first reading
What is in the box · 04 of 12

The first reading

Three wires, one library from the Library Manager, and a dozen lines. The only thing that changes between the blue board and the white one is a single word in the constructor — and getting that word wrong is the fault that prints numbers instead of errors.

Wire it

Pick your board and the drawing names the pins.

Three wires
DHT22 → Arduino Uno
Your board
Which sensor
GND
GND
VCC
5V
DATA
D2
Two of the three wires are free choice; VCC is not. DATA can go on any pin that is not already spoken for, and GND is GND. But the 10 kΩ resistor on the board ties DATA to whatever you put on VCC, so on a 5 V board VCC goes to 5V and nowhere else — feed it 5 V beside a 3.3 V processor and the data line idles at 5 V against a pin that is not rated for it. The DHT22 is happy anywhere from 3.3 to 6 V, so the supply is decided by your board, not by the sensor.

GND to GND, VCC to the supply your board's pins run at, DATA to any free digital pin. NC gets nothing — it is connected to nothing on the board, so a wire there goes nowhere.

The supply is the one that is not free choice. The 10 kΩ resistor on the board ties DATA to VCC, so whatever you feed the sensor is what the data line idles at. On an ESP32, an ESP32-S3 or a Pico that has to be 3V3; on an Uno or a Nano, 5V. 3.3 V or 5 V is the whole of that argument.

Install the library

Tools → Manage Libraries, search DHT sensor library, install Adafruit's. When the IDE offers to install Adafruit Unified Sensor alongside it, say yes — the library declares it as a dependency and will not compile without it.

The sketch

Two lines matter and the rest is printing.

#define DHT_TYPE DHT22   // or DHT11
DHT dht(DHT_PIN, DHT_TYPE);

The type is not detected. Nothing on the wire says which sensor is out there, so the library sends whichever start pulse you asked for and decodes whichever way you told it to. Tell it wrong and it does not fail — it prints a number. That is the whole subject of forty bits and a checksum.

What to expect

A line every two seconds, and occasionally not. readHumidity() and readTemperature() return nan when a reading fails, which is normal at a rate of roughly one in a hundred and is why the check is in the sketch rather than being an optional nicety. nan compares false against everything including itself, so isnan() is the only way to test for it.

The two calls together cost one exchange, not two: the library caches its last reading for two seconds, so the second call comes straight back out of memory. That is convenient here and surprising later, which is how often you may ask.

The code

first_reading.ino

Prints a temperature and a humidity every two seconds. Change DHT_TYPE to match the board you wired: DHT11 for the blue cage, DHT22 for the white one. Nothing else differs between them.

// The first reading: temperature and humidity, every two seconds.
//
// Wiring, sensor to board:
//   Sensor GND  -> board GND
//   Sensor VCC  -> board 5V on an Uno, 3V3 on an ESP32, ESP32-S3 or Pico
//   Sensor NC   -> nothing. It is connected to nothing on the board either.
//   Sensor DATA -> Uno D2, ESP32 GPIO 18, ESP32-S3 GPIO 4, Pico GP2
//
// Count the pins from the square pad: GND, VCC, NC, DATA.
//
// Arduino IDE: install "DHT sensor library" by Adafruit from the Library
// Manager (Tools -> Manage Libraries, search DHT sensor library). When it
// offers to install "Adafruit Unified Sensor" as well, say yes -- it will
// not compile without it. No special Tools settings on any of these boards.

#include <DHT.h>

#define DHT_PIN  2      // Uno D2.  ESP32: 18.  ESP32-S3: 4.  Pico: 2.
#define DHT_TYPE DHT22  // DHT22 for the white cage, DHT11 for the blue one.

DHT dht(DHT_PIN, DHT_TYPE);

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

void loop() {
  // Humidity first, then temperature. The second call does not touch the
  // wire: the library caches for two seconds, so both numbers come out of
  // the same reading rather than out of two readings a millisecond apart.
  float humidity = dht.readHumidity();
  float celsius  = dht.readTemperature();

  // A failed read returns nan rather than throwing, and nan prints as "nan"
  // and compares false against everything. Checking is not optional: one
  // failure in a hundred is normal for this protocol.
  if (isnan(humidity) || isnan(celsius)) {
    Serial.println("read failed");
  } else {
    Serial.print(celsius);  Serial.print(" C  ");
    Serial.print(humidity); Serial.println(" %");
  }

  delay(2000);   // The floor is 2 s for a DHT22, and 2 s in this library
}                // for a DHT11 too. Asking faster returns the old value.

If every line says read failed, nothing answered — check GND and VCC first, then that the pin number matches the wire. If it prints numbers that are obviously silly, the wiring is fine and DHT_TYPE is wrong. Both are covered in when every reading is nan.

When it does not work

DHT.h: No such file or directory

The library is not installed. Search the Library Manager for "DHT sensor library" and install Adafruit's — several other libraries are called DHT-something and none of them has the same function names. Accept the prompt to install Adafruit Unified Sensor with it.

Every line says read failed

Nothing answered on the wire. In order: is GND connected, is VCC on a rail that is actually powered, and is DATA on the pin the sketch names? Then check you counted from the square pad — the third pin along is connected to nothing, so being one position out looks exactly like a dead sensor.

It prints 2.0 % and 0.0 °C, or numbers in the hundreds

The wiring is right and DHT_TYPE is wrong. The bytes arrived and passed their checksum; they were decoded as the other sensor. Blue cage is DHT11, white cage is DHT22, and the back of the board says which.

The Serial Monitor shows nothing at all

That is the serial port rather than the sensor. Check the Serial Monitor's baud rate matches the Serial.begin in the sketch — 115200 here — and that the right port is selected under Tools.

Can I use the DHTesp library instead?

You can, and the old Lonely Binary tutorial did, but its README now opens "This library is no longer maintained". Adafruit's is maintained, works on AVR as well as the ESP boards, and is what every sketch in this handbook uses.

Where this goes next

What happens on the wire in the milliseconds after you call readTemperature.

The start and the answer

Edit this page — content/books/dht22/the-first-reading.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

This page covers several products. Choose yours to see the right discussions.

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum