傳感器的使用——DH11的使用方法


使用DH11先把上面的圓柱蓋先拿開后,可以看到接線方法,如 ACC、OUT、GND。另外板子上還有二個插口,分別可用於 光敏電阻 和 熱敏電阻,可以擴展自己的功能。

要使用DH11需要先下載DH11的函數庫,打開Arduino后,管理庫,在搜索 DH11 即可搜索到 DHT_sensor_library,打開示例 DHTtester ,編譯上傳,會發現一個錯誤,大致意思是缺少 Adafruit_Sensor.h 這個頭文件,可在 https://github.com/adafruit/Adafruit_Sensor 此處下載,將下載后的壓縮包解壓后,找到 Adafruit_Sensor.h 文件,復制到 庫文件夾 DHT_sensor_library 下即可,重新打開 Arduino后,編譯就沒有錯誤了,上傳之后可以看到 濕度,C溫度,F溫度,體感C溫度,體感F溫度。

示例代碼如下:

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

// 導入頭文件
#include "DHT.h"
// 定義OUT接口的數字引腳
#define DHTPIN 2     // what digital pin we're connected to

// Uncomment whatever type you're using!
// 選擇DHT類型為 DHT 11
#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 對象,參數是PIN和DHT類型
DHT dht(DHTPIN, DHTTYPE);

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

  // 必須使用的begin()函數  
  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  // 每次等待2秒后再輸出(這里必須等大於1秒,不然不准確)
  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)
  // readHumidity() 這里是讀取當前的濕度
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  // readTemperature() 讀取當前的溫度,單位C
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  //readTemperature(true) 讀取當前的溫度,單位F
  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)
  // 讀取體感溫度,單位F
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  // 讀取體感溫度,單位C
  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");
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM