使用Arduino与DHT11监测温湿度

这两天开始一一测试之前买过的一些传感器,首先挑选的是DHT11,这个传感器用于粗略估计温湿度。

硬件连接很简单,只需要将DHT11传感器和数字针脚4相连,这里我用到了传感器扩展板,直接连在扩展板上。材料都是用的奥松机器人基地的。

第一件麻烦事儿就是DHT11的库文件,中文材料是木有滴,我到了官网,终于把一个可以用的库文件找出来了。这个库文件还可以测DHT22。如下两个文件,放在DHT文件夹中,然后放到ardunio的库文件夹。

dht.cpp

[cpp]  view plain copy
  1. //  
  2. //    FILE: dht.cpp  
  3. // VERSION: 0.1.01  
  4. // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino  
  5. //  
  6. // DATASHEET:   
  7. //  
  8. // HISTORY:  
  9. // 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011)  
  10. // 0.1.0 by Rob Tillaart (01/04/2011)  
  11. // inspired by DHT11 library  
  12. //  
  13.   
  14. #include "dht.h"  
  15.   
  16. #define TIMEOUT 10000  
  17.   
  18. /  
  19. //  
  20. // PUBLIC  
  21. //  
  22.   
  23.   
  24. // return values:  
  25. //  0 : OK  
  26. // -1 : checksum error  
  27. // -2 : timeout  
  28. int dht::read11(uint8_t pin)  
  29. {  
  30.     // READ VALUES  
  31.     int rv = read(pin);  
  32.     if (rv != 0) return rv;  
  33.   
  34.     // CONVERT AND STORE  
  35.     humidity    = bits[0];  // bit[1] == 0;  
  36.     temperature = bits[2];  // bits[3] == 0;  
  37.   
  38.     // TEST CHECKSUM  
  39.     uint8_t sum = bits[0] + bits[2]; // bits[1] && bits[3] both 0  
  40.     if (bits[4] != sum) return -1;  
  41.   
  42.     return 0;  
  43. }  
  44.   
  45. // return values:  
  46. //  0 : OK  
  47. // -1 : checksum error  
  48. // -2 : timeout  
  49. int dht::read22(uint8_t pin)  
  50. {  
  51.     // READ VALUES  
  52.     int rv = read(pin);  
  53.     if (rv != 0) return rv;  
  54.   
  55.     // CONVERT AND STORE  
  56.     humidity    = word(bits[0], bits[1]) * 0.1;  
  57.   
  58.     int sign = 1;  
  59.     if (bits[2] & 0x80) // negative temperature  
  60.     {  
  61.         bits[2] = bits[2] & 0x7F;  
  62.         sign = -1;  
  63.     }  
  64.     temperature = sign * word(bits[2], bits[3]) * 0.1;  
  65.   
  66.   
  67.     // TEST CHECKSUM  
  68.     uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];  
  69.     if (bits[4] != sum) return -1;  
  70.   
  71.     return 0;  
  72. }  
  73.   
  74. /  
  75. //  
  76. // PRIVATE  
  77. //  
  78.   
  79. // return values:  
  80. //  0 : OK  
  81. // -2 : timeout  
  82. int dht::read(uint8_t pin)  
  83. {  
  84.     // INIT BUFFERVAR TO RECEIVE DATA  
  85.     uint8_t cnt = 7;  
  86.     uint8_t idx = 0;  
  87.   
  88.     // EMPTY BUFFER  
  89.     for (int i=0; i< 5; i++) bits[i] = 0;  
  90.   
  91.     // REQUEST SAMPLE  
  92.     pinMode(pin, OUTPUT);  
  93.     digitalWrite(pin, LOW);  
  94.     delay(20);  
  95.     digitalWrite(pin, HIGH);  
  96.     delayMicroseconds(40);  
  97.     pinMode(pin, INPUT);  
  98.   
  99.     // GET ACKNOWLEDGE or TIMEOUT  
  100.     unsigned int loopCnt = TIMEOUT;  
  101.     while(digitalRead(pin) == LOW)  
  102.         if (loopCnt-- == 0) return -2;  
  103.   
  104.     loopCnt = TIMEOUT;  
  105.     while(digitalRead(pin) == HIGH)  
  106.         if (loopCnt-- == 0) return -2;  
  107.   
  108.     // READ THE OUTPUT - 40 BITS => 5 BYTES  
  109.     for (int i=0; i<40; i++)  
  110.     {  
  111.         loopCnt = TIMEOUT;  
  112.         while(digitalRead(pin) == LOW)  
  113.             if (loopCnt-- == 0) return -2;  
  114.   
  115.         unsigned long t = micros();  
  116.   
  117.         loopCnt = TIMEOUT;  
  118.         while(digitalRead(pin) == HIGH)  
  119.             if (loopCnt-- == 0) return -2;  
  120.   
  121.         if ((micros() - t) > 40) bits[idx] |= (1 << cnt);  
  122.         if (cnt == 0)   // next byte?  
  123.         {  
  124.             cnt = 7;     
  125.             idx++;        
  126.         }  
  127.         else cnt--;  
  128.     }  
  129.   
  130.     return 0;  
  131. }  
  132. //  
  133. // END OF FILE  
  134. //  

dht.h

[cpp]  view plain copy
  1. //   
  2. //    FILE: dht.h  
  3. // VERSION: 0.1.01  
  4. // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino  
  5. //  
  6. //     URL: http://arduino.cc/playground/Main/DHTLib  
  7. //  
  8. // HISTORY:  
  9. // see dht.cpp file  
  10. //   
  11.   
  12. #ifndef dht_h  
  13. #define dht_h  
  14.   
  15. #if ARDUINO < 100  
  16. #include <WProgram.h>  
  17. #else  
  18. #include <Arduino.h>  
  19. #endif  
  20.   
  21. #define DHT_LIB_VERSION "0.1.01"  
  22.   
  23. class dht  
  24. {  
  25. public:  
  26.     int read11(uint8_t pin);  
  27.     int read22(uint8_t pin);  
  28.     double humidity;  
  29.     double temperature;  
  30.   
  31. private:  
  32.     uint8_t bits[5];  // buffer to receive data  
  33.     int read(uint8_t pin);  
  34. };  
  35. #endif  
  36. //  
  37. // END OF FILE  
  38. //  


库文件搞定之后,可以开始写ardunio程序了。这里因为只有DHT11,所以程序就不去测试22了。引入dht的库,然后编写如下代码:

[cpp]  view plain copy
  1. //   
  2. //   FILE:  dht_test.pde  
  3. // PURPOSE: DHT library test sketch for Arduino  
  4. //  
  5.   
  6. #include <dht.h>  
  7.   
  8. dht DHT;  
  9.   
  10. #define DHT11_PIN 4//put the sensor in the digital pin 4  
  11.   
  12.   
  13. void setup()  
  14. {  
  15.   Serial.begin(115200);  
  16.   Serial.println("DHT TEST PROGRAM ");  
  17.   Serial.print("LIBRARY VERSION: ");  
  18.   Serial.println(DHT_LIB_VERSION);  
  19.   Serial.println();  
  20.   Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");  
  21. }  
  22.   
  23. void loop()  
  24. {  
  25.   
  26.   // READ DATA  
  27.   Serial.print("DHT11, \t");  
  28.  int chk = DHT.read11(DHT11_PIN);  
  29.   switch (chk)  
  30.   {  
  31.     case 0:  Serial.print("OK,\t"); break;  
  32.     case -1: Serial.print("Checksum error,\t"); break;  
  33.     case -2: Serial.print("Time out error,\t"); break;  
  34.     default: Serial.print("Unknown error,\t"); break;  
  35.   }  
  36.  // DISPLAT DATA  
  37.   Serial.print(DHT.humidity,1);  
  38.   Serial.print(",\t");  
  39.   Serial.println(DHT.temperature,1);  
  40.   
  41.   delay(1000);  
  42. }  
  43. //  
  44. // END OF FILE  
  45. //  


如果在控制台,出现了time out error,那么就是没读到数据,可能是引脚接错了。记得,我现在接的是数字引脚4。结果:

 

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值