arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

DHTtester.ino (2539B)


      1 // Example testing sketch for various DHT humidity/temperature sensors
      2 // Written by ladyada, public domain
      3 
      4 // REQUIRES the following Arduino libraries:
      5 // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
      6 // - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
      7 
      8 #include "DHT.h"
      9 
     10 #define DHTPIN 2     // Digital pin connected to the DHT sensor
     11 // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
     12 // Pin 15 can work but DHT must be disconnected during program upload.
     13 
     14 // Uncomment whatever type you're using!
     15 //#define DHTTYPE DHT11   // DHT 11
     16 #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
     17 //#define DHTTYPE DHT21   // DHT 21 (AM2301)
     18 
     19 // Connect pin 1 (on the left) of the sensor to +5V
     20 // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
     21 // to 3.3V instead of 5V!
     22 // Connect pin 2 of the sensor to whatever your DHTPIN is
     23 // Connect pin 4 (on the right) of the sensor to GROUND
     24 // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
     25 
     26 // Initialize DHT sensor.
     27 // Note that older versions of this library took an optional third parameter to
     28 // tweak the timings for faster processors.  This parameter is no longer needed
     29 // as the current DHT reading algorithm adjusts itself to work on faster procs.
     30 DHT dht(DHTPIN, DHTTYPE);
     31 
     32 void setup() {
     33   Serial.begin(9600);
     34   Serial.println(F("DHTxx test!"));
     35 
     36   dht.begin();
     37 }
     38 
     39 void loop() {
     40   // Wait a few seconds between measurements.
     41   delay(2000);
     42 
     43   // Reading temperature or humidity takes about 250 milliseconds!
     44   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
     45   float h = dht.readHumidity();
     46   // Read temperature as Celsius (the default)
     47   float t = dht.readTemperature();
     48   // Read temperature as Fahrenheit (isFahrenheit = true)
     49   float f = dht.readTemperature(true);
     50 
     51   // Check if any reads failed and exit early (to try again).
     52   if (isnan(h) || isnan(t) || isnan(f)) {
     53     Serial.println(F("Failed to read from DHT sensor!"));
     54     return;
     55   }
     56 
     57   // Compute heat index in Fahrenheit (the default)
     58   float hif = dht.computeHeatIndex(f, h);
     59   // Compute heat index in Celsius (isFahreheit = false)
     60   float hic = dht.computeHeatIndex(t, h, false);
     61 
     62   Serial.print(F("Humidity: "));
     63   Serial.print(h);
     64   Serial.print(F("%  Temperature: "));
     65   Serial.print(t);
     66   Serial.print(F("°C "));
     67   Serial.print(f);
     68   Serial.print(F("°F  Heat index: "));
     69   Serial.print(hic);
     70   Serial.print(F("°C "));
     71   Serial.print(hif);
     72   Serial.println(F("°F"));
     73 }