DHT_Unified_Sensor.ino 3.2 KB

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