DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器。传感器包括一个电阻式感湿元件和一个NTC测温元件,并与一个8位单片机相连接。温湿度传感器的应用也非常广泛,在室内温度控制、气象监测等领域都有广泛的应用。

实例:可以配合加湿器或除湿器快速搭建一个室内湿度自动调节系统。

dht-11

传感器属性

  • 工作电压:3.5 - 5.5 VDC
  • 湿度范围:20-90%RH (±5%RH)
  • 温度范围:0-50℃ (±2℃)

电路连接

正面从左至右为Pin1-4,Pin2连两次

  • Pin1 → 5V
  • Pin2 → D4
  • Pin2 → 串联5k电阻
  • Pin3 → 悬空
  • Pin4 → GND
dht11-wire

DHT11 Library 下载

DHT11、DHT21、DHT22均可使用

Arudino代码

下载DHT通用库的直接 文件 → 示例 → DHT sensor library → DHTtester

// 各型号DHT温湿度传感器测试脚本
// 
爱鱼客 www.iyu.co
// Written by ladyada, Chinese comments by Simon from www.iyu.co

#include "DHT.h" // 获取DHT通用库

#define DHTPIN 4     // 定义DHT Data Pin所接的GPIO

// 取消对应的温湿度传感器前的注释,默认DHT11
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// 连接 pin 1 (最左侧)到 +5V
// (注意: 如果使用3.3V 逻辑电路,则把 pin 1 连到 3.3V)
// 连接 pin 2 到开发板获取数据引脚,也就是上面定义的 DHTPIN 
// pin 3 悬空不连接
// 连接 pin 4 到 GND
// 在5V和 pin 2 之间连接一个 10K 电阻

// 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("DHT 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("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("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}

其他注意事项

  • 手动焊接,在最高260℃的温度条件下接触时间须少于10秒。
  • 长期保存条件:温度10-40℃,湿度60%以下。

相关文档