1 // Example testing sketch for various DHT humidity/temperature sensors
2 // Written by ladyada, public domain
6 #define DHTPIN 2 // what pin we're connected to
8 // Uncomment whatever type you're using!
9 //#define DHTTYPE DHT11 // DHT 11
10 #define DHTTYPE DHT22 // DHT 22 (AM2302)
11 //#define DHTTYPE DHT21 // DHT 21 (AM2301)
13 // Connect pin 1 (on the left) of the sensor to +5V
14 // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
15 // to 3.3V instead of 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
20 // Initialize DHT sensor for normal 16mhz Arduino
21 DHT dht(DHTPIN, DHTTYPE);
22 // NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
23 // might need to increase the threshold for cycle counts considered a 1 or 0.
24 // You can do this by passing a 3rd parameter for this threshold. It's a bit
25 // of fiddling to find the right value, but in general the faster the CPU the
26 // higher the value. The default for a 16mhz AVR is a value of 6. For an
27 // Arduino Due that runs at 84mhz a value of 30 works.
28 // Example to initialize DHT sensor for Arduino Due:
29 //DHT dht(DHTPIN, DHTTYPE, 30);
33 Serial.println("DHTxx test!");
39 // Wait a few seconds between measurements.
42 // Reading temperature or humidity takes about 250 milliseconds!
43 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
44 float h = dht.readHumidity();
45 // Read temperature as Celsius
46 float t = dht.readTemperature();
47 // Read temperature as Fahrenheit
48 float f = dht.readTemperature(true);
50 // Check if any reads failed and exit early (to try again).
51 if (isnan(h) || isnan(t) || isnan(f)) {
52 Serial.println("Failed to read from DHT sensor!");
57 // Must send in temp in Fahrenheit!
58 float hi = dht.computeHeatIndex(f, h);
60 Serial.print("Humidity: ");
63 Serial.print("Temperature: ");
67 Serial.print(" *F\t");
68 Serial.print("Heat index: ");
70 Serial.println(" *F");