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(" "); 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 }