温湿度传感器库DHT11/DHT22-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 27243|回复: 14

温湿度传感器库DHT11/DHT22

[复制链接]
发表于 2018-9-2 10:14 | 显示全部楼层 |阅读模式
[md]
# arduino温湿度传感器库 DHT11/DHT22
DHT-sensor-library-master.zip (11.7 KB, 下载次数: 3493)

#### 支持的传感器
DHT11 / DHT22 / DHT21 / AM2301  
#### github
https://github.com/adafruit/DHT-sensor-library

请下载上面的附件,如果你自己从github下载,那还要装Adafruit Unified Sensor Library!https://github.com/adafruit/Adafruit_Sensor


#### 示例程序
```
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN D7     // what digital pin we're connected to

// 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("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("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");
}

```
[/md]







相关帖子

发表于 2019-1-24 12:36 | 显示全部楼层
楼主,这个库,我单独编译可以过,但是复制代码到IDE就会报错!如图,求解!!!
dhtbaocuo.jpg
发表于 2019-1-24 18:30 | 显示全部楼层
现在是最新的库了,还是不行!
guzhantu.png
发表于 2019-3-3 16:12 | 显示全部楼层
不能编译通过
发表于 2019-4-20 11:46 | 显示全部楼层
把这个压缩包放到哪个位置?
发表于 2019-4-29 23:52 | 显示全部楼层
fy88 发表于 2019-4-20 11:46
把这个压缩包放到哪个位置?

解压缩后,放到 Arduino>libraries 文件夹中
发表于 2019-5-8 21:03 | 显示全部楼层
那个附带的头文件编译会出错
sketch\DHT.cpp: In member function 'float DHT::readTemperature(bool, bool)':

DHT.cpp:40:13: error: 'NAN' was not declared in this scope

sketch\DHT.cpp: In member function 'float DHT::readHumidity(bool)':

DHT.cpp:89:13: error: 'NAN' was not declared in this scope

exit status 1
'NAN' was not declared in this scope
 楼主| 发表于 2019-5-9 09:43 | 显示全部楼层
家松大王 发表于 2019-5-8 21:03
那个附带的头文件编译会出错
sketch\DHT.cpp: In member function 'float DHT::readTemperature(bool, boo ...

可能和你的环境版本有关,建议使用最新的IDE测试
发表于 2021-2-1 04:24 | 显示全部楼层
Arduino:1.8.12 (Windows 10), 开发板:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"

"DHT.h" 对应多个库
In file included from C:\Users\123\Documents\Arduino\libraries\DHT_sensor_library\DHT_U.cpp:15:0:

已使用: C:\Users\123\Documents\Arduino\libraries\DHT_sensor_library
C:\Users\123\Documents\Arduino\libraries\DHT_sensor_library\DHT_U.h:36:29: fatal error: Adafruit_Sensor.h: No such file or directory

#include <Adafruit_Sensor.h>

                             ^

compilation terminated.

未使用:C:\Users\123\Documents\Arduino\libraries\Grove_Temperature_And_Humidity_Sensor
未使用:D:\arduino\libraries\DHT-sensor-library-master
exit status 1
为开发板 NodeMCU 1.0 (ESP-12E Module) 编译时出错。

在文件 -> 首选项开启
“编译过程中显示详细输出”选项
这份报告会包含更多信息。
 楼主| 发表于 2021-2-2 15:16 | 显示全部楼层
烟花易冷 发表于 2021-2-1 04:24
Arduino:1.8.12 (Windows 10), 开发板:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can r ...

请看一楼,有写
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 09:43 , Processed in 0.205107 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表