close

DHT11的資料在網路上很好找,這裡不多介紹細節,

詳細規格資料在上一篇中已經有提供。

原生Arduino系統沒有支援DHT系列感測器,所以必須

先載入相關的函式庫(Library)。

早期的開發工具都必須自行找尋相關函式庫,現在

新版IDE開發工具已經內建函式庫搜尋功能,DHT

就在支援之列。

開啟Arduino IDE之後,如下圖:

 

呼叫出程式庫管理員介面如下圖,然後搜尋DHT,可以看到有許多不同的程式庫出現,

我建議選擇如下圖我框起來的這個程式庫,因為支援度完整:

點選more info會出現選擇版本,我的建議就是選1.2.3,不要使用1.3.0,原因是程式編譯時會出現

錯誤訊息,可以手動修正但需要外部程式庫比較麻煩,如果只是簡單驗證程式而已,1.2.3就夠了。

這時候可以開啟範例程式,如下圖中所示會出現如下範例,DHTtester:

 

DHTtester程式如下:

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  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");
}

 

程式說明:

要使用DHT除了程式庫要裝起來之外,程式開頭要宣告include DHT.h進來。

再來要define DHT sensor所使用的腳位,範例中是pin 2。

第三要define 所使用sensor的類型,原本範例是用DHT22,這裡改用DHT11。

後面程式就是讀取DHT sensor的內容,然後輸出到com port介面,

只要設定好正確的com port速度,打開com port監視畫面就能看到溫濕度了。

範例輸出如下圖,溫濕度不會跟底下畫面一樣,會跟你的測試環境相同:

 

如果接線錯誤或是sensor故障,程式也會判斷出來並顯示錯誤訊息:

 

以上就完成了DHT sensor的驅動跟使用,

底下說明手動安裝程式庫及解決官方1.3.0版問題的方法。

 

手動安裝程式庫

DHT sensor系列函式庫下載
http://github.com/adafruit/DHT-sensor-library 
函式庫名稱為 DHT-sensor-library-master


注意:只下載此函式庫的話,編譯範例程式會出現錯誤,

需下載以下函式庫並放入Arduino程式庫中

https://github.com/adafruit/Adafruit_Sensor
成式庫名稱為 Adafruit_Sensor-master

以上程式庫也是官版1.3.0程式庫所額外需要的程式庫,下載之後放到Arduino程式庫目錄中,

就能解決1.3.0版編譯的問題。

 

錯誤訊息如下:

Arduino:1.8.5 (Windows 10), 開發板:"Arduino/Genuino Uno"

建置選項已變更,重新建置
In file included from E:\owncloud\Documents\My Arduino project\libraries\DHT_sensor_library\DHT_U.cpp:22:0:

E:\owncloud\Documents\My Arduino project\libraries\DHT_sensor_library\DHT_U.h:25:29: fatal error: Adafruit_Sensor.h: No such file or directory

 #include <Adafruit_Sensor.h>

                             ^

compilation terminated.

exit status 1
開發板 Arduino/Genuino Uno 編譯錯誤。

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
 

arrow
arrow

    呂阿谷 發表在 痞客邦 留言(1) 人氣()