Shekhar777

DHT TEST

Mar 28th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 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)
  11. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
  12.  
  13. // Initialize DHT sensor for normal 16mhz Arduino
  14. DHT dht(DHTPIN, DHTTYPE);
  15.  
  16. void setup() {
  17.   Serial.begin(9600);
  18.   Serial.println("DHTxx test!");
  19.  
  20.   dht.begin();
  21. }
  22.  
  23. void loop() {
  24.   // Wait a few seconds between measurements.
  25.   delay(2000);
  26.  
  27.   // Reading temperature or humidity takes about 250 milliseconds!
  28.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  29.   float h = dht.readHumidity();
  30.   // Read temperature as Celsius
  31.   float t = dht.readTemperature();
  32.   // Read temperature as Fahrenheit
  33.   float f = dht.readTemperature(true);
  34.  
  35.   // Check if any reads failed and exit early (to try again).
  36.   if (isnan(h) || isnan(t) || isnan(f)) {
  37.     Serial.println("Failed to read from DHT sensor!");
  38.     return;
  39.   }
  40.  
  41.   // Compute heat index
  42.   // Must send in temp in Fahrenheit!
  43.   float hi = dht.computeHeatIndex(f, h);
  44.  
  45.   Serial.print("Humidity: ");
  46.   Serial.print(h);
  47.   Serial.print(" %\t");
  48.   Serial.print("Temperature: ");
  49.   Serial.print(t);
  50.   Serial.print(" *C ");
  51.   Serial.print(f);
  52.   Serial.print(" *F\t");
  53.   Serial.print("Heat index: ");
  54.   Serial.print(hi);
  55.   Serial.println(" *F");
  56. }
Add Comment
Please, Sign In to add comment