Advertisement
Maderdash

SocialComrad

Dec 28th, 2021
170
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. // 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 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
  24. // Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
  25. // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
  26.  
  27. // Initialize DHT sensor.
  28. // Note that older versions of this library took an optional third parameter to
  29. // tweak the timings for faster processors.  This parameter is no longer needed
  30. // as the current DHT reading algorithm adjusts itself to work on faster procs.
  31. DHT dht(DHTPIN, DHTTYPE);
  32.  
  33. void setup() {
  34.   Serial.begin(9600);
  35.   Serial.println(F("DHTxx test!"));
  36.  
  37.   dht.begin();
  38. }
  39.  
  40. void loop() {
  41.   // Wait a few seconds between measurements.
  42.   delay(2000);
  43.  
  44.   // Reading temperature or humidity takes about 250 milliseconds!
  45.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  46.   float h = dht.readHumidity();
  47.   // Read temperature as Celsius (the default)
  48.   float t = dht.readTemperature();
  49.   // Read temperature as Fahrenheit (isFahrenheit = true)
  50.   float f = dht.readTemperature(true);
  51.  
  52.   // Check if any reads failed and exit early (to try again).
  53.   if (isnan(h) || isnan(t) || isnan(f)) {
  54.     Serial.println(F("Failed to read from DHT sensor!"));
  55.     return;
  56.   }
  57.  
  58.   // Compute heat index in Fahrenheit (the default)
  59.   float hif = dht.computeHeatIndex(f, h);
  60.   // Compute heat index in Celsius (isFahreheit = false)
  61.   float hic = dht.computeHeatIndex(t, h, false);
  62.  
  63.   Serial.print(F("Humidity: "));
  64.   Serial.print(h);
  65.   Serial.print(F("%  Temperature: "));
  66.   Serial.print(t);
  67.   Serial.print(F("°C "));
  68.   Serial.print(f);
  69.   Serial.print(F("°F  Heat index: "));
  70.   Serial.print(hic);
  71.   Serial.print(F("°C "));
  72.   Serial.print(hif);
  73.   Serial.println(F("°F"));
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement