【Arduino基础教程】DHT11温湿度传感器
2016-06-24 本文已影响4963人
繁著
DHT11温湿度传感器模块
模块简介
- DHT11是用来检测空气温度和湿度的模块。
- 测量范围:
- 湿度: 20% ~ 9 0%(精度5.0%)
- 温度:0 ~ +50 ℃(精度2℃)
- 响应时间:<50ms
- 低功耗、小体积、稳定可靠
接线
DHT11模块接线示意图DHT11 | Arduino | |
---|---|---|
+ | -> | 5V |
OUT | -> | A0 |
- | -> | GND |
加载库文件
到https://arduino-info.wikispaces.com/file/view/DHT-lib.zip/545470280/DHT-lib.zip下载MQ135的库文件,在Arduino IDE中,点击项目
->加载库
->添加一个.zip库
,选择下载好的DHT-lib.zip
文件即可。
示例程序
#include <dht.h>
dht DHT;
#define DHT11_PIN A0
void setup(){
Serial.begin(115200);
Serial.println("DHT TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}
void loop(){
Serial.print("DHT11, \t");
int chk = DHT.read11(DHT11_PIN);
switch (chk){
case DHTLIB_OK:
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default: Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
delay(2000);
}
输出结果
DHT TEST PROGRAM
LIBRARY VERSION: 0.1.20
Type, status, Humidity (%), Temperature (C)
DHT11, OK, 32.0, 29.0
DHT11, OK, 33.0, 28.0
DHT11, OK, 33.0, 28.0
DHT11, OK, 33.0, 28.0 (BREATHE On Sensor ! )
DHT11, OK, 75.0, 30.0
DHT11, OK, 75.0, 29.0
DHT11, OK, 75.0, 29.0