ESO还在用手拧电位器测温的年代,DHT系列几乎是每个刚上手ESP32的人绕不开的第一颗传感器。它把温度和湿度塞进一个四脚小模块里,一根数据线就能把读数拿到手。这篇把DHT22(也是AM2302)接到ESP32上,从接线、装库到读出带小数的温湿度,一次走通。DHT11的价格更低,但精度和量程差一截,代码几乎完全一样,文末会对比两者差异。

先认模块:DHT11和DHT22差在哪
外壳上印着ASAIR AM2302的就是DHT22,蓝色小方壳是DHT11。两者内部都是一颗电阻式湿度元件加一个NTC测温单元,由内部芯片做模数转换后通过单总线输出。区别在指标:DHT11测温0到50摄氏度、误差正负2度,湿度量程20%到80%、误差正负5%;DHT22能测零下40到125摄氏度、误差正负0.5度,湿度全量程、误差正负2%到3%。采样率上DHT11一秒一次,DHT22两秒一次。做室内环境监测DHT11够用,要进户外或做数据记录,DHT22的精度和量程才撑得住。下面这张对比表把关键参数摆在一起了。

接线:三根线加一个上拉电阻
模块只有四个脚:VCC接3.3V,GND接地,DATA接ESP32的GPIO4,NC空着不接。VCC和DATA之间建议串一个10千欧上拉电阻,很多成品模块板载了电阻,裸传感器才需要自己补。引脚别看错方向,DHT22正面朝自己时从左到右是VCC、DATA、NC、GND,接反了模块会发热,读数一直是NaN。3.3V供电完全够用,整块板子和传感器可以共用ESP32的3V3引脚。

装库:两个库都要装
在Arduino IDE的库管理器里搜DHT sensor library,选Adafruit那个装上;装的过程会提示依赖Adafruit Unified Sensor,一并装了。这两个库缺一个都会报编译错误,报错信息是一长串找不到Adafruit_Sensor.h,看到它先想到库没装齐就行。
完整代码
下面是Adafruit示例草图,注释里写得很清楚,读数、换算华氏度和体感温度都在里面:
// Example testing sketch for various DHT humidity/temperature sensors written by ladyada
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include "DHT.h"
#define DHTPIN 4 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
读数与常见坑
烧录后打开串口监视器,波特率9600,两秒出一行湿度、摄氏度、华氏度和体感温度。第一次上电读数偏低很正常,DHT系列需要一两分钟预热稳定。读出NaN先查三件事:上拉电阻有没有接、DATA是不是接了GPIO4以外的脚但代码里DHTPIN没改、以及采样间隔是不是小于两秒——读得太勤传感器会返回失败。
serial.println里的Failed to read from DHT sensor就是典型的间隔过短或接触不良,把delay拉到2000毫秒以上,重新动一动杜邦线基本能解决。DHT22的DATA线长度建议控制在20米以内,再长就得考虑改用带校准的SHT系列了。











上拉电阻真的不能省,裸传感器接上去读数全是NaN,折腾一晚上
GPIO4换到别的脚记得改DHTPIN,我第一次就栽在这
体感温度那个字段原来算法在库里有,涨知识了
预热两分钟这条记下了,新模块刚上电读数虚低我还以为坏了
终于有人把DHT11和DHT22的精度差讲清楚了,之前买错一次