Advertisement
microrobotics

DHT11 Sensor

Aug 18th, 2017
5,499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | None | 0 0
  1. #include <dht11.h>
  2. dht11 DHT11;
  3.  
  4. #define DHT11PIN 2
  5.  
  6. void setup()
  7. {
  8.   Serial.begin(9600);
  9.   Serial.println("DHT11 TEST PROGRAM ");
  10.   Serial.print("LIBRARY VERSION: ");
  11.   Serial.println(DHT11LIB_VERSION);
  12.   Serial.println();
  13. }
  14.  
  15. void loop()
  16. {
  17.   Serial.println("\n");
  18.   int chk = DHT11.read(DHT11PIN);
  19.   Serial.print("Read sensor: ");
  20.   switch (chk)
  21.   {
  22.     case DHTLIB_OK:
  23.                 Serial.println("OK");
  24.                 break;
  25.     case DHTLIB_ERROR_CHECKSUM:
  26.                 Serial.println("Checksum error");
  27.                 break;
  28.     case DHTLIB_ERROR_TIMEOUT:
  29.                 Serial.println("Time out error");
  30.                 break;
  31.     default:
  32.                 Serial.println("Unknown error");
  33.                 break;
  34.   }
  35.  
  36.   Serial.print("Humidity (%): ");
  37.   Serial.println((float)DHT11.humidity, 2);
  38.  
  39.   Serial.print("Temperature (oC): ");
  40.   Serial.println((float)DHT11.temperature, 2);
  41.  
  42.   Serial.print("Temperature (oF): ");
  43.   Serial.println(Fahrenheit(DHT11.temperature), 2);
  44.  
  45.   Serial.print("Temperature (K): ");
  46.   Serial.println(Kelvin(DHT11.temperature), 2);
  47.  
  48.   Serial.print("Dew Point (oC): ");
  49.   Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
  50.  
  51.   Serial.print("Dew PointFast (oC): ");
  52.   Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
  53.  
  54.   delay(2000);
  55. }
  56.  
  57. double Fahrenheit(double celsius)
  58. {
  59.         return 1.8 * celsius + 32;
  60. }  
  61.  
  62. double Kelvin(double celsius)
  63. {
  64.         return celsius + 273.15;
  65. }    
  66.  
  67. double dewPoint(double celsius, double humidity)
  68. {
  69.         double A0= 373.15/(273.15 + celsius);
  70.         double SUM = -7.90298 * (A0-1);
  71.         SUM += 5.02808 * log10(A0);
  72.         SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
  73.         SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
  74.         SUM += log10(1013.246);
  75.         double VP = pow(10, SUM-3) * humidity;
  76.         double T = log(VP/0.61078);   // temp var
  77.         return (241.88 * T) / (17.558-T);
  78. }
  79.  
  80. double dewPointFast(double celsius, double humidity)
  81. {
  82.         double a = 17.271;
  83.         double b = 237.7;
  84.         double temp = (a * celsius) / (b + celsius) + log(humidity/100);
  85.         double Td = (b * temp) / (a - temp);
  86.         return Td;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement