2 // FILE: dht11_test1.pde
3 // PURPOSE: DHT11 library test sketch for Arduino
6 //Celsius to Fahrenheit conversion
7 double Fahrenheit(double celsius)
9 return 1.8 * celsius + 32;
12 // fast integer version with rounding
13 //int Celcius2Fahrenheit(int celcius)
15 // return (celsius * 18 + 5)/10 + 32;
19 //Celsius to Kelvin conversion
20 double Kelvin(double celsius)
22 return celsius + 273.15;
25 // dewPoint function NOAA
26 // reference: http://wahiduddin.net/calc/density_algorithms.htm
27 double dewPoint(double celsius, double humidity)
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);
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)
47 double temp = (a * celsius) / (b + celsius) + log(humidity/100);
48 double Td = (b * temp) / (a - temp);
60 #include <DallasTemperature.h>
62 // Data wire is plugged into port 2 on the Arduino
63 #define ONE_WIRE_BUS 10
65 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
66 OneWire oneWire(ONE_WIRE_BUS);
68 // Pass our oneWire reference to Dallas Temperature.
69 DallasTemperature sensors(&oneWire);
72 #include <Adafruit_GFX.h>
73 #include <Adafruit_PCD8544.h>
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);
86 Serial.println("DHT11 DS18B20 temperature");
87 Serial.print("DHT11 LIBRARY VERSION: ");
88 Serial.println(DHT11LIB_VERSION);
97 // you can change the contrast around to adapt the display
98 // for the best viewing!
99 display.setContrast(50);
101 display.clearDisplay();
102 display.setTextSize(1);
103 display.setTextColor(BLACK);
104 display.setCursor(0,0);
105 display.println("Ready");
109 float dht11_temperature = 0;
110 float dht11_humidity = 0;
111 float ds18b20_temperature = 0;
113 #define TEMP_SIZE LCDWIDTH
114 float temp[TEMP_SIZE] = {0.0};
115 int temp_pos = 0; // position in circular buffer above
119 Serial.println("\n");
121 int chk = DHT11.read(DHT11PIN);
123 Serial.print("DHT11 Read sensor: ");
127 Serial.println("OK");
129 case DHTLIB_ERROR_CHECKSUM:
130 Serial.println("Checksum error");
132 case DHTLIB_ERROR_TIMEOUT:
133 Serial.println("Time out error");
136 Serial.println("Unknown error");
140 dht11_humidity = (float)DHT11.humidity;
141 Serial.print("Humidity (%): ");
142 Serial.println(dht11_humidity, 2);
144 dht11_temperature = (float)DHT11.temperature;
145 Serial.print("Temperature (oC): ");
146 Serial.println(dht11_temperature, 2);
148 Serial.print("Temperature (oF): ");
149 Serial.println(Fahrenheit(DHT11.temperature), 2);
151 Serial.print("Temperature (K): ");
152 Serial.println(Kelvin(DHT11.temperature), 2);
154 Serial.print("Dew Point (oC): ");
155 Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
157 Serial.print("Dew PointFast (oC): ");
158 Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
161 Serial.print("DS18B20 Requesting temperatures...");
162 sensors.requestTemperatures(); // Send the command to get temperatures
163 Serial.println("DONE");
165 sensors.requestTemperatures(); // Send the command to get temperatures
166 Serial.print("Temperature for the device 1 (index 0) is: ");
167 ds18b20_temperature = sensors.getTempCByIndex(0);
168 Serial.println(ds18b20_temperature);
170 temp[temp_pos] = ds18b20_temperature;
172 display.clearDisplay();
173 display.setCursor(0,0);
174 display.print(dht11_temperature, 0);
176 display.print(dht11_humidity, 0);
178 display.print(ds18b20_temperature, 2);
181 float min = temp[0], max = temp[0];
183 for(int i = 0; i < TEMP_SIZE; i++) {
184 // Serial.print(temp[i]);
185 // Serial.print(" ");
186 if (temp[i] < min && temp[i] > 0) min = temp[i];
187 if (temp[i] > max) max = temp[i];
191 Serial.print("temperature range ");
196 // draw right to left so most recent value is on the right
197 for(int x = TEMP_SIZE - 1; x >= 0; x--) {
198 int pos = ( x + temp_pos + 1 ) % TEMP_SIZE;
199 if ( temp[pos] > 0 ) {
200 int y = ( ( temp[pos] - min ) / ( max - min ) ) * ( LCDHEIGHT - 10 );
201 display.drawLine(x, LCDHEIGHT - y, x, LCDHEIGHT, BLACK);
202 // display.drawPixel(x,y + 10, BLACK);
203 // Serial.print(temp[pos],2);
204 // Serial.print(" ");
212 // move slot in circular bugger
213 if ( ++temp_pos > TEMP_SIZE ) temp_pos = 0;