DHTtester.ino 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #define debug SerialUSB
  23. #else
  24. #define debug Serial
  25. #endif
  26. void setup() {
  27. debug.begin(115200);
  28. debug.println("DHTxx test!");
  29. Wire.begin();
  30. /*if using WIO link,must pull up the power pin.*/
  31. // pinMode(PIN_GROVE_POWER, OUTPUT);
  32. // digitalWrite(PIN_GROVE_POWER, 1);
  33. dht.begin();
  34. }
  35. void loop() {
  36. float temp_hum_val[2] = {0};
  37. // Reading temperature or humidity takes about 250 milliseconds!
  38. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  39. if (!dht.readTempAndHumidity(temp_hum_val)) {
  40. debug.print("Humidity: ");
  41. debug.print(temp_hum_val[0]);
  42. debug.print(" %\t");
  43. debug.print("Temperature: ");
  44. debug.print(temp_hum_val[1]);
  45. debug.println(" *C");
  46. } else {
  47. debug.println("Failed to get temprature and humidity value.");
  48. }
  49. delay(1500);
  50. }