Part

Temperature and humidity, DHT22

One wire, forty bits and a checksum. Drag the two sliders for the weather it reads.

or see every part
  • 3 pins
  • 5 properties
Drawn live by the editor's own code, at the size you see it.
Reference

Every one of its 3 pins

Pin
What it does
VCC
DATA
GND
This part

What it does

The DHT22 and its cheaper sibling the DHT11 measure temperature and humidity and report both over one data wire, with no bus standard behind it at all. The board pulls the line low to ask, lets go, and the sensor answers with forty pulses whose widths are the bits, ending in a checksum. Mokxi decodes nothing on the sensor’s behalf: the part drives the pin with the datasheet’s timing and the sketch has to measure it, exactly as on a desk. It also keeps the two habits that confuse people most. A request in the first second after power-up is ignored, so the first reading fails; and the DHT22 only measures every two seconds, handing back its last reading in between, so a sketch that polls faster sees a number that seems stuck. The model property switches between the DHT22 and the DHT11, whose bytes mean different things on the same wire.

How to use it

How to use the DHT22 with an Arduino

Wire VCC to 5 V, GND to GND and DATA to any digital pin; a three-pin module has its pull-up on board, and a bare four-pin sensor needs a 10 k resistor from DATA to VCC. Then ask for a reading no more than once every two seconds, and check every reading before you use it, because a failed one comes back as nan.

The Adafruit DHT library’s calls compile here as they stand. The floats it returns print normally and take arithmetic of your own; the built-in weather station works in whole tenths only to stay small on the Uno.

Part pin
Board pin
VCC
5 V
DATA
Pin 2
GND
GND

The DHT library compiles on every board here, the ESP32 and the Pico included, and the sensor is happy on 3.3 V.

Arduino Uno: DHT22 weather stationlive0.000 s 0.00x
Click to open it in the editor
This is the simulator itself, running here. Click anything to open it in the editor.
Reading a DHT22 with the DHT library
#include "DHT.h"

DHT dht(2, DHT22);

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

void loop() {
  delay(2000);
  float t = dht.readTemperature();
  if (isnan(t)) return;
  Serial.println(t);
}
Reference

The 5 properties you can set

Property
Default
What it means
model
dht22 (or dht11)
temperature
22
humidity
50
pullup
true
pullup_ohms
5100
How it is modeled

What is true about the Temperature and humidity, DHT22, here

The DHT11 and the DHT22

One wire, three pins, forty bits. The blue brick is the DHT11 and the white one is the DHT22 (also sold as the AM2302); the wire is identical and only the five bytes differ.

Not modeled

The accuracy: +/-2 %RH and +/-0.5 C on a DHT22, +/-5 and +/-2 on a DHT11. The number the slider says is the number that goes on the wire. Nor the seconds the capacitive element takes to follow a change in the air, nor self-heating.

From Signals you time, not read, in full.

Projects

See the Temperature and humidity, DHT22 in a project

Shown here on: Arduino Uno R3

Learn

Where it turns up in a lesson

In a learn article

Wire up the Temperature and humidity, DHT22

Open the editor and push it into the breadboard. It is free, and it runs on your own machine.