使用Arduino與DHT11監測溫溼度

這兩天開始一一測試之前買過的一些傳感器,首先挑選的是DHT11,這個傳感器用於粗略估計溫溼度。

硬件連接很簡單,只需要將DHT11傳感器和數字針腳4相連,這裏我用到了傳感器擴展板,直接連在擴展板上。材料都是用的奧鬆機器人基地的。

第一件麻煩事兒就是DHT11的庫文件,中文材料是木有滴,我到了官網,終於把一個可以用的庫文件找出來了。這個庫文件還可以測DHT22。如下兩個文件,放在DHT文件夾中,然後放到ardunio的庫文件夾。

dht.cpp

  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

  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的庫,然後編寫如下代碼:

  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。結果:

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章