Arduino 記事本 09 : DHT11溫濕度感知模組
網頁筆記本-009
Arduino 記事本 09 : DHT11溫濕度感知模組
原廠網頁:https://learn.adafruit.com/dht/overviewLIB下載網頁:https://learn.adafruit.com/dht/downloads
下載兩個LIB資料
- Arduino library and example code for DHT sensors
- Adafruit_Sensor library (required by the DHT library above)
下載回來解壓縮後將資料夾放入:Arduino\libraries\位置下
[Adafruit_Sensor-master]
[DHT-sensor-library-master]
重啟 Arduino,可於範例程式中找到DHTtester的程式範例
DHT11溫濕度感知模組
// 刪去註解
#include "DHT.h"
#define DHTPIN 2 // 選擇Arduino輸入接腳
#define DHTTYPE DHT11 // DHT 11 , 選擇選用的感知器
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
測試結果:
留言
張貼留言