DHT11溫度濕度模塊
此模塊搭配Arduino使用,需先安裝外部程式libraries
方式1,開啟Arduino軟體程式IDE,
在上列點選-草稿碼>匯入>管理程式庫,
輸入DHT11,選擇安裝後離開重新執行Arduino圖↓
方式2,到以下連結網站下載ZIP,
https://github.com/adafruit/DHT-sensor-library
解壓後將整個目錄安裝到你的Arduino位置,
例如Arduino\libraries\DHT_sensor_library圖↓
安裝後建議重新執行Arduino IDE,
在工具列檔案>範例>會出現一個DHT sensor>DHTtester
打開內建範例後查看程式,圖↓
注意此段說明~依DHT的版本選用
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
我們使用DHT11,所以只留下
#define DHTTYPE DHT11
其他版本及備註刪除,成為以下碼,圖↓
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11 // 只留DHT 11
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\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
選擇好Arduino版本及通訊阜上傳到機板,
習慣先離線斷電再裝硬體,
將DHT11模塊裝上硬體,僅3腳,
在程式裡可看到輸入端為p2接模塊OUT,
正負輸入電力3.3~5v,圖↓
連結電腦執行Arduino IDE,
並打開監控窗,確認鮑率是相符的,
模塊就會開始回傳資料,
濕度++溫度++及體感溫度,圖↓
如需要外接螢幕可參考該相關教文搭配使用,
https://ee543.blogspot.com/2016/05/arduino-uno262iici2c.html
沒有留言:
張貼留言