DHTtester.ino 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Example testing sketch for various DHT humidity/temperature sensors
  2. // Written by ladyada, public domain
  3. #include "DHT.h"
  4. #define DHTPIN 2 // what digital 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), AM2321
  8. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  9. // Connect pin 1 (on the left) of the sensor to +5V
  10. // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
  11. // to 3.3V instead of 5V!
  12. // Connect pin 2 of the sensor to whatever your DHTPIN is
  13. // Connect pin 4 (on the right) of the sensor to GROUND
  14. // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
  15. // Initialize DHT sensor.
  16. // Note that older versions of this library took an optional third parameter to
  17. // tweak the timings for faster processors. This parameter is no longer needed
  18. // as the current DHT reading algorithm adjusts itself to work on faster procs.
  19. DHT dht(DHTPIN, DHTTYPE);
  20. void setup() {
  21. Serial.begin(9600);
  22. Serial.println("DHTxx test!");
  23. dht.begin();
  24. }
  25. void loop() {
  26. // Wait a few seconds between measurements.
  27. delay(2000);
  28. // Reading temperature or humidity takes about 250 milliseconds!
  29. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  30. float h = dht.readHumidity();
  31. // Read temperature as Celsius (the default)
  32. float t = dht.readTemperature();
  33. // Read temperature as Fahrenheit (isFahrenheit = true)
  34. float f = dht.readTemperature(true);
  35. // Check if any reads failed and exit early (to try again).
  36. if (isnan(h) || isnan(t) || isnan(f)) {
  37. Serial.println("Failed to read from DHT sensor!");
  38. return;
  39. }
  40. // Compute heat index in Fahrenheit (the default)
  41. float hif = dht.computeHeatIndex(f, h);
  42. // Compute heat index in Celsius (isFahreheit = false)
  43. float hic = dht.computeHeatIndex(t, h, false);
  44. Serial.print("Humidity: ");
  45. Serial.print(h);
  46. Serial.print(" %\t");
  47. Serial.print("Temperature: ");
  48. Serial.print(t);
  49. Serial.print(" *C ");
  50. Serial.print(f);
  51. Serial.print(" *F\t");
  52. Serial.print("Heat index: ");
  53. Serial.print(hic);
  54. Serial.print(" *C ");
  55. Serial.print(hif);
  56. Serial.println(" *F");
  57. }