DHTtester.ino 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 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. /*Notice: The DHT10 is different from other DHT* sensor ,it uses i2c interface rather than one wire*/
  10. /*So it doesn't require a pin.*/
  11. //#define DHTTYPE DHT10
  12. // Connect pin 1 (on the left) of the sensor to +5V
  13. // Connect pin 2 of the sensor to whatever your DHTPIN is
  14. // Connect pin 4 (on the right) of the sensor to GROUND
  15. // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
  16. DHT dht(DHTPIN, DHTTYPE);
  17. #if defined(ARDUINO_ARCH_AVR)
  18. #define debug Serial
  19. #elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM)
  20. #define debug SerialUSB
  21. #else
  22. #define debug Serial
  23. #endif
  24. void setup() {
  25. debug.begin(115200);
  26. debug.println("DHTxx test!");
  27. Wire.begin();
  28. /*if using WIO link,must pull up the power pin.*/
  29. // pinMode(PIN_GROVE_POWER, OUTPUT);
  30. // digitalWrite(PIN_GROVE_POWER, 1);
  31. dht.begin();
  32. }
  33. void loop() {
  34. float temp_hum_val[2] = {0};
  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. if (!dht.readTempAndHumidity(temp_hum_val)) {
  38. debug.print("Humidity: ");
  39. debug.print(temp_hum_val[0]);
  40. debug.print(" %\t");
  41. debug.print("Temperature: ");
  42. debug.print(temp_hum_val[1]);
  43. debug.println(" *C");
  44. } else {
  45. debug.println("Failed to get temprature and humidity value.");
  46. }
  47. delay(1500);
  48. }