风筝
发表于: 2019-8-20 22:41:14 | 显示全部楼层

在本篇文章中,您将学习如何设置DHT11和DHT22传感器,并测量环境温度和湿度。


DHT11和DHT22温湿度传感器

在许多项目中,由于温度和湿度等参数的重要性,正确选择能够测量温度和湿度的传感器非常重要。DHT11和DHT22的DHT系列是温度和湿度传感器中最受欢迎和最常见的传感器。


您可以在下表中看到它们的功能:

参数 DHT11 DHT22
工作电压 3-5.5 v 3.3-6 v
电流 2.5 mA 1.5 mA
温度测量范围  0 - 50摄氏度 -40 - 80摄氏度
温度测量精度 ±5% ±0.5%
湿度测量范围 20-90%RH 0-100%RH
湿度测量精度 ±2% ±2%
响应速度 2s 2s

价格低是DHT11最重要的特性,但它的精度不高和测量范围窄。而DHT22具有宽范围和高精度测量,但价格比另一个贵2.5倍。


使用DHT11和Arduino测量温度和湿度

所需的材料

●    Arduino Uno R3开发板

●    DHT11传感器

●    线路板

●    跳线

●    Arduino IDE


电路连接

DHT11包含4个引脚。 2个用于供电,1个用于发送数据,另一个为空。要使此传感器正常工作并为您提供正确的数据,必须使用4.7 k电阻上拉数据引脚。将传感器的第三个引脚悬空。

dht_fz.jpg

dht_4.jpg

注意:小心传感器的引脚方向,如果发生错误,传感器有可能损坏。


代码

要开始传输数据,请将此代码上传到Arduino并打开串行监视器。

  1. /*
  2. DHT11 Tempertature and Hummidity
  3. */
  4. #include "dht.h"

  5. conat int dht_pin = 8;

  6. dht DHT;

  7. void setup(){

  8.   Serial.begin(9600);
  9.   delay(500);
  10.   Serial.print("***Electropeak***\n\n");

  11. }

  12. void loop(){

  13.     DHT.read11(dht_pin);
  14.    
  15.     Serial.print("humidity = ");
  16.     Serial.print(DHT.humidity);
  17.     Serial.print("%  ");
  18.     Serial.print("temperature = ");
  19.     Serial.print(DHT.temperature);
  20.     Serial.println("C  ");
  21.    
  22.     delay(3000);//We have to wait at least 2 seconds before accessing the sensor again.
  23. }

  24. }
复制代码

DHT.humidity以百分比形式返回湿度,DHT.temperature以摄氏度返回温度。


注意:我们必须在每次测量之间等待2秒钟。 否则,传感器将返回错误的数据。

dht11_screen.jpg

您可以在液晶显示屏上显示温度和湿度,而无需在计算机上显示信息。


使用DHT22和Arduino测量温度和湿度

设置和使用DHT22传感器几乎与DHT11相同。


所需的材料

●    Arduino Uno R3开发板

●    DHT22传感器

●    线路板

●    跳线

●    Arduino IDE


电路连接

dht22_fz.jpg


代码

唯一的区别在于DHT.read函数,你应该写22而不是11。另外,你可以将温度和湿度参数定义浮点数用于DHT22,以便更准确地看到它们。


上传此代码到Arduino开发板,然后在串行监视器中观察结果。

  1. /*
  2. DHT22 Tempertature and Hummidity
  3. */
  4. #include "dht.h"
  5. const int dataPin = 8;
  6. dht DHT;
  7. void setup() {
  8.   Serial.begin(9600);
  9. }
  10. void loop() {
  11.   int readData = DHT.read22(dataPin);

  12.   float t = DHT.temperature;
  13.   float h = DHT.humidity;
  14.   
  15.   Serial.print("Temperature = ");
  16.   Serial.print(t);
  17.   Serial.print(" *C ");
  18.   Serial.print("    Humidity = ");
  19.   Serial.print(h);
  20.   Serial.println(" % ");
  21.   
  22.   delay(2000);
  23. }
复制代码

dht22_screen.png

代码中使用的DHT库的下载地址:DHT Sensor库


以上就是本文的全部内容。如果遇到问题,请随时在本帖下面进回复。

跳转到指定楼层
azi1974
发表于: 2020-1-6 02:19:09 | 显示全部楼层

怎么显示到液晶显示屏上呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-20 13:55 , Processed in 0.139804 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

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