tuxmartin

Arduino DHT22

Nov 15th, 2013
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. /*
  2. LIB: https://github.com/ringerc/Arduino-DHT22
  3. DHT22: http://dx.com/p/dht22-2302-digital-temperature-and-humidity-sensor-module-184847
  4. http://learn.adafruit.com/dht
  5. ZAPOJENI: http://www.manuel-esteban.com/wp-content/uploads/2013/03/Arduino-et-DHT22_bb.jpg
  6.  
  7. DHT22
  8. Low cost
  9. 3 to 5V power and I/O
  10. 2.5mA max current use during conversion (while requesting data)
  11. Good for 0-100% humidity readings with 2-5% accuracy
  12. Good for -40 to 125°C temperature readings ±0.5°C accuracy
  13. No more than 0.5 Hz sampling rate (once every 2 seconds)
  14. Body size 15.1mm x 25mm x 7.7mm
  15. 4 pins with 0.1" spacing
  16. */
  17. #include <DHT22.h>
  18.  
  19. // Connect a 4.7K resistor between VCC and the data pin (strong pullup)
  20. #define DHT22_PIN 8
  21.  
  22. // Setup a DHT22 instance
  23. DHT22 myDHT22(DHT22_PIN);
  24.  
  25. void setup(void)
  26. {
  27.   Serial.begin(9600);
  28. }
  29.  
  30. void loop(void)
  31. {
  32.   DHT22_ERROR_t errorCode;
  33.  
  34.   // The sensor can only be read from every 1-2s, and requires a minimum
  35.   // 2s warm-up after power-on.
  36.   delay(5000);
  37.  
  38.   //Serial.print("Requesting data...");
  39.   errorCode = myDHT22.readData();
  40.   switch(errorCode)
  41.   {
  42.     case DHT_ERROR_NONE:
  43.       //Serial.print("Got Data ");
  44.       Serial.print(myDHT22.getTemperatureC());
  45.       Serial.print("C ");
  46.       Serial.print(myDHT22.getHumidity());
  47.       Serial.println("%");
  48.       break;
  49.     case DHT_ERROR_CHECKSUM:
  50.       Serial.print("check_sum_error ");
  51.       Serial.print(myDHT22.getTemperatureC());
  52.       Serial.print("C ");
  53.       Serial.print(myDHT22.getHumidity());
  54.       Serial.println("%");
  55.       break;
  56.     case DHT_BUS_HUNG:
  57.       Serial.println("BUS_Hung");
  58.       break;
  59.     case DHT_ERROR_NOT_PRESENT:
  60.       Serial.println("Not_Present");
  61.       break;
  62.     case DHT_ERROR_ACK_TOO_LONG:
  63.       Serial.println("ACK_time_out");
  64.       break;
  65.     case DHT_ERROR_SYNC_TIMEOUT:
  66.       Serial.println("Sync_Timeout");
  67.       break;
  68.     case DHT_ERROR_DATA_TIMEOUT:
  69.       Serial.println("Data_Timeout");
  70.       break;
  71.     case DHT_ERROR_TOOQUICK:
  72.       Serial.println("Polled_to_quick");
  73.       break;
  74.   }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment