想给项目加个温湿度监测,DHT11 和 DHT22 是几乎所有教程都会推荐的两块入门传感器。它们长得像、接法一样、代码通用,价格却差着好几倍,第一次买很容易犯迷糊。这篇文章把两者的差别、接线和读数代码一次说清,照着做十分钟就能在串口里看到温湿度。

先分清:DHT11 和 DHT22 差在哪
两颗都是"电容式湿度加热敏电阻温度"的组合传感器,走同一套单总线协议。差别在量程和精度:DHT11 温度量程 0 到 50 摄氏度、湿度 20% 到 90%,精度粗到正负 2 度和正负 5%;DHT22(也叫 AM2302)温度量程扩大到零下 40 到 80 度,湿度 0 到 100%,温度精度正负 0.5 度、湿度正负 2% 到 3%,采样间隔也从一秒拉长到两秒。简单说,屋里玩选 DHT11 就够,要上室外或做数据记录就上 DHT22。

接线:三根线加一颗上拉电阻
模块版(带蓝色或白色塑料壳的)通常已经板载上拉电阻,VCC 接 3.3V 或 5V,GND 接地,DATA 接任意数字脚,教程示例用的是 GPIO4。裸传感器(四脚那版)要自己加一颗 10K 电阻把数据脚拉到电源,1 脚接电源、2 脚接数据、4 脚接地。接线图照着下面这张连就行。

代码:装两个库就能读
在库管理器里装 Adafruit 的 DHT Sensor Library,它会连带要求装 Adafruit Unified Sensor。示例代码原样贴在下面,DHTPIN 按接线改成 4,DHTTYPE 二选一:用 DHT11 就把 DHT22 那行注释掉:
// 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 波特率),每两秒打印一行湿度、摄氏和华氏温度,还有体感指数。体感指数是把温湿度折算成"人感觉有多热"的参考值,夏天做室内监测时挺有意思。

读不出数?三个常见坑
串口报 Failed to read from DHT sensor 时按顺序查三件事:一是供电,裸传感器接 3.3V 时部分板子驱动不稳,换 5V 再试;二是上拉电阻,模块版不用加,裸版必须加 10K;三是读取间隔,DHT22 官方要求两次读取间隔不小于两秒,读太勤会返回 NaN。另外这颗传感器对新风吹气都没反应是正常的——它测的是缓慢变化的环境温湿度,不是气流。
数据要做图的话,把串口读数接到 ESP32 的网页端或者干脆存进数组定时打印曲线都行。两颗传感器引脚完全兼容,先买 DHT11 练手,不够用再换 DHT22,代码一行不用改,把宏定义换掉就行。











DHT22换DHT11真的只用改宏定义吗,我上次忘了换TYPE,读出来全是NaN,排查了半天。
体感指数这个输出挺好玩,夏天开着看确实和体感对得上。
补充一个:裸版DHT22接5V以后数据脚电平也是5V,ESP32记得分压。
两秒间隔这个坑踩过,读太快全是Failed,新手真得注意。