c5fb8dab53dcc53012412a15c5e76f5c06e25055
[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
72 #include <Adafruit_GFX.h>
73 #include <Adafruit_PCD8544.h>
74
75 // pin 7 - Serial clock out (SCLK)
76 // pin 6 - Serial data out (DIN)
77 // pin 5 - Data/Command select (D/C)
78 // pin 4 - LCD chip select (CS)
79 // pin 3 - LCD reset (RST)
80 Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
81
82 #define Backlight_Pin 9
83
84 void setup()
85 {
86   Serial.begin(9600);
87   Serial.println("DHT11 DS18B20 temperature");
88   Serial.print("DHT11 LIBRARY VERSION: ");
89   Serial.println(DHT11LIB_VERSION);
90   Serial.println();
91
92   // DS18B20
93   sensors.begin();
94   
95   // Nokia 5110 display
96   display.begin();
97
98   // you can change the contrast around to adapt the display
99   // for the best viewing!
100   display.setContrast(50);
101
102   pinMode(Backlight_Pin, OUTPUT);
103
104   pinMode(0, INPUT);
105   Serial.print("Backlight ");
106   Serial.println(analogRead(0));
107
108   display.clearDisplay();
109   display.setTextSize(1);
110   display.setTextColor(BLACK);
111   display.setCursor(0,0);
112   display.println("Ready");
113   display.display();
114 }
115
116 float dht11_temperature = 0;
117 float dht11_humidity = 0;
118 float ds18b20_temperature = 0;
119
120 #define TEMP_SIZE LCDWIDTH
121 float temp[TEMP_SIZE] = {0.0};
122 int temp_pos = 0; // position in circular buffer above
123
124 void loop()
125 {
126   Serial.println("\n");
127
128   int chk = DHT11.read(DHT11PIN);
129
130   Serial.print("DHT11 Read sensor: ");
131   switch (chk)
132   {
133     case DHTLIB_OK: 
134                 Serial.println("OK"); 
135                 break;
136     case DHTLIB_ERROR_CHECKSUM: 
137                 Serial.println("Checksum error"); 
138                 break;
139     case DHTLIB_ERROR_TIMEOUT: 
140                 Serial.println("Time out error"); 
141                 break;
142     default: 
143                 Serial.println("Unknown error"); 
144                 break;
145   }
146
147   dht11_humidity = (float)DHT11.humidity;
148   Serial.print("Humidity (%): ");
149   Serial.println(dht11_humidity, 2);
150
151   dht11_temperature = (float)DHT11.temperature;
152   Serial.print("Temperature (oC): ");
153   Serial.println(dht11_temperature, 2);
154
155   Serial.print("Temperature (oF): ");
156   Serial.println(Fahrenheit(DHT11.temperature), 2);
157
158   Serial.print("Temperature (K): ");
159   Serial.println(Kelvin(DHT11.temperature), 2);
160
161   Serial.print("Dew Point (oC): ");
162   Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
163
164   Serial.print("Dew PointFast (oC): ");
165   Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
166
167
168   Serial.print("DS18B20 Requesting temperatures...");
169   sensors.requestTemperatures(); // Send the command to get temperatures
170   Serial.println("DONE");
171
172   sensors.requestTemperatures(); // Send the command to get temperatures
173   Serial.print("Temperature for the device 1 (index 0) is: ");
174   ds18b20_temperature = sensors.getTempCByIndex(0);
175   Serial.println(ds18b20_temperature);  
176
177   temp[temp_pos] = ds18b20_temperature;
178
179   display.clearDisplay();
180   display.setCursor(0,0);
181   display.print(dht11_temperature, 0);
182   display.print("C ");
183   display.print(dht11_humidity, 0);
184   display.print("% ");
185   display.print(ds18b20_temperature, 2);
186   display.print("C");
187
188   float min = temp[0], max = temp[0];
189   
190   for(int i = 0; i < TEMP_SIZE; i++) {
191 //    Serial.print(temp[i]);
192 //    Serial.print(" ");
193     if (temp[i] < min && temp[i] > 0) min = temp[i];
194     if (temp[i] > max) max = temp[i];
195   }
196   Serial.println();
197   
198   Serial.print("temperature range ");
199   Serial.print(min);
200   Serial.print("-");
201   Serial.println(max);
202
203   // draw right to left so most recent value is on the right
204   for(int x = TEMP_SIZE - 1; x >= 0; x--) {
205     int pos = ( x + temp_pos + 1 ) % TEMP_SIZE;
206     if ( temp[pos] > 0 ) {
207       int y = ( ( temp[pos] - min ) / ( max - min ) ) * ( LCDHEIGHT - 10 );
208       display.drawLine(x, LCDHEIGHT - y, x, LCDHEIGHT, BLACK);
209 //      display.drawPixel(x,y + 10, BLACK);
210 //      Serial.print(temp[pos],2);
211 //      Serial.print(" ");
212     }
213   }
214   Serial.println();
215
216   // refresh LCD
217   display.display();
218
219   // pulse display backlight
220   int backlight = 0;
221
222   float old_temp = temp[(temp_pos + TEMP_SIZE - 1) % TEMP_SIZE];
223   if ( ds18b20_temperature < old_temp ) {
224     backlight = 32;
225   } else if ( ds18b20_temperature > old_temp ) {
226     backlight = 255;
227   }
228   analogWrite(Backlight_Pin, backlight);
229
230   delay(2000);
231
232   // move slot in circular bugger
233   if ( ++temp_pos > TEMP_SIZE ) temp_pos = 0;
234
235 }
236 //
237 // END OF FILE
238 //