

Arduino Code:
// MattEccles.me
// DHT11 Humidity and Temperature Sensor
// DS18B20 Waterproof Temperature Probe
// Displayed on I2C LCD Display
// Credits: Matthew Eccles
/*-----( Import Libraries )-----*/
#include <dht11.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
/*-----( Declare Constants, Pin Numbers )-----*/
#define ONE_WIRE_BUS 3
#define DHT11PIN 2
#define analogPin A0 //the thermistor attach to
#define beta 4090 //the beta of the thermistor
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
dht11 DHT11;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const int ledPin4 = 4; // pin that the LED is attached to
DeviceAddress insideThermometer = { 0x28, 0x94, 0xE2, 0xDF, 0x02, 0x00, 0x00, 0xFE };
DeviceAddress outsideThermometer = { 0x28, 0x6B, 0xDF, 0xDF, 0x02, 0x00, 0x00, 0xC0 };
DeviceAddress dogHouseThermometer = { 0x28, 0x59, 0xBE, 0xDF, 0x02, 0x00, 0x00, 0x9F };
/*----( SETUP: RUNS ONCE )----*/
void setup()
{
Serial.begin(9600); //(Remove all 'Serial' commands if not needed)
sensors.begin();
sensors.setResolution(insideThermometer, 10);
sensors.setResolution(outsideThermometer, 10);
sensors.setResolution(dogHouseThermometer, 10);
Serial.println("Dallas Temperature IC Control Library Demo");
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight
lcd.backlight();
pinMode(ledPin4, OUTPUT);
}
/*--(end setup )---*/
//void printTemperature(DeviceAddress deviceAddress)
//{
// float tempC = (sensors.getTempC(deviceAddress) * 9.0 / 5.0) + 32.0;
// float temperatureF = ((float)tempC * 9.0 / 5.0) + 32.0;
// Serial.print(temperatureF), 0; Serial.println(" degrees F");
// if (tempC == -127.00) {
// Serial.print("Error getting temperature");
// } else {
// Serial.print("C: ");
// Serial.print(tempC);
// }
//}
/*----( LOOP: RUNS CONSTANTLY )----*/
void loop()
{
int reading = analogRead(3);
float voltage = reading * 5.0;
Serial.print(voltage); Serial.println(" volts");
float temperatureC = (voltage - 0.5) * 100 ;
Serial.print(temperatureC);
Serial.println(" degrees C");
float tempC = (sensors.getTempCByIndex(0) * 9.0 / 5.0) + 32.0; //If wanting Celcius, remove "* 9.0 / 5.0) + 32.0"
float temperatureF = ((float)tempC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF), 0; Serial.println(" degrees F");
int chk = DHT11.read(DHT11PIN); //read thermistor value
long a =1023 - analogRead(analogPin);
if ((float)tempC < 73.00) { //This will make the LEN on Pin4 blink if the temp from the "Probe" is lower than given temp
digitalWrite(ledPin4, HIGH);
}
else {
digitalWrite(ledPin4,LOW);
}
// lcd.clear(); //--can be used to reset the screen
/*---- 1st Line of the LCD ----*/
lcd.setCursor(0, 0);
lcd.print("THUMPER BIO MONITOR");
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
Serial.print("Temperature for Device 1 is: ");
Serial.print(sensors.getTempCByIndex(0), 2); // This will print Celcius
// You can have more than one IC on the same bus.
// 0 refers to the first IC on the wire
Serial.print("Read sensor: ");
switch (chk)
{
case 0: Serial.println("OK"); break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}
/*----Used for Celcius ----*/
/*----lcd.print("C=");
lcd.print((float)DHT11.temperature, 0);
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);----*/
/*---- 2nd Line of the LCD ----*/
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print((float)DHT11.humidity, 0);
lcd.print("%");
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
/*---- 3rd Line of the LCD ----*/
lcd.setCursor(0, 2);
lcd.print("Air Temp: ");
lcd.print(Fahrenheit(DHT11.temperature), 2);
lcd.print((char)223); //degree
Serial.print("Temperature (oF): ");
Serial.println(Fahrenheit(DHT11.temperature), 2);
/*---- 4th Line of the LCD ----*/
lcd.setCursor(0, 3);
lcd.print("H20 Temp: ");
lcd.print((float)tempC), 0;
Serial.print(sensors.getTempCByIndex(0), 0);
Serial.print(DallasTemperature::toFahrenheit(tempC), 0);
lcd.print((char)223);
lcd.print(" ");
// --- Other Options --- //
/*----
Serial.print("Temperature (K): ");
Serial.println(Kelvin(DHT11.temperature), 2);
Serial.print("Dew Point (oC): ");
Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
Serial.print("Dew PointFast (oC): ");
Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
----*/
delay(250); // Delay used to allow the LED to stay on if needed for 250 miliseconds
digitalWrite(ledPin4,LOW);
delay(250); // Delay used to allow the LED to turn off and to allow time between the reading of the sensors
}
/* --(end main loop )-- */
/*-----( Declare User-written Functions )-----*/
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 31.0;
}
//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}
// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}
// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
No comments:
Post a Comment