DH11數字溫濕度傳感器是一種集溫度、濕度一體的復合傳感器,它能把溫度和濕度物理量通過溫、濕度敏感元件和相應電路轉化成方便計算機、PLC、智能儀表等數據采集設備直接讀取的數字量。DHT11由電阻式感濕器件和NTC系數感溫器件構成,具有校准數字信號輸出功能,采用單總線串行接口,輸出數據一共5個字節,分別表示:濕度整數位、濕度小數位,溫度整數位、溫度小數位及校驗和,其中檢驗和為濕度與溫度之和的最低八位數據。
arduino引腳 |
模塊引腳 |
D2 |
S |
5V |
VCC |
GND |
GND |
1 double Fahrenheit(double celsius) 2 { 3 return 1.8 * celsius + 32; 4 } //攝氏溫度度轉化為華氏溫度 5 6 double Kelvin(double celsius) 7 { 8 return celsius + 273.15; 9 } //攝氏溫度轉化為開氏溫度 10 11 // 露點(點在此溫度時,空氣飽和並產生露珠) 12 // 參考: http://wahiduddin.net/calc/density_algorithms.htm 13 double dewPoint(double celsius, double humidity) 14 { 15 double A0= 373.15/(273.15 + celsius); 16 double SUM = -7.90298 * (A0-1); 17 SUM += 5.02808 * log10(A0); 18 SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; 19 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; 20 SUM += log10(1013.246); 21 double VP = pow(10, SUM-3) * humidity; 22 double T = log(VP/0.61078); // temp var 23 return (241.88 * T) / (17.558-T); 24 } 25 26 // 快速計算露點,速度是5倍dewPoint() 27 // 參考: http://en.wikipedia.org/wiki/Dew_point 28 double dewPointFast(double celsius, double humidity) 29 { 30 double a = 17.271; 31 double b = 237.7; 32 double temp = (a * celsius) / (b + celsius) + log(humidity/100); 33 double Td = (b * temp) / (a - temp); 34 return Td; 35 } 36 37 #include <dht11.h> 38 39 dht11 DHT11; 40 41 #define DHT11PIN 2 42 43 void setup() 44 { 45 Serial.begin(9600); 46 Serial.println("DHT11 TEST PROGRAM "); 47 Serial.print("LIBRARY VERSION: "); 48 Serial.println(DHT11LIB_VERSION); 49 Serial.println(); 50 } 51 52 void loop() 53 { 54 Serial.println("\n"); 55 56 int chk = DHT11.read(DHT11PIN); 57 58 Serial.print("Read sensor: "); 59 switch (chk) 60 { 61 case DHTLIB_OK: 62 Serial.println("OK"); 63 break; 64 case DHTLIB_ERROR_CHECKSUM: 65 Serial.println("Checksum error"); 66 break; 67 case DHTLIB_ERROR_TIMEOUT: 68 Serial.println("Time out error"); 69 break; 70 default: 71 Serial.println("Unknown error"); 72 break; 73 } 74 75 Serial.print("Humidity (%): "); 76 Serial.println((float)DHT11.humidity, 2); 77 78 Serial.print("Temperature (oC): "); 79 Serial.println((float)DHT11.temperature, 2); 80 81 Serial.print("Temperature (oF): "); 82 Serial.println(Fahrenheit(DHT11.temperature), 2); 83 84 Serial.print("Temperature (K): "); 85 Serial.println(Kelvin(DHT11.temperature), 2); 86 87 Serial.print("Dew Point (oC): "); 88 Serial.println(dewPoint(DHT11.temperature, DHT11.humidity)); 89 90 Serial.print("Dew PointFast (oC): "); 91 Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity)); 92 93 delay(2000); 94 }