Advertisement
iyera20

Humidity Sensor

Jun 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. //www.elegoo.com
  2. //2016.12.9
  3. // Atiriya Iyer
  4.  
  5. #include <SimpleDHT.h>
  6.  
  7. // for DHT11,
  8. // VCC: 5V or 3V
  9. // GND: GND
  10. // DATA: 2
  11. int pinDHT11 = 7;
  12. SimpleDHT11 dht11;
  13. const int pinLEDblue= 9;
  14. const int pinLEDgreen= 10;
  15. const int pinLEDred= 11;
  16.  
  17. void setup() {
  18. Serial.begin(9600);
  19. pinMode (pinLEDblue, OUTPUT);
  20. pinMode (pinLEDgreen, OUTPUT);
  21. pinMode (pinLEDred, OUTPUT);
  22.  
  23. analogWrite (pinLEDblue, 255);
  24. delay(500);
  25. analogWrite (pinLEDblue, 0);
  26. delay(500);
  27. analogWrite (pinLEDgreen, 255);
  28. delay(500);
  29. analogWrite (pinLEDgreen, 0);
  30. delay(500);
  31. analogWrite (pinLEDred, 255);
  32. delay(500);
  33. analogWrite (pinLEDred, 0);
  34. }
  35.  
  36. void loop() {
  37. // start working...
  38. Serial.println("=================================");
  39. Serial.println("Sample DHT11...");
  40.  
  41. // read with raw sample data.
  42. byte temperature = 0;
  43. byte humidity = 0;
  44. byte data[40] = {0};
  45. if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
  46. Serial.print("Read DHT11 failed");
  47. return;
  48. }
  49.  
  50. Serial.print("Sample RAW Bits: ");
  51. for (int i = 0; i < 40; i++) {
  52. Serial.print((int)data[i]);
  53. if (i > 0 && ((i + 1) % 4) == 0) {
  54. Serial.print(' ');
  55. }
  56. }
  57. Serial.println("");
  58.  
  59. Serial.print("Sample OK: ");
  60. Serial.print((int)temperature); Serial.print(" *C, ");
  61. Serial.print((int)humidity); Serial.println(" %");
  62.  
  63. // DHT11 sampling rate is 1HZ.
  64. delay(1000);
  65.  
  66. if (humidity <= 70)
  67. {
  68. analogWrite (pinLEDred, 200);
  69. delay (1000);
  70. analogWrite (pinLEDred, 0);
  71. }
  72.  
  73. if (humidity >= 70)
  74. {
  75. analogWrite (pinLEDgreen, 200);
  76. delay (1000);
  77. analogWrite (pinLEDgreen, 0);
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement