Created
December 14, 2019 20:11
-
-
Save pknowledge/f2d5ce59d236320ecccbab80e6f53fa1 to your computer and use it in GitHub Desktop.
How to create WEATHER BOX USING ARDUINO using DHT22 Temperature and Humidity Sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DHT.h" | |
#include <Wire.h> | |
#include <LiquidCrystal_I2C.h> | |
// Define button pins | |
#define Toggle_Up 2 | |
#define Maxdisplay 3 | |
#define Set 4 | |
// Define led pins | |
#define Led_1 5 | |
#define Led_2 6 | |
// Define ON/OFF Relay state | |
// With Relay module and connect VCC-VCC jumper as the video | |
// We will trigger Relay by low state | |
#define ON LOW | |
#define OFF HIGH | |
// Define Relay pins, I assum these two pins will be connected to Heater and Fan | |
// In the video i used 220AC bulb to demo | |
#define Heater 8 //Relay2 | |
#define Fan 9 //Relay1 | |
// Define DHT pins and DHT type | |
#define DHTPIN A0 | |
#define DHTTYPE DHT22 | |
// Define (C) or (F) degree type | |
#define C_Mode true | |
#define F_Mode false | |
// Default mode is (C) degree | |
boolean Mode = C_Mode; | |
// Max degree = 30 | |
float MaxtempC = 30; | |
float MaxtempF = MaxtempC * 1.8 + 32; // Convert to *F | |
// Min degree = 25; | |
float MintempC = 25; | |
float MintempF = MintempC * 1.8 + 32; // Convert to *F | |
// Change the address to appropriate for your LCD | |
LiquidCrystal_I2C lcd(0x3F, 16, 2); // I2C module address, 16 columns , 2 rows | |
DHT dht(DHTPIN, DHTTYPE); // Init DHT | |
void Init_button() { //Init button function | |
pinMode(Toggle_Up, INPUT_PULLUP); // Setup internal pull up resistor | |
pinMode(Maxdisplay, INPUT_PULLUP);// Setup internal pull up resistor | |
pinMode(Set, INPUT_PULLUP); // Setup internal pull up resistor | |
} | |
void Init_relay(){ //Init relay function | |
pinMode(Heater, OUTPUT); // Setup output | |
pinMode(Fan, OUTPUT); // Setup output | |
digitalWrite(Heater, OFF); // Don't trigger | |
digitalWrite(Fan, OFF); // Don't trigger | |
} | |
void Init_led(){ //Init led function | |
pinMode(Led_1, OUTPUT); // Setup output | |
pinMode(Led_2, OUTPUT); // Setup output | |
digitalWrite(Led_1, HIGH); // ON led 1 | |
digitalWrite(Led_2, LOW); // OFF led 2 | |
} | |
float Set_Value(String str){ // This function help you adjust the value of degree | |
static float ArrayofValue[4]; | |
int Test_case = 0; | |
int Input_number = 0; | |
int Finish_case = 4; | |
float Value = 0; | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print(str); | |
lcd.setCursor(5,1); | |
lcd.print("00.00"); | |
while(Test_case < Finish_case) { | |
if (!digitalRead(Maxdisplay)){ | |
while(!digitalRead(Maxdisplay)); | |
if (Finish_case == 4) { | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print(str); | |
lcd.setCursor(5,1); | |
lcd.print("000.00"); | |
Finish_case += 1; | |
Test_case = 0; | |
Input_number = 0; | |
for (int i = 0; i <= 4; i++) | |
ArrayofValue[i] = 0; | |
} | |
else if (Finish_case == 5) { | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print(str); | |
lcd.setCursor(5,1); | |
lcd.print("00.00"); | |
Finish_case -= 1; | |
Test_case = 0; | |
Input_number = 0; | |
for (int i = 0; i <= 4; i++) | |
ArrayofValue[i] = 0; | |
} | |
} | |
if (Test_case <= Finish_case - 3){ | |
lcd.setCursor(Test_case + 5, 1); | |
lcd.print(Input_number); | |
} | |
else{ | |
lcd.setCursor(Test_case + 6, 1); | |
lcd.print(Input_number); | |
} | |
if (!digitalRead(Toggle_Up)) { | |
while (!digitalRead(Toggle_Up)); | |
if (Input_number == 9) | |
Input_number = 0; | |
else | |
Input_number += 1; | |
} | |
else if (!digitalRead(Set)) { | |
while (!digitalRead(Set)); | |
ArrayofValue[Test_case] = Input_number; | |
Test_case += 1; | |
Input_number = 0; | |
} | |
} | |
for (int i = 0; i <= Finish_case - 1; i++){ | |
if (i <= Finish_case - 3) | |
Value += ArrayofValue[i] * pow(10, Finish_case - 3 - i); | |
else { | |
if (i == Finish_case - 2) | |
Value += ArrayofValue[Finish_case - 2] / 10.0; | |
else | |
Value += ArrayofValue[Finish_case - 1] / 100.0; | |
} | |
} | |
lcd.clear(); | |
lcd.print("Done!"); | |
delay(500); | |
lcd.clear(); | |
return Value; | |
} | |
void Set_MaxTemp_Value(){ // This function will invoke the function Set_Value() above to adjust degree1 | |
if (Mode == C_Mode){ | |
MaxtempC = Set_Value("Set Max Temp(C): "); | |
MaxtempF = MaxtempC*1.8 + 32; | |
} | |
else{ | |
MaxtempF = Set_Value("Set Max Temp(F): "); | |
MaxtempC = MaxtempF*0.555 - 32; | |
} | |
} | |
void Set_MinTemp_Value(){ // This function will invoke the function Set_Value() above to adjust degree1 | |
if (Mode == C_Mode){ | |
MintempC = Set_Value("Set Min Temp(C): "); | |
MintempF = MintempC*1.8 + 32; | |
} | |
else{ | |
MintempF = Set_Value("Set Min Temp(F): "); | |
MintempC = MintempF*0.555 - 32; | |
} | |
} | |
void setup() { | |
Serial.begin(9600); | |
Init_relay(); // Init Relay | |
Init_button(); // Init Button | |
Init_led(); // Init led | |
dht.begin(); // Init DHT | |
lcd.init(); // Init lcd | |
lcd.backlight(); // Turn on LCD's led | |
lcd.clear(); // Clear LCD screen | |
lcd.setCursor(0, 0); // Start LCD at column = 0 , row = 0 | |
} | |
void loop() { | |
float hum = dht.readHumidity(); // Get hum | |
float tempC = dht.readTemperature(); // Get C temp | |
float tempF = tempC*1.8 + 32; // Convert to F | |
digitalWrite(Heater, OFF); // Don't trigger Relay | |
digitalWrite(Fan, OFF); // Don't trigger Relay | |
digitalWrite(Led_1, HIGH); // ON led 1 | |
digitalWrite(Led_2, LOW); // OFF led 2 | |
// If temp is too high or too low | |
while ((tempC > MaxtempC) || (tempC < MintempC)) { | |
digitalWrite(Led_1, LOW); | |
digitalWrite(Led_2, HIGH); | |
lcd.clear(); | |
tempC = dht.readTemperature(); | |
// If temp is too high | |
if (tempC > MaxtempC) { | |
digitalWrite(Heater, OFF); | |
digitalWrite(Fan, ON); | |
lcd.setCursor(4,0); | |
lcd.print("Too Hot! "); | |
} | |
// If temp is too low | |
else if (tempC < MintempC){ | |
digitalWrite(Fan, OFF); | |
digitalWrite(Heater, ON); | |
lcd.setCursor(4,0); | |
lcd.print("Too Cold!"); | |
} | |
lcd.setCursor(2,1); | |
lcd.print("Temp: "); | |
lcd.print(tempC); | |
lcd.print((char)223); | |
lcd.print("C"); | |
delay(500); | |
lcd.noBacklight(); | |
delay(500); | |
lcd.backlight(); | |
delay(500); | |
lcd.clear(); | |
} | |
// If press change mode between C <-> F | |
if (!digitalRead(Toggle_Up)) { | |
while (!digitalRead(Toggle_Up)); | |
if (Mode == C_Mode) | |
Mode = F_Mode; | |
else Mode = C_Mode; | |
} | |
// If press setup Max Min temp | |
else if (!digitalRead(Maxdisplay)) { | |
while (!digitalRead(Maxdisplay)); | |
lcd.clear(); | |
if(Mode == C_Mode) { | |
lcd.setCursor(0,0); | |
lcd.print("MaxTemp:"); | |
lcd.print(MaxtempC); | |
lcd.print((char)223); | |
lcd.print("C"); | |
lcd.setCursor(0,1); | |
lcd.print("MinTemp:"); | |
lcd.print(MintempC); | |
lcd.print((char)223); | |
lcd.print("C"); | |
} | |
else { | |
lcd.setCursor(0,0); | |
lcd.print("MaxTemp:"); | |
lcd.print(MaxtempF); | |
lcd.print((char)223); | |
lcd.print("F"); | |
lcd.setCursor(0,1); | |
lcd.print("MinTemp:"); | |
lcd.print(MintempF); | |
lcd.print((char)223); | |
lcd.print("F"); | |
} | |
delay(5000); | |
lcd.clear(); | |
} | |
// If press Set | |
else if(!digitalRead(Set)) { | |
while (!digitalRead(Set)); | |
const bool Max_Mode = true; | |
const bool Min_Mode = false; | |
String str0 = "<="; | |
String str1 = ""; | |
bool Mode_Set = true; | |
lcd.clear(); | |
while (1){ | |
lcd.setCursor(0,0); | |
lcd.print("Set Max Temp"); | |
lcd.setCursor(14,0); | |
lcd.print(str0); | |
lcd.setCursor(0,1); | |
lcd.print("Set Min Temp"); | |
lcd.setCursor(14,1); | |
lcd.print(str1); | |
if(!digitalRead(Set)) { | |
while (!digitalRead(Set)); | |
if (Mode_Set == Max_Mode) | |
Set_MaxTemp_Value(); | |
else | |
Set_MinTemp_Value(); | |
lcd.clear(); | |
break; | |
} | |
if (!digitalRead(Toggle_Up)){ | |
while(!digitalRead(Toggle_Up)); | |
if (str0 == "<="){ | |
str0 = ""; | |
str1 = "<="; | |
Mode_Set = Min_Mode; | |
} | |
else { | |
str0 = "<="; | |
str1 = ""; | |
Mode_Set = Max_Mode; | |
} | |
lcd.clear(); | |
} | |
} | |
} | |
if (Mode == C_Mode) { | |
lcd.setCursor(0,0); | |
lcd.print("Temp: "); | |
lcd.print(tempC); | |
lcd.print((char)223); | |
lcd.print("C"); | |
} | |
else { | |
lcd.setCursor(0,0); | |
lcd.print("Temp: "); | |
lcd.print(tempF); | |
lcd.print((char)223); | |
lcd.print("F"); | |
} | |
lcd.setCursor(0,1); | |
lcd.print("Hum : "); | |
lcd.print(hum); | |
lcd.print("%"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment