DHTtester.ino 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Example testing sketch for various DHT humidity/temperature sensors
  2. // Written by ladyada, public domain
  3. #include "DHT.h"
  4. #define DHTPIN A0 // what pin we're connected to
  5. // Uncomment whatever type you're using!
  6. //#define DHTTYPE DHT11 // DHT 11
  7. #define DHTTYPE DHT22 // DHT 22 (AM2302)
  8. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  9. // Connect pin 1 (on the left) of the sensor to +5V
  10. // Connect pin 2 of the sensor to whatever your DHTPIN is
  11. // Connect pin 4 (on the right) of the sensor to GROUND
  12. // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
  13. DHT dht(DHTPIN, DHTTYPE);
  14. void setup()
  15. {
  16. Serial.begin(9600);
  17. Serial.println("DHTxx test!");
  18. dht.begin();
  19. }
  20. void loop()
  21. {
  22. // Reading temperature or humidity takes about 250 milliseconds!
  23. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  24. float h = dht.readHumidity();
  25. float t = dht.readTemperature();
  26. // check if returns are valid, if they are NaN (not a number) then something went wrong!
  27. if (isnan(t) || isnan(h))
  28. {
  29. Serial.println("Failed to read from DHT");
  30. }
  31. else
  32. {
  33. Serial.print("Humidity: ");
  34. Serial.print(h);
  35. Serial.print(" %\t");
  36. Serial.print("Temperature: ");
  37. Serial.print(t);
  38. Serial.println(" *C");
  39. }
  40. }