DHTtester.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Example testing sketch for various DHT humidity/temperature sensors
  2. // Written by ladyada, public domain
  3. #include "Grove_Temperature_And_Humidity_Sensor.h"
  4. // Uncomment whatever type you're using!
  5. //#define DHTTYPE DHT11 // DHT 11
  6. #define DHTTYPE DHT22 // DHT 22 (AM2302)
  7. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  8. //#define DHTTYPE DHT10 // DHT 10
  9. //#define DHTTYPE DHT20 // DHT 20
  10. /*Notice: The DHT10 and DHT20 is different from other DHT* sensor ,it uses i2c interface rather than one wire*/
  11. /*So it doesn't require a pin.*/
  12. #define DHTPIN 2 // what pin we're connected to(DHT10 and DHT20 don't need define it)
  13. DHT dht(DHTPIN, DHTTYPE); // DHT11 DHT21 DHT22
  14. //DHT dht(DHTTYPE); // DHT10 DHT20 don't need to define Pin
  15. // Connect pin 1 (on the left) of the sensor to +5V
  16. // Connect pin 2 of the sensor to whatever your DHTPIN is
  17. // Connect pin 4 (on the right) of the sensor to GROUND
  18. // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
  19. #if defined(ARDUINO_ARCH_AVR)
  20. #define debug Serial
  21. #elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM)
  22. #ifdef SerialUSB
  23. #define debug SerialUSB
  24. #else
  25. #define debug Serial
  26. #endif
  27. #else
  28. #define debug Serial
  29. #endif
  30. void setup() {
  31. debug.begin(115200);
  32. debug.println("DHTxx test!");
  33. Wire.begin();
  34. /*if using WIO link,must pull up the power pin.*/
  35. // pinMode(PIN_GROVE_POWER, OUTPUT);
  36. // digitalWrite(PIN_GROVE_POWER, 1);
  37. dht.begin();
  38. }
  39. void loop() {
  40. float temp_hum_val[2] = {0};
  41. // Reading temperature or humidity takes about 250 milliseconds!
  42. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  43. if (!dht.readTempAndHumidity(temp_hum_val)) {
  44. debug.print("Humidity: ");
  45. debug.print(temp_hum_val[0]);
  46. debug.print(" %\t");
  47. debug.print("Temperature: ");
  48. debug.print(temp_hum_val[1]);
  49. debug.println(" *C");
  50. } else {
  51. debug.println("Failed to get temprature and humidity value.");
  52. }
  53. delay(1500);
  54. }