compare temperature from DHT11 and DS18B20
[Arduino] / DHT11_DS18B20_temperature / DHT11_DS18B20_temperature.ino
1 // 
2 //   FILE:  dht11_test1.pde
3 // PURPOSE: DHT11 library test sketch for Arduino
4 //
5
6 //Celsius to Fahrenheit conversion
7 double Fahrenheit(double celsius)
8 {
9         return 1.8 * celsius + 32;
10 }
11
12 // fast integer version with rounding
13 //int Celcius2Fahrenheit(int celcius)
14 //{
15 //  return (celsius * 18 + 5)/10 + 32;
16 //}
17
18
19 //Celsius to Kelvin conversion
20 double Kelvin(double celsius)
21 {
22         return celsius + 273.15;
23 }
24
25 // dewPoint function NOAA
26 // reference: http://wahiduddin.net/calc/density_algorithms.htm 
27 double dewPoint(double celsius, double humidity)
28 {
29         double RATIO = 373.15 / (273.15 + celsius);  // RATIO was originally named A0, possibly confusing in Arduino context
30         double SUM = -7.90298 * (RATIO - 1);
31         SUM += 5.02808 * log10(RATIO);
32         SUM += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
33         SUM += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
34         SUM += log10(1013.246);
35         double VP = pow(10, SUM - 3) * humidity;
36         double T = log(VP/0.61078);   // temp var
37         return (241.88 * T) / (17.558 - T);
38 }
39
40 // delta max = 0.6544 wrt dewPoint()
41 // 5x faster than dewPoint()
42 // reference: http://en.wikipedia.org/wiki/Dew_point
43 double dewPointFast(double celsius, double humidity)
44 {
45         double a = 17.271;
46         double b = 237.7;
47         double temp = (a * celsius) / (b + celsius) + log(humidity/100);
48         double Td = (b * temp) / (a - temp);
49         return Td;
50 }
51
52
53 #include <dht11.h>
54
55 dht11 DHT11;
56
57 #define DHT11PIN 2
58
59 #include <OneWire.h>
60 #include <DallasTemperature.h>
61
62 // Data wire is plugged into port 2 on the Arduino
63 #define ONE_WIRE_BUS 10
64
65 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
66 OneWire oneWire(ONE_WIRE_BUS);
67
68 // Pass our oneWire reference to Dallas Temperature. 
69 DallasTemperature sensors(&oneWire);
70
71 void setup()
72 {
73   Serial.begin(9600);
74   Serial.println("DHT11 TEST PROGRAM ");
75   Serial.print("LIBRARY VERSION: ");
76   Serial.println(DHT11LIB_VERSION);
77   Serial.println();
78   sensors.begin();
79 }
80
81 void loop()
82 {
83   Serial.println("\n");
84
85   int chk = DHT11.read(DHT11PIN);
86
87   Serial.print("DHT11 Read sensor: ");
88   switch (chk)
89   {
90     case DHTLIB_OK: 
91                 Serial.println("OK"); 
92                 break;
93     case DHTLIB_ERROR_CHECKSUM: 
94                 Serial.println("Checksum error"); 
95                 break;
96     case DHTLIB_ERROR_TIMEOUT: 
97                 Serial.println("Time out error"); 
98                 break;
99     default: 
100                 Serial.println("Unknown error"); 
101                 break;
102   }
103
104   Serial.print("Humidity (%): ");
105   Serial.println((float)DHT11.humidity, 2);
106
107   Serial.print("Temperature (oC): ");
108   Serial.println((float)DHT11.temperature, 2);
109
110   Serial.print("Temperature (oF): ");
111   Serial.println(Fahrenheit(DHT11.temperature), 2);
112
113   Serial.print("Temperature (K): ");
114   Serial.println(Kelvin(DHT11.temperature), 2);
115
116   Serial.print("Dew Point (oC): ");
117   Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
118
119   Serial.print("Dew PointFast (oC): ");
120   Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
121
122
123   Serial.print("DS18B20 Requesting temperatures...");
124   sensors.requestTemperatures(); // Send the command to get temperatures
125   Serial.println("DONE");
126
127   sensors.requestTemperatures(); // Send the command to get temperatures
128   Serial.print("Temperature for the device 1 (index 0) is: ");
129   Serial.println(sensors.getTempCByIndex(0));  
130   
131   delay(2000);
132 }
133 //
134 // END OF FILE
135 //