Advertisement
Guest User

Arduino LCD + Humidity Sensor

a guest
Apr 21st, 2014
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. // original humidity sensor code taken from: http://playground.arduino.cc/main/DHT11Lib
  2.  
  3. //Celsius to Fahrenheit conversion
  4. double Fahrenheit(double celsius)
  5. {
  6.     return 1.8 * celsius + 32;
  7. }
  8.  
  9. //Celsius to Kelvin conversion
  10. double Kelvin(double celsius)
  11. {
  12.     return celsius + 273.15;
  13. }
  14.  
  15. // dewPoint function NOAA
  16. // reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
  17. // reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
  18. //
  19. double dewPoint(double celsius, double humidity)
  20. {
  21.     // (1) Saturation Vapor Pressure = ESGG(T)
  22.     double RATIO = 373.15 / (273.15 + celsius);
  23.     double RHS = -7.90298 * (RATIO - 1);
  24.     RHS += 5.02808 * log10(RATIO);
  25.     RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
  26.     RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  27.     RHS += log10(1013.246);
  28.  
  29.         // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  30.     double VP = pow(10, RHS - 3) * humidity;
  31.  
  32.         // (2) DEWPOINT = F(Vapor Pressure)
  33.     double T = log(VP/0.61078);   // temp var
  34.     return (241.88 * T) / (17.558 - T);
  35. }
  36.  
  37. // delta max = 0.6544 wrt dewPoint()
  38. // 6.9 x faster than dewPoint()
  39. // reference: http://en.wikipedia.org/wiki/Dew_point
  40. double dewPointFast(double celsius, double humidity)
  41. {
  42.     double a = 17.271;
  43.     double b = 237.7;
  44.     double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
  45.     double Td = (b * temp) / (a - temp);
  46.     return Td;
  47. }
  48.  
  49.  
  50. #include <dht11.h>
  51.  
  52. // library required for the LCD:
  53. #include <LiquidCrystal.h>
  54.  
  55. dht11 DHT11;
  56.  
  57. // pin 7 for the humidity sensor
  58. #define DHT11PIN 7
  59.  
  60. // these pins are used by the LCD
  61. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  62.  
  63. void setup()
  64. {
  65.   Serial.begin(9600);
  66.   Serial.println("DHT11 TEST PROGRAM ");
  67.   Serial.print("LIBRARY VERSION: ");
  68.   Serial.println(DHT11LIB_VERSION);
  69.   Serial.println();
  70.  
  71.   // set up the LCD's number of columns and rows:
  72.   lcd.begin(16, 2);
  73. }
  74.  
  75. void loop()
  76. {
  77.   Serial.println("\n");
  78.  
  79.   int chk = DHT11.read(DHT11PIN);
  80.  
  81.   Serial.print("Read sensor: ");
  82.   switch (chk)
  83.   {
  84.     case DHTLIB_OK:
  85.         Serial.println("OK");
  86.         break;
  87.     case DHTLIB_ERROR_CHECKSUM:
  88.         Serial.println("Checksum error");
  89.         break;
  90.     case DHTLIB_ERROR_TIMEOUT:
  91.         Serial.println("Time out error");
  92.         break;
  93.     default:
  94.         Serial.println("Unknown error");
  95.         break;
  96.   }
  97.  
  98.   Serial.print("Humidity (%): ");
  99.   Serial.println((float)DHT11.humidity, 2);
  100.  
  101.   Serial.print("Temperature (°C): ");
  102.   Serial.println((float)DHT11.temperature, 2);
  103.  
  104.   Serial.print("Temperature (°F): ");
  105.   Serial.println(Fahrenheit(DHT11.temperature), 2);
  106.  
  107.   Serial.print("Temperature (°K): ");
  108.   Serial.println(Kelvin(DHT11.temperature), 2);
  109.  
  110.   Serial.print("Dew Point (°C): ");
  111.   Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
  112.  
  113.   Serial.print("Dew PointFast (°C): ");
  114.   Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
  115.  
  116.   // set the cursor to column 0, line 0
  117.   lcd.setCursor(0, 0);
  118.   lcd.print("Humidity (%): ");
  119.   lcd.print(DHT11.humidity);
  120.  
  121.   // set the cursor to column 0, line 1
  122.   // (note: line 1 is the second row, since counting begins with 0):
  123.   lcd.setCursor(0, 1);
  124.   lcd.print("Temp. ('C): ");
  125.   lcd.print(DHT11.temperature);
  126.  
  127.   delay(500);
  128. }
  129. //
  130. // END OF FILE
  131. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement