回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

【花雕学编程】Arduino动手做(218)---使用 DHT22 传感器读取温湿度数据并计算热指数 简单

头像 驴友花雕 2025.05.08 8 0

0.jpg
03.jpg
04-1.jpg
05.jpg
05-0.jpg
06.jpg

  【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
 实验二百一十八:DHT22 单总线数字温湿度传感器 AM2302电子积木模块
 项目之三:使用 DHT22 传感器读取温湿度数据并计算热指数

实验开源代码

 

代码
/*
  【Arduino】189种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十八:DHT22 单总线数字温湿度传感器 AM2302电子积木模块
  项目之三:使用 DHT22 传感器读取温湿度数据并计算热指数
*/

#include "DHT.h"

#define DHTPIN 2     // 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 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
// 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"));
}

代码解读

这段代码使用 DHT22 传感器 读取 温度和湿度数据,并计算 热指数,核心逻辑如下:

1. 传感器连接
✅ #define DHTPIN 2 → 数据线连接 Arduino 的 D2 引脚 
✅ #define DHTTYPE DHT22 → 指定传感器类型(DHT22,可更换为 DHT11 或 DHT21) 
✅ 供电连接 → 5V 或 3.3V(根据开发板逻辑电压选择) 
✅ 上拉电阻 → 使用 10kΩ 电阻连接数据线与电源,确保信号稳定

2. 初始化
✅ Serial.begin(9600); → 启动串口通信 
✅ dht.begin(); → 初始化 DHT22 传感器

3. 读取温湿度
✅ float h = dht.readHumidity(); → 获取湿度 
✅ float t = dht.readTemperature(); → 获取温度(摄氏度) 
✅ float f = dht.readTemperature(true); → 获取温度(华氏度)

4. 数据处理
✅ 检测读取失败 → isnan(h) || isnan(t) || isnan(f) 确保数据有效 
✅ 计算热指数(体感温度) → dht.computeHeatIndex(t, h, false); 以摄氏度计算 
✅ dht.computeHeatIndex(f, h); 以华氏度计算

5. 输出数据
✅ 湿度 → Serial.print(h); Serial.print("%"); 
✅ 温度(摄氏 & 华氏) → Serial.print(t); Serial.print("°C "); Serial.print(f); Serial.print("°F"); 
✅ 热指数 → Serial.print(hic); Serial.print("°C "); Serial.print(hif); Serial.println("°F");

6. 循环采集
✅ delay(2000); → 每 2 秒采集一次数据,适合定期监测

这段代码让 DHT22 传感器不断测量环境温湿度,并计算体感温度后输出到串口。


实验串口返回情况

 

10-.jpg

09.jpg

评论

user-avatar
icon 他的勋章
    展开更多