DHT_Unified_Sensor.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // DHT Temperature & Humidity Sensor
  2. // Unified Sensor Library Example
  3. // Written by Tony DiCola for Adafruit Industries
  4. // Released under an MIT license.
  5. // Depends on the following Arduino libraries:
  6. // - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
  7. // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
  8. #include <Adafruit_Sensor.h>
  9. #include <DHT.h>
  10. #include <DHT_U.h>
  11. #define DHTPIN 2 // Pin which is connected to the DHT sensor.
  12. // Uncomment the type of sensor in use:
  13. //#define DHTTYPE DHT11 // DHT 11
  14. #define DHTTYPE DHT22 // DHT 22 (AM2302)
  15. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  16. // See guide for details on sensor wiring and usage:
  17. // https://learn.adafruit.com/dht/overview
  18. DHT_Unified dht(DHTPIN, DHTTYPE);
  19. uint32_t delayMS;
  20. void setup() {
  21. Serial.begin(9600);
  22. // Initialize device.
  23. dht.begin();
  24. Serial.println("DHTxx Unified Sensor Example");
  25. // Print temperature sensor details.
  26. sensor_t sensor;
  27. dht.temperature().getSensor(&sensor);
  28. Serial.println("------------------------------------");
  29. Serial.println("Temperature");
  30. Serial.print ("Sensor: "); Serial.println(sensor.name);
  31. Serial.print ("Driver Ver: "); Serial.println(sensor.version);
  32. Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
  33. Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
  34. Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
  35. Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
  36. Serial.println("------------------------------------");
  37. // Print humidity sensor details.
  38. dht.humidity().getSensor(&sensor);
  39. Serial.println("------------------------------------");
  40. Serial.println("Humidity");
  41. Serial.print ("Sensor: "); Serial.println(sensor.name);
  42. Serial.print ("Driver Ver: "); Serial.println(sensor.version);
  43. Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
  44. Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
  45. Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
  46. Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
  47. Serial.println("------------------------------------");
  48. // Set delay between sensor readings based on sensor details.
  49. delayMS = sensor.min_delay / 1000;
  50. }
  51. void loop() {
  52. // Delay between measurements.
  53. delay(delayMS);
  54. // Get temperature event and print its value.
  55. sensors_event_t event;
  56. dht.temperature().getEvent(&event);
  57. if (isnan(event.temperature)) {
  58. Serial.println("Error reading temperature!");
  59. }
  60. else {
  61. Serial.print("Temperature: ");
  62. Serial.print(event.temperature);
  63. Serial.println(" *C");
  64. }
  65. // Get humidity event and print its value.
  66. dht.humidity().getEvent(&event);
  67. if (isnan(event.relative_humidity)) {
  68. Serial.println("Error reading humidity!");
  69. }
  70. else {
  71. Serial.print("Humidity: ");
  72. Serial.print(event.relative_humidity);
  73. Serial.println("%");
  74. }
  75. }