风筝
发表于: 2018-11-23 23:16:53 | 显示全部楼层

在本篇文章中,我们将学习如何基于Arduino开发板使用DHT11或DHT22传感器测量温度和湿度。这两个传感器在电子爱好者中非常受欢迎,因为它们价格便宜,并且提供了出色的性能。以下是这两个传感器之间的主要参数及不同之处:

相对来说,DHT22价格略微贵一些,因为它具有更好的规参数。其温度测量范围为-40〜125度,精度±0.5度,而DHT11温度范围为0〜50度,精度+ -2度。 DHT22传感器也有更好湿度测量范围,可以测量从0至100%,精度达到25%,而DHT11测量的湿度范围为20〜80%,5%的精度。

DHT11-vs-DHT22-specifications-parameters.png


其中有两个参数,DHT11显得比DHT22更好一些。其中一个是采样率,DHT11为1Hz,也就是每秒读取一次数据,而DHT22的采样率是0.5Hz,也就是每两秒读取一次数据。除此之外,DHT11还具有较小的尺寸。两个传感器的工作电压都是3至5V,同时测量时使用的最大电流为2.5毫安。


本篇文章所需的组件如下所示:

●    DHT11传感器

●    DHT22传感器

●    Arduino开发板

●    面包板和跳线


DHT11 / DHT22工作原理

好了,现在让我们来看看这些传感器是如何工作的。它们由湿度感测组件、NTC温度传感器(或热敏电阻)以及传感器背面的IC组成。

DHT11-DDHT22-Working-Principle.png

它们使用湿度感测部件测量湿度,这些部件有两个电极,它们之间有保湿基质。因此,随着湿度变化,基质的导电率发生改变,或者说电极之间的电阻发生改变。电阻的这种变化被IC测量和处理,这样就准备好由微控制器读取。

Humidity-Sensor-Working-Principle.jpg

另一方面,这些传感器使用NTC温度传感器或热敏电阻来测量温度。


热敏电阻实际上是一个可变电阻,其阻值随温度的变化而变化。这些传感器由半导体材料如陶瓷或聚合物的烧结制成,以在较小的温度变化时可以提供较大变化的阻值。术语“NTC”表示“负温度系数”,这意味着该电阻随着温度的增加而降低。

Thermistor-Working-Principle.jpg


电路原理图

DHTxx传感器有四个引脚,VCC、GND、DATA以及未使用的引脚。一个5K至10K的上拉电阻是必需的,以保持数据线为高电平,并且为了使能传感器和Arduino开发板板之间的通信。传感器的一些版本中内置了上拉电阻,并且仅有3个引脚。

DHT22-DHT11-Circuit-Schematics.png


DHTXX传感器具有用于传送数据自己的单线协议。此协议需要精确的时序,用于获取来自传感器的数据的时序图可以从传感器的数据表中找到。不过,我们不必担心太多关于这些时序图,因为我们将使用DHT库完成所有的操作。


源代码

首先,我们需要包含DHT库,该库可从Arduino的官方网站上找到,然后定义传感器所连接的引脚,并创建一个DHT对象。在setup()函数中,我们需要启动串行通讯,因为我们将使用串口监视器打印结果。使用read22()函数,我们将读取来自传感器的数据,并把温度值和湿度值保存到变量T和H中。如果您使用的是DHT11传感器,你需要使用read11()函数。最后,我们将串口监视器上打印温度值和湿度值。

  1. /*  DHT11/ DHT22 Sensor Temperature and Humidity Tutorial
  2. *  Program made by Dejan Nedelkovski,
  3. *  www.HowToMechatronics.com
  4. */
  5. /*
  6. * You can find the DHT Library from Arduino official website
  7. * https://playground.arduino.cc/Main/DHTLib
  8. */

  9. #include <dht.h>
  10. #define dataPin 8 // Defines pin number to which the sensor is connected
  11. dht DHT; // Creats a DHT object
  12. void setup() {
  13.   Serial.begin(9600);
  14. }
  15. void loop() {
  16.   int readData = DHT.read22(dataPin); // Reads the data from the sensor
  17.   float t = DHT.temperature; // Gets the values of the temperature
  18.   float h = DHT.humidity; // Gets the values of the humidity
  19.   
  20.   // Printing the results on the serial monitor
  21.   Serial.print("Temperature = ");
  22.   Serial.print(t);
  23.   Serial.print(" *C ");
  24.   Serial.print("    Humidity = ");
  25.   Serial.print(h);
  26.   Serial.println(" % ");
  27.   
  28.   delay(2000); // Delays 2 secods, as the DHT22 sampling rate is 0.5Hz
  29. }
复制代码

我们将以上代码上传到Arduino开发板,然后就可以在串口显示器上看到传感器的温度值和湿度值。


我还制作了另外一个示例,在LCD上显示这些结果。 以下是该示例的源代码:

  1. /*  DHT11/ DHT22 Sensor Temperature and Humidity Tutorial
  2. *  Program made by Dejan Nedelkovski,
  3. *  www.HowToMechatronics.com
  4. */
  5. /*
  6. * You can find the DHT Library from Arduino official website
  7. * https://playground.arduino.cc/Main/DHTLib
  8. */
  9. #include <LiquidCrystal.h> // includes the LiquidCrystal Library
  10. #include <dht.h>
  11. #define dataPin 8
  12. LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
  13. dht DHT;
  14. void setup() {
  15.   lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
  16. }
  17. void loop() {
  18.   int readData = DHT.read22(dataPin);
  19.   float t = DHT.temperature;
  20.   float h = DHT.humidity;
  21.   lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
  22.   lcd.print("Temp.: "); // Prints string "Temp." on the LCD
  23.   lcd.print(t); // Prints the temperature value from the sensor
  24.   lcd.print(" C");
  25.   lcd.setCursor(0,1);
  26.   lcd.print("Humi.: ");
  27.   lcd.print(h);
  28.   lcd.print(" %");
  29.   delay(2000);
  30. }
复制代码

以上就是本篇文章的全部内容。如果遇到任何问题,请随时在本帖下面进行回复,我会及时为您解答。

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-26 15:22 , Processed in 0.074590 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

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

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