libraries / DHT-sensor-library-master / examples / DHTtester / DHTtester.inoon commit Added link to project report (97a3ba0)
   1// Example testing sketch for various DHT humidity/temperature sensors
   2// Written by ladyada, public domain
   3
   4#include "DHT.h"
   5
   6#define DHTPIN 2     // what digital pin we're connected to
   7
   8// Uncomment whatever type you're using!
   9//#define DHTTYPE DHT11   // DHT 11
  10#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
  11//#define DHTTYPE DHT21   // DHT 21 (AM2301)
  12
  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
  19
  20// Initialize DHT sensor.
  21// Note that older versions of this library took an optional third parameter to
  22// tweak the timings for faster processors.  This parameter is no longer needed
  23// as the current DHT reading algorithm adjusts itself to work on faster procs.
  24DHT dht(DHTPIN, DHTTYPE);
  25
  26void setup() {
  27  Serial.begin(9600);
  28  Serial.println("DHTxx test!");
  29
  30  dht.begin();
  31}
  32
  33void loop() {
  34  // Wait a few seconds between measurements.
  35  delay(2000);
  36
  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  float h = dht.readHumidity();
  40  // Read temperature as Celsius (the default)
  41  float t = dht.readTemperature();
  42  // Read temperature as Fahrenheit (isFahrenheit = true)
  43  float f = dht.readTemperature(true);
  44
  45  // Check if any reads failed and exit early (to try again).
  46  if (isnan(h) || isnan(t) || isnan(f)) {
  47    Serial.println("Failed to read from DHT sensor!");
  48    return;
  49  }
  50
  51  // Compute heat index in Fahrenheit (the default)
  52  float hif = dht.computeHeatIndex(f, h);
  53  // Compute heat index in Celsius (isFahreheit = false)
  54  float hic = dht.computeHeatIndex(t, h, false);
  55
  56  Serial.print("Humidity: ");
  57  Serial.print(h);
  58  Serial.print(" %\t");
  59  Serial.print("Temperature: ");
  60  Serial.print(t);
  61  Serial.print(" *C ");
  62  Serial.print(f);
  63  Serial.print(" *F\t");
  64  Serial.print("Heat index: ");
  65  Serial.print(hic);
  66  Serial.print(" *C ");
  67  Serial.print(hif);
  68  Serial.println(" *F");
  69}