.
DHT-11 是一個結合濕度計和測溫元件量測週遭空氣環境,並與一個高性能8位元單晶片相連接,將所量測到的溫、濕度資料拆解成為數位訊號,再由 data pin腳將資料送出。
腳位圖與接線方式
圖源: http://ming-shian.blogspot.tw/2014/05/arduino19dht11.html
以下程式會將從 DHT11取到的溫度、溼度與露點溫度呈現在LCD顯示器上,每2秒更新一次
Source Code
- #include "DHT.h"
- #include <LiquidCrystal.h>
- #define DHTPIN 7 // what digital pin we're connected to
- #define DHTTYPE DHT11 // DHT 11
- DHT dht(DHTPIN, DHTTYPE);
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- unsigned long time_previous,now;
- void setup() {
- Serial.begin(9600);
- Serial.println("DHTxx test!");
- //------------------------------
- // 設定 LCD 的行列數目 (2 x 16)
- lcd.begin(16, 2);
- // 列印 "Welcome" 訊息到 LCD 上
- lcd.print("Welcome!");
- delay(1000);
- lcd.clear();
- time_previous = millis();
- //-----------------------------
- dht.begin();
- }
-
- void loop() {
- // Wait a few seconds between measurements.
- 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");
- //---------------
- // 將游標設到 column 0, line 1
- // (注意: line 1 是第二行(row),因為是從 0 開始數起):
- lcd.setCursor(0, 0);
- lcd.print(String("") + "H:" + h + "%");
- lcd.setCursor(0, 1);
- // 列印 Arduino 重開之後經過的秒數
- lcd.print(String("") + "T:"+ t + (char)223 + "C");
- //-------------
- }
執行結果
顯示器上呈現取得的溼度與溫度
從comport上顯示讀取到的資訊
實際運行畫面