AI Lab for the Internet of Things

From sensors to intelligent self-deciding systems: lessons, projects, architectures, interactive simulation labs and a specialist AIoT assistant.

20Lesson

DHT22

A low-cost digital sensor that measures temperature and humidity and sends them over a single data wire.

BeginnerEnvironmental

Illustration of DHT22
A schematic illustration created for this section for teaching purposes; it is not a photograph of the part or of any specific product.

Educational content in scientific-draft form; a specialist should review it before it is adopted as an academic reference. No measurement or performance result that was not actually obtained is shown here.

Facts

Measured quantityTemperature and relative humidity
Data typeDigital (single wire)
Units of measurement°C, %
Examples of common partsDHT22 / AM2302

How it works

It contains a capacitive element for measuring humidity and a thermal resistor for measuring temperature, and a circuit that converts both measurements into a digital signal.

How to connect

Connected with power, ground and a data wire to a digital pin on the microcontroller, with a pull-up resistor where needed according to the datasheet.

AIoT applications

  • Monitoring the home environment
  • Greenhouses

Measurement and calibration issues

  • A slow reading rate
  • Limited accuracy that needs comparison with a reference
  • Occasional failed readings that must be handled

Code example

CPPIllustrative, not implemented or tested
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

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

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

Simulations and labs run inside your browser. Nothing you enter and none of the conversations is stored on the server. Privacy policy