风筝
发表于: 2022-7-5 17:10:17 | 显示全部楼层

使用奥松公司的DHT11或DHT22数字温湿度传感器,让您的下一个Arduino项目能够感知周围的世界。


这些传感器经过预先校准,不需要额外的组件,因此您可以立即开始测量相对湿度和温度。


它们提供的最大功能之一是温度和湿度都测量到最接近的十分之一,也就是小数点后一位。该传感器的唯一缺点是您只能每隔一两秒从中获取一次新数据。


DHT11与DHT22/AM2302

我们有两个版本的DHTxx传感器系列。它们看起来有点相似,具有相同的引脚排列,但具有不同的特性。以下是详细信息:


DHT22是更昂贵的版本,显然具有更好的规格。其温度测量范围为-40°C至+125°C,精度为+-0.5 度,而DHT11测量温度范围为 0°C到50°C,精度为+-2 度。此外,DHT22传感器具有更好的湿度测量范围,从0到 100%,精度为2-5%,而 DHT11 湿度范围从20%到80%,精度为5%。

DHT11
DHT22
工作电压
3V - 5V
3V - 5V
最大工作电流
2.5mA
2.5mA
湿度范围
20-80% / 5%
0-100% / 2-5%
温度范围
0-50°C / ± 2°C
-40-80°C / ± 0.5°C
采样率
1 Hz(每秒读取一次)
0.5 Hz(每 2 秒读取一次)
外形尺寸
15.5mm x 12mm x 5.5mm
15.1mm x 25mm x 7.7mm
优势
超低成本
更准确

虽然DHT22更精确,工作温度和湿度范围更大; 但DHT11更便宜,尺寸更小,采样率更高。 DHT11的采样率为 1Hz,即每秒读取一次,而DHT22的采样率为0.5Hz,即每两秒读取一次。


两个传感器的工作电压为3到5 伏,而转换期间(请求数据时)使用的最大电流为2.5毫安。最好的一点是DHT11和DHT22传感器是可互换的——这意味着,如果你用一个来构建你的项目,你可以拔掉它并使用另一个。您的代码可能需要稍作调整,但至少接线是相同的!


硬件概述

现在让我们继续讨论有趣的事情。让我们拆解DHT11和DHT22传感器,看看里面有什么。


外壳分为两部分,因此要进入它只需拿一把锋利的刀并将外壳分开即可。在外壳内部,在感应侧,有一个湿度感应组件以及一个NTC温度传感器(或热敏电阻)

Inside-DHT11-DHT22-AM2302-Temperature-Humidity-Sensor.jpg


湿度传感组件当然是用来测量湿度的,它有两个电极和夹在它们之间的水分保持基板(通常是盐或导电塑料聚合物)。当水蒸气被基板吸收时,离子被基板释放,这反过来又增加了电极之间的电导率。两个电极之间的电阻变化与相对湿度成正比。较高的相对湿度会降低电极之间的电阻,而较低的相对湿度会增加电极之间的电阻。

Internal-Structure-of-Humidity-Sensor-in-DHT11-DHT22.png


此外,它们还包括一个NTC温度传感器/热敏电阻来测量温度。热敏电阻是一种随温度改变其电阻的电阻。从技术上讲,所有电阻器都是热敏电阻——它们的电阻会随着温度的变化而略有变化——但这种变化通常非常非常小并且难以测量。


热敏电阻的制作使得电阻随温度发生剧烈变化,因此每度可以有100欧姆或更多的变化!术语“NTC”的意思是“负温度系数(Negative Temperature Coefficient)”,这意味着电阻随着温度的升高而降低。

NTC-Thermistor-Temperature-Resistance-Characteristic-Curve.png


在另一边,有一块带有8位SOIC-14封装IC的小PCB。该IC使用存储的校准系数测量和处理模拟信号,进行模数转换并输出具有温度和湿度的数字信号。


DHT11和DHT22引脚排列

DHT11和DHT22传感器相当容易连接。它们有四个引脚:

DHT11-DHT22-AM2302-Temperature-Humidity-Sensor-Pinout.jpg


VCC 引脚为传感器供电。虽然电源电压范围为3.3V至5.5V,但建议使用5V电源。在5V供电的情况下,传感器可以保持20米的距离。但是,使用3.3V电源电压时,电缆长度不得超过1米。否则,线路电压降会导致测量误差。

Data 引脚用于传感器和微控制器之间的通信。

NC 未连接

GND 连接到Arduino开发板的地。


将DHT11和DHT22连接到Arduino UNO

现在我们已经完全了解了DHT传感器的工作原理,我们可以开始将它连接到Arduino!


幸运的是,将DHT11或DHT22传感器连接到Arduino很简单。它们使用2.54mm间距的排针,因此您可以轻松地将它们插入任何面包板。用5V为传感器供电并将GND连接到地线。最后,将Data引脚连接到数字引脚#2。


我们还需要在VCC和Data线之间放置一个10KΩ的上拉电阻,以保持它为高电平,以便传感器和MCU之间的正确通信。如果您碰巧有传感器的分线板,则无需添加任何外部上拉电阻。它带有一个内置的上拉电阻。

Arduino-Wiring-Fritzing-Connections-with-DHT11.jpg


Arduino-Wiring-Fritzing-Connections-with-DHT22.jpg


硬件连接完成后,你现在可以上传一些代码并让它工作了。

跳转到指定楼层
风筝
发表于: 2022-7-5 17:21:01 | 显示全部楼层

Arduino代码 – 在串口监视器上打印值

DHT11和DHT22传感器有自己的单线协议,用于传输数据。 该协议需要精确的时间。 幸运的是,我们不必为此担心太多,因为我们将使用DHT库来处理几乎所有事情。


首先通过访问GitHub存储库下载库,或者只需单击此按钮即可下载zip: DHTlib-0.1.35.zip (20.1 KB, 下载次数: 8)


要进行安装,请打开Arduino IDE,转到 Sketch > Include Library > Add .ZIP Library,然后选择刚刚下载的 DHTlib ZIP 文件。 如果您需要有关安装库的更多详细信息,请访问此安装 Arduino 库教程


安装库后,您可以将此草图复制到Arduino IDE 中。 以下测试草图将在串口监视器上打印温度和相对湿度值。

  1. #include <dht.h>
  2. #define dataPin 8 // Defines pin number to which the sensor is connected
  3. dht DHT; // Creats a DHT object

  4. void setup()
  5. {
  6.         Serial.begin(9600);
  7. }
  8. void loop()
  9. {
  10.         //Uncomment whatever type you're using!
  11.         int readData = DHT.read22(dataPin); // DHT22/AM2302
  12.         //int readData = DHT.read11(dataPin); // DHT11

  13.         float t = DHT.temperature; // Gets the values of the temperature
  14.         float h = DHT.humidity; // Gets the values of the humidity

  15.         // Printing the results on the serial monitor
  16.         Serial.print("Temperature = ");
  17.         Serial.print(t);
  18.         Serial.print(" ");
  19.         Serial.print((char)176);//shows degrees character
  20.         Serial.print("C | ");
  21.         Serial.print((t * 9.0) / 5.0 + 32.0);//print the temperature in Fahrenheit
  22.         Serial.print(" ");
  23.         Serial.print((char)176);//shows degrees character
  24.         Serial.println("F ");
  25.         Serial.print("Humidity = ");
  26.         Serial.print(h);
  27.         Serial.println(" % ");
  28.         Serial.println("");

  29.         delay(2000); // Delays 2 secods
  30. }
复制代码

上传草图后,打开串口监视器窗口并查看Arduino的输出。

DHT11-DHT22-AM2302-Sensor-DHTlib-library-Output-on-Serial-Monitor.png


代码说明

草图代码首先包含DHT库。接下来,我们需要定义传感器的数据引脚连接到的Arduino引脚号,并创建一个DHT对象。这样,我们可以访问与库相关的特殊函数。

  1. #include <dht.h>
  2. #define dataPin 8 // Defines pin number to which the sensor is connected
  3. dht DHT; // Creats a DHT object
复制代码

setup()函数中,我们需要启动串口通信,因为我们将使用串口监视器打印结果。

  1. void setup() {
  2.   Serial.begin(9600);
  3. }
复制代码

loop()函数中;我们将使用read22() 函数从DHT22读取数据。它将传感器的数据引脚号作为参数。如果您使用的是DHT11,则需要使用read11() 函数。您可以通过取消注释第二行来做到这一点。

  1. //Uncomment whatever type you're using!
  2. int readData = DHT.read22(dataPin); // DHT22/AM2302
  3. //int readData = DHT.read11(dataPin); // DHT11
复制代码

计算出湿度和温度值后,我们可以通过以下方式访问它们:

  1. float t = DHT.temperature; // Gets the values of the temperature
  2. float h = DHT.humidity; // Gets the values of the humidity
复制代码

DHT对象以摄氏 (°C) 为单位返回温度值。可以使用一个简单的公式将其转换为华氏 (°F):

T(°F) = T(°C) × 9/5 + 32

  1. //print the temperature in Fahrenheit
  2. Serial.print((t * 9.0) / 5.0 + 32.0);
复制代码

回复

使用道具 举报

风筝
发表于: 2022-7-5 17:30:07 | 显示全部楼层

Arduino代码 – 在LCD显示DHT11和DHT22

有时您会想出一个主意,在哪里监控自制的培养箱中的温度和湿度值。那么您可能需要1602字符LCD来显示孵化器中的主要环境,而不是串口监视器。因此,在本例中,我们将LCD与DHT11和DHT22传感器一起连接到Arduino开发板。


接下来,我们需要如下所示连接LCD显示屏。

Arduino-Wiring-Fritzing-Connections-with-DHT11-and-16x2-Character-LCD.jpg


Arduino-Wiring-Fritzing-Connections-with-DHT22-and-16x2-Character-LCD.jpg


以下草图将在1602字符LCD上打印温度和相对湿度值。除了我们在LCD上打印值外,它与上面的示例使用相同的代码。

  1. #include <LiquidCrystal.h> // includes the LiquidCrystal Library
  2. #include <dht.h>
  3. #define dataPin 8

  4. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
  5. dht DHT;
  6. bool showcelciusorfarenheit = false;

  7. void setup()
  8. {
  9.         lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
  10. }

  11. void loop()
  12. {
  13.         int readData = DHT.read22(dataPin);
  14.         float t = DHT.temperature;
  15.         float h = DHT.humidity;
  16.         lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
  17.         lcd.print("Temp.: "); // Prints string "Temp." on the LCD

  18.         //Print temperature value in Celcius and Fahrenheit every alternate cycle
  19.         if(showcelciusorfarenheit)
  20.         {
  21.                 lcd.print(t); // Prints the temperature value from the sensor
  22.                 lcd.print(" ");
  23.                 lcd.print((char)223);//shows degrees character
  24.                 lcd.print("C");
  25.                 showcelciusorfarenheit = false;
  26.         }
  27.         else
  28.         {
  29.                 lcd.print((t * 9.0) / 5.0 + 32.0); // print the temperature in Fahrenheit
  30.                 lcd.print(" ");
  31.                 lcd.print((char)223);//shows degrees character
  32.                 lcd.print("F");
  33.                 showcelciusorfarenheit = true;
  34.         }
  35.         
  36.         lcd.setCursor(0,1);
  37.         lcd.print("Humi.: ");
  38.         lcd.print(h);
  39.         lcd.print(" %");
  40.         delay(5000);
  41. }
复制代码

最终效果如下:

DHT11-DHT22-Arduino-Sketch-Temperature-Humidity-Measurements-Output-on-LCD.jpg

回复

使用道具 举报

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

本版积分规则

主题 700 | 回复: 1480



手机版|

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

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

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