使用Arduino与DHT11监测温湿度

【原文】http://blog.csdn.net/micaroo/article/details/7239294


这两天开始一一测试之前买过的一些传感器,首先挑选的是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:DHTTemperature&HumiditySensorlibraryforArduino
  5. //
  6. //DATASHEET:
  7. //
  8. //HISTORY:
  9. //0.1.01addedsupportforArduino1.0,fixedtypos(31/12/2011)
  10. //0.1.0byRobTillaart(01/04/2011)
  11. //inspiredbyDHT11library
  12. //
  13. #include"dht.h"
  14. #defineTIMEOUT10000
  15. /
  16. //
  17. //PUBLIC
  18. //
  19. //returnvalues:
  20. //0:OK
  21. //-1:checksumerror
  22. //-2:timeout
  23. intdht::read11(uint8_tpin)
  24. {
  25. //READVALUES
  26. intrv=read(pin);
  27. if(rv!=0)returnrv;
  28. //CONVERTANDSTORE
  29. humidity=bits[0];//bit[1]==0;
  30. temperature=bits[2];//bits[3]==0;
  31. //TESTCHECKSUM
  32. uint8_tsum=bits[0]+bits[2];//bits[1]&&bits[3]both0
  33. if(bits[4]!=sum)return-1;
  34. return0;
  35. }
  36. //returnvalues:
  37. //0:OK
  38. //-1:checksumerror
  39. //-2:timeout
  40. intdht::read22(uint8_tpin)
  41. {
  42. //READVALUES
  43. intrv=read(pin);
  44. if(rv!=0)returnrv;
  45. //CONVERTANDSTORE
  46. humidity=word(bits[0],bits[1])*0.1;
  47. intsign=1;
  48. if(bits[2]&0x80)//negativetemperature
  49. {
  50. bits[2]=bits[2]&0x7F;
  51. sign=-1;
  52. }
  53. temperature=sign*word(bits[2],bits[3])*0.1;
  54. //TESTCHECKSUM
  55. uint8_tsum=bits[0]+bits[1]+bits[2]+bits[3];
  56. if(bits[4]!=sum)return-1;
  57. return0;
  58. }
  59. /
  60. //
  61. //PRIVATE
  62. //
  63. //returnvalues:
  64. //0:OK
  65. //-2:timeout
  66. intdht::read(uint8_tpin)
  67. {
  68. //INITBUFFERVARTORECEIVEDATA
  69. uint8_tcnt=7;
  70. uint8_tidx=0;
  71. //EMPTYBUFFER
  72. for(inti=0;i<5;i++)bits[i]=0;
  73. //REQUESTSAMPLE
  74. pinMode(pin,OUTPUT);
  75. digitalWrite(pin,LOW);
  76. delay(20);
  77. digitalWrite(pin,HIGH);
  78. delayMicroseconds(40);
  79. pinMode(pin,INPUT);
  80. //GETACKNOWLEDGEorTIMEOUT
  81. unsignedintloopCnt=TIMEOUT;
  82. while(digitalRead(pin)==LOW)
  83. if(loopCnt--==0)return-2;
  84. loopCnt=TIMEOUT;
  85. while(digitalRead(pin)==HIGH)
  86. if(loopCnt--==0)return-2;
  87. //READTHEOUTPUT-40BITS=>5BYTES
  88. for(inti=0;i<40;i++)
  89. {
  90. loopCnt=TIMEOUT;
  91. while(digitalRead(pin)==LOW)
  92. if(loopCnt--==0)return-2;
  93. unsignedlongt=micros();
  94. loopCnt=TIMEOUT;
  95. while(digitalRead(pin)==HIGH)
  96. if(loopCnt--==0)return-2;
  97. if((micros()-t)>40)bits[idx]|=(1<
  98. if(cnt==0)//nextbyte?
  99. {
  100. cnt=7;
  101. idx++;
  102. }
  103. elsecnt--;
  104. }
  105. return0;
  106. }
  107. //
  108. //ENDOFFILE
  109. //

dht.h

[cpp] view plain copy
  1. //
  2. //FILE:dht.h
  3. //VERSION:0.1.01
  4. //PURPOSE:DHTTemperature&HumiditySensorlibraryforArduino
  5. //
  6. //URL:http://arduino.cc/playground/Main/DHTLib
  7. //
  8. //HISTORY:
  9. //seedht.cppfile
  10. //
  11. #ifndefdht_h
  12. #definedht_h
  13. #ifARDUINO<100
  14. #include
  15. #else
  16. #include
  17. #endif
  18. #defineDHT_LIB_VERSION"0.1.01"
  19. classdht
  20. {
  21. public:
  22. intread11(uint8_tpin);
  23. intread22(uint8_tpin);
  24. doublehumidity;
  25. doubletemperature;
  26. private:
  27. uint8_tbits[5];//buffertoreceivedata
  28. intread(uint8_tpin);
  29. };
  30. #endif
  31. //
  32. //ENDOFFILE
  33. //


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

[cpp] view plain copy
  1. //
  2. //FILE:dht_test.pde
  3. //PURPOSE:DHTlibrarytestsketchforArduino
  4. //
  5. #include
  6. dhtDHT;
  7. #defineDHT11_PIN4//putthesensorinthedigitalpin4
  8. voidsetup()
  9. {
  10. Serial.begin(115200);
  11. Serial.println("DHTTESTPROGRAM");
  12. Serial.print("LIBRARYVERSION:");
  13. Serial.println(DHT_LIB_VERSION);
  14. Serial.println();
  15. Serial.println("Type,\tstatus,\tHumidity(%),\tTemperature(C)");
  16. }
  17. voidloop()
  18. {
  19. //READDATA
  20. Serial.print("DHT11,\t");
  21. intchk=DHT.read11(DHT11_PIN);
  22. switch(chk)
  23. {
  24. case0:Serial.print("OK,\t");break;
  25. case-1:Serial.print("Checksumerror,\t");break;
  26. case-2:Serial.print("Timeouterror,\t");break;
  27. default:Serial.print("Unknownerror,\t");break;
  28. }
  29. //DISPLATDATA
  30. Serial.print(DHT.humidity,1);
  31. Serial.print(",\t");
  32. Serial.println(DHT.temperature,1);
  33. delay(1000);
  34. }
  35. //
  36. //ENDOFFILE
  37. //


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


你可能感兴趣的:(使用Arduino与DHT11监测温湿度)