Author Options:

Timer,Temprature & Humidity Based Relay Switching with Arduino Mega.? Answered

I am making a Climate Controler for my Green House. Please help me about Arduino code for Four Relay operation.
relay1 will be high when Temp will rise higher then Temprature value defined and will low when reach under defined value.
relay2 will be high when Humidity will rise higher then Humidity value defined and will low when reach under defined value 
relay3 will be high for 10 Minutes in every 120 Minutes.
relay4 will be high for 10 Minutes in every 120 Minutes. 

I am Using DHT11 sensor with Mega2560 Board.
Currently using this code.

Its working for Temp & Humidty based Relay operation only. Need to add two relay with Timer based operation.

System needs to check Humidity and Temprature value in every 10 minutes.

Please Help. 
I am very new for Arduino and making this for my personal use.

#include <dht11.h>
#include <Wire.h>
#include <LiquidCrystal.h>

/*-----( 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 lcd(20, 19, 18, 17, 16, 15); // Set the LCD I2C address
dht11 DHT11;

/*-----( Declare Constants, Pin Numbers )-----*/
#define DHT11PIN 22
#define RELAY1  6                       
#define RELAY2  7                       
#define RELAY3  4                       
#define RELAY4  5
#define TempTarget 30
#define HUMTarget 50

void setup()   /*----( SETUP: RUNS ONCE )----*/
  Serial.begin(9600); //(Remove all 'Serial' commands if not needed)
  lcd.begin(20,2);         // initialize the lcd for 20 chars 4 lines, turn on backlight
    // Print a message to the LCD.
  //lcd.setCursor(0, 1);
  lcd.print("SHOURYA's GARDEN");
  // Initialise the Arduino data pins for OUTPUT
  pinMode(RELAY1, OUTPUT);      
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  }/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
  int chk = DHT11.read(DHT11PIN);
  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;
  lcd.setCursor(0, 1);

  lcd.print((float)DHT11.temperature, 0);
  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);

  //lcd.print(" F=");
  //lcd.print(Fahrenheit(DHT11.temperature), 0);
  //Serial.print("Temperature (oF): ");
  //Serial.println(Fahrenheit(DHT11.temperature), 2);

  lcd.print(" Hum=");
  lcd.print((float)DHT11.humidity, 0);
  Serial.print("Humid(%): ");
  Serial.println((float)DHT11.humidity, 2); 

  //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));

   if (DHT11.temperature > TempTarget)
Serial.println("Turning on Exaust Fan");
//lcd.print("Turning on Exaust Fan");
digitalWrite (RELAY1, HIGH);
     if (DHT11.temperature < TempTarget)
Serial.println("Turning Off Exaust Fan");
//lcd.print("Turning off Exaust Fan");
digitalWrite (RELAY1, LOW);

    if (DHT11.humidity < HUMTarget)
Serial.println("Turning on HUMIDIFIER");
//lcd.print("Turning on HUMIDIFIER");
digitalWrite (RELAY2, HIGH);
     if (DHT11.humidity > HUMTarget)
Serial.println("Turning Off HUMIDIFIER");
//lcd.print("Turning off HUMIDIFIER");
digitalWrite (RELAY2, LOW);

   digitalWrite(RELAY3,HIGH);           // Turns ON Relays 3
   digitalWrite(RELAY4,HIGH);           // Turns ON Relays 4
   digitalWrite(RELAY3,LOW);          // Turns Relay Off
   digitalWrite(RELAY4,LOW);          // Turns Relay Off  
}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
        return 1.8 * celsius + 32;

//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;

/* ( THE END ) */


The forums are retiring in 2021 and are now closed for new topics and comments.

Best Answer 5 years ago

First another note - right now you have temperature and humidity comparisions with "greater than" and "less than" operators. But the program skips the part, including delay, if the measurement and setting are equal. In such situations use "grater or equal than" or "less or equal than" in place of one of them (but not both at the same time).

The easy and not super precise method to get timing based operation is to make the loop cycle the same length every time (for example one second or one minute) and count how many loops have passed since some operation was started. (Put a delay only in the end of the loop and nowhere else.)

For example: you declare a counter variable and set it to 10 (and you make your loop have delay of 1minute = 60000ms). In each loop you check if the counter is greater than 0 - if it is you substract 1 and do nothing more. If it's 0 then 10 minutes have passed - you make measurements and set the counter back to 10.

Another counter variable can count down the 120 minutes. You can make a relay turn on when this variable is between 0 and 10 and off on all other cases.

Hope this helps.


Answer 5 years ago

Thanks. Alot for your reply. the programe i uploaded was based on a similer program i found on Instructable. So mainly i copied and did minor changes as per my requirments. i understands what you guided about timer through loop. but as i am not an arduino programmer so it will take long time to execute it as propper programming language. I am searching such examples and trying to make programme as you guided. If you can write this in programming languge that will be very helpfull. Otherwise you guidance is still very usefull, and sure i am working on that.


5 years ago

Relay 3 & relay 4 for Timing based Operation is not yet programmed. Because with only simple delay comand the whole Loop stops for that delay. i wants to run loop(Sensing Humidity & Temprature and trigger related relay 1 & 2 accordingly)) in every 5 or 10 minutes. And Relay 4 and 5 should tigger in every 120 min for 10 min on (Relay 4 & 5 On 10 min and off 120 min). What will be the appropriate comand for relay 4 & 5 without disturbing the loop repeat cycle of 5 or 10 min.