首先在该地址下载库: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,\tstatus,\tHumidity (%),\tTemperature (C)"); } void loop() { Serial.print("DHT22, \t"); int chk = DHT.read22(DHT22_PIN); //读取数据 switch (chk) { case DHTLIB_OK: Serial.print("OK,\t"); break; case DHTLIB_ERROR_CHECKSUM: Serial.print("Checksum error,\t"); break; case DHTLIB_ERROR_TIMEOUT: Serial.print("Time out error,\t"); break; default: Serial.print("Unknown error,\t"); break; } // 显示数据 Serial.print(DHT.humidity, 1); Serial.print(",\t"); Serial.println(DHT.temperature, 1); delay(1000); }
原文地址:https://www.cnblogs.com/wuchaodzxx/p/8443043.html
时间: 2024-10-17 14:31:07