/*
RedTar4
Bobs_DewPoint_LCD_meter_DHT22_rev7
10/18/18 - 3/10/19
*/
// Library section
#include <LiquidCrystal.h> // LCD library
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // (rs,e,d4,d5,d6,d7),initialize the LCD with the numbers of the interface pins
#include <DHT.h> // DHT22 library, delete any "DHT.U" files from library,or it wont run
// Definition section
#define DHTPIN 2 // what arduino pin we're connected to, arduino pin D2
DHT dht(DHTPIN, DHT22); // initialize DHT sensor and create sensor objects
#define DHTTYPE DHT22 // create DHT 22 (AM2302), AM2321 object
// pin assignment section
int alarmLed = 3; // assigns alarmLed int to arduino pin D3
void setup()
{
// initilize and start systems
Serial.begin(9600); // sets up serial comm's to 9600 baud
dht.begin(); // initialize the DHT22
lcd.begin(16, 2); // initialize the LCD's number of columns and rows
// display and serial print initilization
Serial.println("DHTxx test!");
lcd.setCursor(0, 1); lcd.print("DHTxx test!");
delay(1500);
// output pin setup section
pinMode(alarmLed, OUTPUT); // assigns alarmLed (arduino pin 3) as an output
digitalWrite(alarmLed, LOW); // sets output alarmLed (arduino pin 3) low
}
void loop()
{
// Reading temperature and humidity takes about 250 milliseconds
float H = dht.readHumidity(); // read humitity as %f
float C = dht.readTemperature(); // read temperature as Celsius (default)
float F = dht.readTemperature(true); // read temperature as Fahrenheit (if true)
// DHT22 data error check section
if (isnan(H) || isnan(C) || isnan(F))
{
Serial.println("Failed to read from DHT sensor!");
lcd.setCursor(0, 0); lcd.print(" ");
lcd.setCursor(0, 0); lcd.print(" Failed to read ");
lcd.setCursor(0, 1); lcd.print(" ");
lcd.setCursor(0, 1); lcd.print("DHT sensor Fail!");
return; // returns to start of loop
}
// calculations and conversions section
float HiF = dht.computeHeatIndex(F, H); // read and compute heat index in Fahrenheit (the default)
float DewPoint = (C - (100 - H) / 5); // dewpoint calculation using Celsius value
float DP = (DewPoint * 9 / 5) + 32; // converts dewPoint calculation to fahrenheit
// alarm output section
if ((F - 2) <= DP) // if the F value - 2 is <= to the DP value then this statement is true
{digitalWrite(alarmLed, HIGH);} // since statement is true set alarmLed (arduino pin D3) high
else // if statement is not true then
{digitalWrite(alarmLed, LOW);} // set alarmLed (arduino pin D3) low
// serial monitor and debug section
Serial.print("Sample OK: "); // prints sample ok to serial monitor
Serial.print((int)C); Serial.print(" *C, "); // prints celsius value to serial monitor and returns cursor
Serial.print((int)F); Serial.print(" *F, "); // prints fahrenheit value to serial monitor and returns cursor
Serial.print((int)H); Serial.print(" %, "); // prints humidity value to serial monitor and returns cursor
Serial.print((int)DP); Serial.print(" DP, "); // prints dewpoint value to serial monitor and returns cursor to next line
Serial.print((int)HiF); Serial.print(" Heat index, "); // prints humidity value to serial monitor and returns cursor
Serial.print((int)DewPoint); Serial.println(" DewP, "); // prints dewpoint value to serial monitor and returns cursor to next line
// LCD print section
lcd.setCursor(0, 0); lcd.print("Tmp");
lcd.setCursor(4, 0); lcd.print(F);
lcd.setCursor(6, 0); lcd.print((char)223); lcd.print("F"); // prints the degree's symbol
lcd.setCursor(8, 0); lcd.print("Dew");
lcd.setCursor(12, 0); lcd.print(DP);
lcd.setCursor(14, 0); lcd.print((char)223); lcd.print("F"); // prints the degree's symbol
lcd.setCursor(0, 1); lcd.print("Hum ");
lcd.setCursor(4, 1); lcd.print(H);
lcd.setCursor(6, 1); lcd.print(" % ");
lcd.setCursor(8, 1); lcd.print("HId");
lcd.setCursor(12, 1); lcd.print(HiF);
lcd.setCursor(14, 1); lcd.print((char)223); lcd.print("F"); // prints the degree's symbol
// The DHT22 sampling rate is .5HZ (.5 Sec), this delay sets program sample rate to match so no errors happen
delay(2000);
}
Comments
Please log in or sign up to comment.