W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在本節(jié)中,我們將學(xué)習(xí)如何使用不同的傳感器連接我們的Arduino板。我們將討論以下傳感器:
DHT-22(也稱為AM2302)是一個(gè)數(shù)字輸出,相對濕度和溫度傳感器。它使用電容式濕度傳感器和熱敏電阻來測量周圍空氣,并在數(shù)據(jù)引腳上發(fā)送數(shù)字信號。
在本例中,你將學(xué)習(xí)如何將此傳感器與Arduino UNO一起使用。室溫和濕度將打印到串口監(jiān)視器上。
連接很簡單。左邊的第一個(gè)引腳為3-5V電源,第二個(gè)引腳連接到數(shù)據(jù)輸入引腳,最右邊的引腳接地。
電源 - 3-5V
最大電流 - 2.5mA
濕度 - 0-100%,精確度為2-5%
溫度 - 40至80°C,精確度為±0.5°C
你將需要以下組件:
按照電路圖連接面包板上的組件,如下圖所示。
在計(jì)算機(jī)上打開Arduino IDE軟件。使用Arduino語言進(jìn)行編碼控制你的電路。通過單擊“New”打開一個(gè)新的草圖文件。
// 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傳感器具有四個(gè)端子連接到電路板的端子(Vcc,DATA,NC,GND),如下:
一旦硬件連接完成,你需要添加DHT22庫到你的Arduino庫文件,如前所述。
你將看到串口監(jiān)視器上的溫度和濕度顯示,每2秒更新一次。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: