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);
82 #define Backlight_Pin 9
87 Serial.println("DHT11 DS18B20 temperature");
88 Serial.print("DHT11 LIBRARY VERSION: ");
89 Serial.println(DHT11LIB_VERSION);
98 // you can change the contrast around to adapt the display
99 // for the best viewing!
100 display.setContrast(50);
102 pinMode(Backlight_Pin, OUTPUT);
105 Serial.print("Backlight ");
106 Serial.println(analogRead(0));
108 display.clearDisplay();
109 display.setTextSize(1);
110 display.setTextColor(BLACK);
111 display.setCursor(0,0);
112 display.println("Ready");
116 float dht11_temperature = 0;
117 float dht11_humidity = 0;
118 float ds18b20_temperature = 0;
120 #define TEMP_SIZE LCDWIDTH
121 float temp[TEMP_SIZE] = {0.0};
122 int temp_pos = 0; // position in circular buffer above
126 Serial.println("\n");
128 int chk = DHT11.read(DHT11PIN);
130 Serial.print("DHT11 Read sensor: ");
134 Serial.println("OK");
136 case DHTLIB_ERROR_CHECKSUM:
137 Serial.println("Checksum error");
139 case DHTLIB_ERROR_TIMEOUT:
140 Serial.println("Time out error");
143 Serial.println("Unknown error");
147 dht11_humidity = (float)DHT11.humidity;
148 Serial.print("Humidity (%): ");
149 Serial.println(dht11_humidity, 2);
151 dht11_temperature = (float)DHT11.temperature;
152 Serial.print("Temperature (oC): ");
153 Serial.println(dht11_temperature, 2);
155 Serial.print("Temperature (oF): ");
156 Serial.println(Fahrenheit(DHT11.temperature), 2);
158 Serial.print("Temperature (K): ");
159 Serial.println(Kelvin(DHT11.temperature), 2);
161 Serial.print("Dew Point (oC): ");
162 Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
164 Serial.print("Dew PointFast (oC): ");
165 Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
168 Serial.print("DS18B20 Requesting temperatures...");
169 sensors.requestTemperatures(); // Send the command to get temperatures
170 Serial.println("DONE");
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);
177 temp[temp_pos] = ds18b20_temperature;
179 display.clearDisplay();
180 display.setCursor(0,0);
181 display.print(dht11_temperature, 0);
183 display.print(dht11_humidity, 0);
185 display.print(ds18b20_temperature, 2);
188 float min = temp[0], max = temp[0];
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];
198 Serial.print("temperature range ");
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(" ");
219 // pulse display backlight
222 float old_temp = temp[(temp_pos + TEMP_SIZE - 1) % TEMP_SIZE];
223 if ( ds18b20_temperature < old_temp ) {
225 } else if ( ds18b20_temperature > old_temp ) {
228 analogWrite(Backlight_Pin, backlight);
232 // move slot in circular bugger
233 if ( ++temp_pos > TEMP_SIZE ) temp_pos = 0;