Advertisement
Guest User

Untitled

a guest
May 27th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  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.
  24. DHT dht(DHTPIN, DHTTYPE);
  25.  
  26. void setup() {
  27.  
  28. Serial.begin(9600);
  29. Serial.println("DHTxx test!");
  30.  
  31. dht.begin();
  32. }
  33.  
  34. void loop() {
  35. // Wait a few seconds between measurements.
  36. delay(2000);
  37.  
  38. // Reading temperature or humidity takes about 250 milliseconds!
  39. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  40. float h = dht.readHumidity();
  41. // Read temperature as Celsius (the default)
  42. float t = dht.readTemperature();
  43. // Read temperature as Fahrenheit (isFahrenheit = true)
  44. float f = dht.readTemperature(true);
  45.  
  46. // Check if any reads failed and exit early (to try again).
  47. if (isnan(h) || isnan(t) || isnan(f)) {
  48. Serial.println("Failed to read from DHT sensor!");
  49. return;
  50. }
  51.  
  52. // Compute heat index in Fahrenheit (the default)
  53. float hif = dht.computeHeatIndex(f, h);
  54. // Compute heat index in Celsius (isFahreheit = false)
  55. float hic = dht.computeHeatIndex(t, h, false);
  56.  
  57. Serial.print("Humidity: ");
  58. Serial.print(h);
  59. Serial.print(" %\t");
  60. Serial.print("Temperature: ");
  61. Serial.print(t);
  62. Serial.print(" *C ");
  63. Serial.print(f);
  64. Serial.print(" *F\t");
  65. Serial.print("Heat index: ");
  66. Serial.print(hic);
  67. Serial.print(" *C ");
  68. Serial.print(hif);
  69. Serial.println(" *F");
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement