Advertisement
pablo7890

DHT11 driver

Jun 8th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. /*
  2. DHT11Tester_ATtiny.ino
  3.  
  4.                     ATtiny 85
  5.                       _____
  6.               RESET -|     |- 5V
  7.  Software Serial Rx -|     |- DHT11 Pin (pullup resistor 4.7k)
  8.  Software Serial Tx -|     |-
  9.                 GND -|_____|-
  10.  
  11.  
  12.  
  13. This sketch demonstrates the usage of the DHT11 library on an ATtiny85. For details on wiring and information on possible compiler errors, see the link below.
  14. http://projectsfromtech.blogspot.com/2014/02/dht11-on-attiny85.html
  15.  
  16.  
  17.  
  18.  
  19.  
  20. Last edited: 2/8/2014
  21. Matthew
  22. http://projectsfromtech.blogspot.com/
  23.  
  24.  
  25. */
  26.  
  27. //Celsius to Fahrenheit conversion
  28. double Fahrenheit(double celsius)
  29. {
  30.   return 1.8 * celsius + 32;
  31. }
  32.  
  33. // fast integer version with rounding
  34. //int Celcius2Fahrenheit(int celcius)
  35. //{
  36. //  return (celsius * 18 + 5)/10 + 32;
  37. //}
  38.  
  39.  
  40. //Celsius to Kelvin conversion
  41. double Kelvin(double celsius)
  42. {
  43.   return celsius + 273.15;
  44. }
  45.  
  46. // dewPoint function NOAA
  47. // reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
  48. // reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
  49. //
  50. double dewPoint(double celsius, double humidity)
  51. {
  52.   // (1) Saturation Vapor Pressure = ESGG(T)
  53.   double RATIO = 373.15 / (273.15 + celsius);
  54.   double RHS = -7.90298 * (RATIO - 1);
  55.   RHS += 5.02808 * log10(RATIO);
  56.   RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
  57.   RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  58.   RHS += log10(1013.246);
  59.  
  60.   // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  61.   double VP = pow(10, RHS - 3) * humidity;
  62.  
  63.   // (2) DEWPOINT = F(Vapor Pressure)
  64.   double T = log(VP / 0.61078); // temp var
  65.   return (241.88 * T) / (17.558 - T);
  66. }
  67.  
  68. // delta max = 0.6544 wrt dewPoint()
  69. // 6.9 x faster than dewPoint()
  70. // reference: http://en.wikipedia.org/wiki/Dew_point
  71. double dewPointFast(double celsius, double humidity)
  72. {
  73.   double a = 17.271;
  74.   double b = 237.7;
  75.   double temp = (a * celsius) / (b + celsius) + log(humidity * 0.01);
  76.   double Td = (b * temp) / (a - temp);
  77.   return Td;
  78. }
  79.  
  80.  
  81. #include <dht11.h>
  82. #include <SoftwareSerial.h>
  83.  
  84. dht11 DHT11;
  85.  
  86. #define DHT11PIN 2
  87.  
  88.  
  89. SoftwareSerial mySerial(3, 4); // RX, TX
  90.  
  91.  
  92. void setup()
  93. {
  94.   mySerial.begin(9600);
  95.   mySerial.println("DHT11 TEST PROGRAM ");
  96.   mySerial.print("LIBRARY VERSION: ");
  97.   mySerial.println(DHT11LIB_VERSION);
  98.   mySerial.println();
  99. }
  100.  
  101. void loop()
  102. {
  103.   mySerial.println("\n");
  104.  
  105.   int chk = DHT11.read(DHT11PIN);
  106.  
  107.   //  mySerial.print("Read sensor: ");
  108.   switch (chk)
  109.   {
  110.     case DHTLIB_OK:
  111.       mySerial.println("OK");
  112.       break;
  113.     case DHTLIB_ERROR_CHECKSUM:
  114.       mySerial.println("Checksum error");
  115.       break;
  116.     case DHTLIB_ERROR_TIMEOUT:
  117.       mySerial.println("Time out error");
  118.       break;
  119.     default:
  120.       mySerial.println("Unknown error");
  121.       break;
  122.   }
  123.  
  124.   mySerial.print("Humidity (%): ");
  125.   mySerial.println((float)DHT11.humidity, 2);
  126.  
  127.   mySerial.print("Temperature (°C): ");
  128.   mySerial.println((float)DHT11.temperature, 2);
  129.  
  130.   mySerial.print("Temperature (°F): ");
  131.   mySerial.println(Fahrenheit(DHT11.temperature), 2);
  132.  
  133.   mySerial.print("Temperature (°K): ");
  134.   mySerial.println(Kelvin(DHT11.temperature), 2);
  135.  
  136.   mySerial.print("Dew Point (°C): ");
  137.   mySerial.println(dewPoint(DHT11.temperature, DHT11.humidity));
  138.  
  139.   mySerial.print("Dew PointFast (°C): ");
  140.   mySerial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
  141.  
  142.   delay(2000);
  143. }
  144. //
  145. // END OF FILE
  146. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement