DHTtester.ino 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 SERIAL Serial
  19. #elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM)
  20. #define SERIAL SerialUSB
  21. #else
  22. #define SERIAL Serial
  23. #endif
  24. void setup()
  25. {
  26. SERIAL.begin(115200);
  27. SERIAL.println("DHTxx test!");
  28. Wire.begin();
  29. /*if using WIO link,must pull up the power pin.*/
  30. // pinMode(PIN_GROVE_POWER, OUTPUT);
  31. // digitalWrite(PIN_GROVE_POWER, 1);
  32. dht.begin();
  33. }
  34. void loop()
  35. {
  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. SERIAL.print("Humidity: ");
  41. SERIAL.print(temp_hum_val[0]);
  42. SERIAL.print(" %\t");
  43. SERIAL.print("Temperature: ");
  44. SERIAL.print(temp_hum_val[1]);
  45. SERIAL.println(" *C");
  46. }
  47. else{
  48. SERIAL.println("Failed to get temprature and humidity value.");
  49. }
  50. delay(1500);
  51. }