From sensors to intelligent self-deciding systems: lessons, projects, architectures, interactive simulation labs and a specialist AIoT assistant.
DHT22
A low-cost digital sensor that measures temperature and humidity and sends them over a single data wire.
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 quantity | Temperature and relative humidity |
|---|---|
| Data type | Digital (single wire) |
| Units of measurement | °C, % |
| Examples of common parts | DHT22 / 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
#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
