首先在该地址下载库:https://codeload.github.com/nethoncho/Arduino-DHT22/zip/master
使用以下代码测试:
/******************************************************* 这个程序用来测试DHT22的温湿度。 ********************************************************/ #include <dht.h> dht DHT; #define DHT22_PIN 7 void setup() { Serial.begin(115200); Serial.println("DHT TEST PROGRAM "); Serial.print("LIBRARY VERSION: "); Serial.println(DHT_LIB_VERSION); Serial.println(); Serial.println("Type, status, Humidity (%), Temperature (C)"); } void loop() { Serial.print("DHT22, "); int chk = DHT.read22(DHT22_PIN); //读取数据 switch (chk) { case DHTLIB_OK: Serial.print("OK, "); break; case DHTLIB_ERROR_CHECKSUM: Serial.print("Checksum error, "); break; case DHTLIB_ERROR_TIMEOUT: Serial.print("Time out error, "); break; default: Serial.print("Unknown error, "); break; } // 显示数据 Serial.print(DHT.humidity, 1); Serial.print(", "); Serial.println(DHT.temperature, 1); delay(1000); }