Arduino - 溼度感測器



在本節中,我們將學習如何將 Arduino 板與不同的感測器連線起來。我們將討論以下感測器:

  • 溼度感測器 (DHT22)
  • 溫度感測器 (LM35)
  • 水位檢測感測器(簡單水位觸發器)
  • PIR 感測器
  • 超聲波感測器
  • GPS

溼度感測器 (DHT22)

DHT-22(也稱為 AM2302)是一種數字輸出、相對溼度和溫度感測器。它使用電容式溼度感測器和熱敏電阻來測量周圍空氣,並在資料引腳上傳送數字訊號。

在本例中,您將學習如何將此感測器與 Arduino UNO 一起使用。房間溫度和溼度將列印到序列監視器。

DHT-22 感測器

DHT-22 Sensor

連線很簡單。最左邊的第一個引腳連線到 3-5V 電源,第二個引腳連線到資料輸入引腳,最右邊的引腳連線到地。

技術細節

  • 電源 − 3-5V

  • 最大電流 − 2.5mA

  • 溼度 − 0-100%,精度 2-5%

  • 溫度 − 40 至 80°C,精度 ±0.5°C

所需元件

您將需要以下元件:

  • 1 × 麵包板
  • 1 × Arduino Uno R3
  • 1 × DHT22
  • 1 × 10K 歐姆電阻

步驟

按照電路圖,將元件連線到麵包板上,如下面的圖片所示。

Humidity Sensor Circuit Connection

Humidity Sensor Breadboard

草圖

在您的計算機上開啟 Arduino IDE 軟體。使用 Arduino 語言進行編碼將控制您的電路。透過點選“新建”開啟一個新的草圖檔案。

Sketch

Arduino 程式碼

// Example testing sketch for various DHT humidity/temperature sensors

#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() {
   delay(2000); // Wait a few seconds between measurements
   float h = dht.readHumidity();
   // Reading temperature or humidity takes about 250 milliseconds!
   float t = dht.readTemperature();
   // Read temperature as Celsius (the default)
   float f = dht.readTemperature(true);
   // Read temperature as Fahrenheit (isFahrenheit = 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");
}

程式碼說明

DHT22 感測器有四個端子 (Vcc、DATA、NC、GND),連線到板上的方式如下:

  • DATA 引腳連線到 Arduino 引腳 2
  • Vcc 引腳連線到 Arduino 板的 5 伏
  • GND 引腳連線到 Arduino 板的地
  • 我們需要在 DATA 和 Vcc 引腳之間連線一個 10k 歐姆電阻(上拉電阻)

硬體連線完成後,需要將 DHT22 庫新增到 Arduino 庫檔案中,如前所述。

結果

您將在序列埠監視器上看到溫度和溼度顯示,每 2 秒更新一次。

廣告