另外DHT22別名則是AM2302,不過內部晶片似乎有其不一樣之處,這可能還需各位前輩指教了
我所使用的DHT22Library則是DHT-sensor-library
下圖是我所購買的DHT22
腳位接法如下:
VCC接5V DATA接Pin 2 NC不接 GND接GND
程式碼:
#include <DHT.h> #define PIN 2 #define TYPE DHT22 DHT dht(PIN, TYPE); // the setup function runs once when you press reset or power the board void setup() { Serial.begin(9600); } // the loop function runs over and over again until power down or reset void loop() { delay(1000); 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; } Serial.print(h, 2); Serial.print(","); Serial.println(t, 2); }
執行結果:
參考資料:
https://github.com/adafruit/DHT-sensor-library