Advertisement
Guest User

Untitled

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