| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- // Example testing sketch for various DHT humidity/temperature sensors
- // Written by ladyada, public domain
- #include "Grove_Temperature_And_Humidity_Sensor.h"
- // Uncomment whatever type you're using!
- //#define DHTTYPE DHT11 // DHT 11
- #define DHTTYPE DHT22 // DHT 22 (AM2302)
- //#define DHTTYPE DHT21 // DHT 21 (AM2301)
- //#define DHTTYPE DHT10 // DHT 10
- //#define DHTTYPE DHT20 // DHT 20
- /*Notice: The DHT10 and DHT20 is different from other DHT* sensor ,it uses i2c interface rather than one wire*/
- /*So it doesn't require a pin.*/
- #define DHTPIN 2 // what pin we're connected to(DHT10 and DHT20 don't need define it)
- DHT dht(DHTPIN, DHTTYPE); // DHT11 DHT21 DHT22
- //DHT dht(DHTTYPE); // DHT10 DHT20 don't need to define Pin
- // Connect pin 1 (on the left) of the sensor to +5V
- // Connect pin 2 of the sensor to whatever your DHTPIN is
- // Connect pin 4 (on the right) of the sensor to GROUND
- // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
- #if defined(ARDUINO_ARCH_AVR)
- #define debug Serial
- #elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM)
- #ifdef SerialUSB
- #define debug SerialUSB
- #else
- #define debug Serial
- #endif
- #else
- #define debug Serial
- #endif
- void setup() {
- debug.begin(115200);
- debug.println("DHTxx test!");
- Wire.begin();
- /*if using WIO link,must pull up the power pin.*/
- // pinMode(PIN_GROVE_POWER, OUTPUT);
- // digitalWrite(PIN_GROVE_POWER, 1);
- dht.begin();
- }
- void loop() {
- float temp_hum_val[2] = {0};
- // Reading temperature or humidity takes about 250 milliseconds!
- // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
- if (!dht.readTempAndHumidity(temp_hum_val)) {
- debug.print("Humidity: ");
- debug.print(temp_hum_val[0]);
- debug.print(" %\t");
- debug.print("Temperature: ");
- debug.print(temp_hum_val[1]);
- debug.println(" *C");
- } else {
- debug.println("Failed to get temprature and humidity value.");
- }
- delay(1500);
- }
|