DHT22/Reading it/03. The first reading
Reading it · 03 of 9

The first reading

Three wires, Adafruit's DHT sensor library, and a sketch whose one DHT22-specific line is #define DHT_TYPE DHT22. It prints a temperature and a humidity in tenths every two seconds, and the first reading waits two seconds after power-up, because the datasheet asks for at least one.

Three wires

Pick your board and the drawing names the pins.

Three wires
ESP32 · DATA on GPIO 18
Your board
VCC to
3V3
DATA to
GPIO 18
DATA idles at
3.3 V
3V3, because DATA idles at VCC. The board’s 10 kΩ pull-up joins them, so VCC has to be your ESP32’s own 3.3 V. That is also the bottom of the DHT22’s 3.3 to 6 V range: it works there, with nothing to spare for a long, thin supply wire.

GND to GND. DATA to the pin this book uses for your board: D2 on an Uno, GPIO 18 on an ESP32, GPIO 4 on an ESP32-S3, GP2 on a Pico. NC gets nothing.

VCC is the wire with a rule. The 10 kΩ pull-up on the board joins DATA to VCC, so the data line idles at whatever you feed VCC, and it has to be your board's own logic voltage: 5V on an Uno, 3V3 on the other three. 3.3 V or 5 V in the DHT11 book is the whole argument.

On 3V3 the DHT22 is at the bottom of its 3.3 to 6 V range. It is specified there, so it works, but a long thin supply wire or a 3V3 rail that dips has nothing to eat into. The DHT11 had 0.3 V more room.

The library

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

The sketch

One line is the DHT22's:

#define DHT_TYPE DHT22

Nothing on the wire says which sensor is there, so the library believes this line. It decides how long the start pulse is, 1.1 ms for a DHT22 against 20 ms for a DHT11, and how the five bytes that come back are read. The wrong type is what happens when it is wrong.

The delay(2000) comes first in loop() rather than last. The datasheet's §6 asks for no instruction in the first second after power-up, and dht.begin() lets the first read happen immediately. Waiting first covers both that and the two seconds between readings. What happens on the wire during a read is the start and the answer.

What to expect

A line every two seconds, such as 23.40 C 51.20 %. The second decimal is always 0 because the sensor sends tenths.

Now and then, a read failed. Every fault this protocol has arrives as nan, so the isnan() check is part of the sketch, not decoration. A run of them is when every reading is nan.

The code

dht22_first_reading.ino

Prints the temperature and humidity every two seconds, to two decimal places of which the second is always 0: the DHT22 sends tenths. Set DHT_PIN to the pin you wired DATA to.

/*
  DHT22 - first reading                                 TK39 / /p/tk39

  Wiring. Count from the square pad on the TinkerBlock board, sensor
  side up, header at the bottom:

    GND  -> GND
    VCC  -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico.
            DATA idles at VCC through the board's 10k pull-up,
            so VCC is your board's logic voltage.
    NC   -> nothing   (unconnected on the board)
    DATA -> D2 on an Uno, GPIO 18 on an ESP32,
            GPIO 4 on an ESP32-S3, GP2 on a Raspberry Pi Pico

  Arduino IDE
    Tools > Board                 your board, e.g. ESP32S3 Dev Module
    Tools > Port                  the one that appears when you plug in
    Tools > USB CDC On Boot       Enabled   (ESP32-S3 only)
    Tools > Manage Libraries      DHT sensor library, by Adafruit.
                                  Say yes to Adafruit Unified Sensor.
*/

#include <DHT.h>

// The pin DATA is wired to.
// Uno: 2. ESP32: 18. ESP32-S3: 4. Pico: 2.
#define DHT_PIN  4
#define DHT_TYPE DHT22   // the white cage. DHT11 is the blue one.

DHT dht(DHT_PIN, DHT_TYPE);

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

void loop() {
  // First, so the first read comes 2 s after power-up: the
  // datasheet wants 1 s of quiet, and the DHT22 more than 2 s
  // between readings.
  delay(2000);

  // One exchange on the wire. The second call is answered from
  // the library's copy of the same five bytes.
  float humidity = dht.readHumidity();
  float celsius  = dht.readTemperature();

  // A failed read is nan, not an error. Check every time.
  if (isnan(humidity) || isnan(celsius)) {
    Serial.println("read failed");
    return;
  }

  Serial.print(celsius);
  Serial.print(" C  ");
  Serial.print(humidity);
  Serial.println(" %");
}

Every line says read failed: nothing answered, so check GND, VCC and the DATA pin in that order. Numbers that are steady but silly, 2.00 % and 0.00 C, mean DHT_TYPE says DHT11. The sketch compiles for an Uno and an ESP32-S3.

View on GitHub · blocks/tk39-dht22/arduino/dht22_first_reading/dht22_first_reading.ino @ v1.13

When it does not work

DHT.h: No such file or directory

The library is not installed. In Tools > Manage Libraries search DHT sensor library and install Adafruit's; several others are called DHT-something and none has the same functions. Accept Adafruit Unified Sensor when the IDE offers it, or it will not compile.

Every line says read failed

Nothing answered on the wire. Check GND, then that VCC is on a pin that is actually powered, then that DATA is on the pin DHT_PIN names. Count from the square pad: the third pin, NC, is connected to nothing, so a wire one pin out looks exactly like a dead sensor.

It prints 2.00 % and 0.00 C, and never changes much

The wiring is right and DHT_TYPE says DHT11. The DHT22 answered and its checksum passed; the bytes were then read as a DHT11's. The white cage is DHT22.

The Serial Monitor is empty on my ESP32-S3

Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port brings up no serial port when the sketch starts, so it runs with nowhere to print. Then check the monitor is at 115200 baud.

Every number ends in 0 after the decimal point

It should. The DHT22 sends tenths, and Serial.print of a float prints two decimal places, so the second is always 0. Serial.print(celsius, 1) prints the one place the sensor actually has.

Where this goes next

How 23.4 °C and −5.3 °C each fit in sixteen bits, and what a decoder that forgets the sign prints.

Tenths and the sign bit →

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 →