V1.ino 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Example testing sketch for various DHT humidity/temperature sensors
  2. // Written by ladyada, public domain
  3. // REQUIRES the following Arduino libraries:
  4. // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
  5. // - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
  6. #include "DHT.h"
  7. #define DHTPIN 4 // Digital pin connected to the DHT sensor
  8. // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
  9. // Pin 15 can work but DHT must be disconnected during program upload.
  10. // Uncomment whatever type you're using!
  11. //#define DHTTYPE DHT11 // DHT 11
  12. #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
  13. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  14. // Connect pin 1 (on the left) of the sensor to +5V
  15. // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
  16. // to 3.3V instead of 5V!
  17. // Connect pin 2 of the sensor to whatever your DHTPIN is
  18. // Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
  19. // Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
  20. // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
  21. // Initialize DHT sensor.
  22. // Note that older versions of this library took an optional third parameter to
  23. // tweak the timings for faster processors. This parameter is no longer needed
  24. // as the current DHT reading algorithm adjusts itself to work on faster procs.
  25. DHT dht(DHTPIN, DHTTYPE);
  26. unsigned long Tini, Tfin;
  27. void setup() {
  28. Serial.begin(9600);
  29. Serial.println(F("DHTxx test!"));
  30. dht.begin();
  31. }
  32. void loop() {
  33. // Wait a few seconds between measurements.
  34. delay(2000);
  35. // Reading temperature or humidity takes about 250 milliseconds!
  36. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  37. Tini = millis();
  38. float h = dht.readHumidity();
  39. // Read temperature as Celsius (the default)
  40. Serial.println(millis()-Tini);
  41. float t = dht.readTemperature();
  42. // Read temperature as Fahrenheit (isFahrenheit = true)
  43. float f = dht.readTemperature(true);
  44. // Check if any reads failed and exit early (to try again).
  45. if (isnan(h) || isnan(t) || isnan(f)) {
  46. Serial.println(F("Failed to read from DHT sensor!"));
  47. return;
  48. }
  49. // Compute heat index in Fahrenheit (the default)
  50. float hif = dht.computeHeatIndex(f, h);
  51. // Compute heat index in Celsius (isFahreheit = false)
  52. float hic = dht.computeHeatIndex(t, h, false);
  53. Serial.print(F("Humidity: "));
  54. Serial.print(h);
  55. Serial.print(F("% Temperature: "));
  56. Serial.print(t);
  57. Serial.print(F("°C "));
  58. Serial.print(f);
  59. Serial.print(F("°F Heat index: "));
  60. Serial.print(hic);
  61. Serial.print(F("°C "));
  62. Serial.print(hif);
  63. Serial.println(F("°F"));
  64. }