Help with temp/humidity sensor and relay control with RTC!

This is my first post here, and have been lurking a while. I just picked up the Arduino and have been trying to learn the language and such for a few months.

I’m in the process of building a device with:

RTC
2 Relays
SHT20 Temp and Humidity Sensor
16x2 LCD

THE GOAL will essentially have a heating pad and a humidifier plugged into each relay. The sensor will read the levels and control the relays based on parameters I have set and based on what time of the day it is. I’m trying to get roughly a 75% humidity and 29.5 C level from 8am to 8pm. Then at 8pm to 8am (overnight) I’d like the levels to change to 80% humidity and 24 C temp. I am also trying to implement a ‘deadzone’ so that the relays aren’t constantly cycling around the setpoint (hope that makes sense)

I’ve gone through several iterations of this sketch and am trying to finalize it. Maybe I’m over-complicating things.

// New Version June 15th
#include <Wire.h>
#include "DFRobot_SHT20.h"              // establish sensor library
#include "RTClib.h"                     // establish RTC library
#include <LiquidCrystal_I2C.h>          // establish LCD library
// #include "SD.h"                      // potential SD card use
 
DFRobot_SHT20    sht20;                 // Define SHT20 Temperature and Humidity Sensor
LiquidCrystal_I2C lcd(0x27,20,4);       // set the LCD address to 0x27 for a 16 chars and 2 line display
RTC_DS1307 RTC;                         // Define RTC
 
int humidityRelay = 7;                                      
int tempRelay = 8;
int hourOn1 = 8;
int minOn = 0;
int hourOn2 = 20;
int Secs;
int Minutes;
int Hours;
 
 
float RH = sht20.readHumidity();                        
float Temp = sht20.readTemperature();
 
float RHsetpoint;
float RHdeadzone;
float TempSetpoint;
float TempDeadzone;
 
String myTime;
 
#define Humidifier_ON LOW
#define Humidifier_OFF HIGH
#define Heater_ON   LOW
#define Heater_OFF HIGH
 
 
void setup() {
 
// SETUP RTC, SHT20, LCD Screen, and set Relay Pins and start reading the hardware
  Serial.begin(9600);
  Wire.begin();
 
  RTC.begin();                                        // initialize RTC 24H format
 
// Datalogger RTC
   if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
   
// SHT20
  sht20.initSHT20();                                  // Init SHT20 Sensor
  delay(100);                                         // Think about using this for potential wiggle room zone
  sht20.checkSHT20();                                 // Check SHT20 Sensor
 
// I2C LCD SCREEN VOID SETUP
  lcd.init();                                         // Initialize LCD
 
// Format Screen
  lcd.backlight();                                    // Fill Screen with contant variables
  lcd.setCursor(1,0);
  lcd.print("Time: ");
  lcd.setCursor(0,1);
  lcd.print("T:");
  lcd.setCursor(7,1);
  lcd.print("H:");
  lcd.setCursor(13,1);
  lcd.print("%");
   
// RELAY VOID SETUP
  pinMode(humidityRelay, OUTPUT);                    // Set relay pins as output
  pinMode(tempRelay, OUTPUT);
  //digitalWrite(humidityRelay, Humidifier_OFF);       // Turn relays off initially.  
  //digitalWrite(tempRelay, Heater_OFF);
}
 
 
 
void loop() {
                                                     // Main void loop which calls the RunEnvironment sub and establishes time parameters  
  DateTime now = RTC.now();
  Secs = now.second();
  Hours = now.hour();
  Minutes = now.minute();
  myTime = myTime + Hours +":"+ Minutes +":"+ Secs ;
 
  if(now.hour() >= hourOn1 && now.minute() == minOn)        // RunEnvironment with these settings at 8:00 AM              
  {
    RHsetpoint = 77;
    RHdeadzone = 7;
    TempSetpoint = 29.5;
    TempDeadzone = 2;
    RunEnvironment();
  }
 
  if(now.hour() == hourOn2 && now.minute() ==minOn)         // RunEnvironment with these settings at 8:00 PM
  {
    RHsetpoint = 80;
    RHdeadzone = 5;
    TempSetpoint = 24;
    TempDeadzone = 2;
    RunEnvironment();
  }
                                                            //  LCD SCREEN VOID LOOP                                                          
    lcd.setCursor(0,0);
    lcd.print(myTime);
    myTime = "";
    lcd.setCursor(2,1);                                     // Display Temperature and Humidity on LCD Screen
    lcd.print(Temp,1);
    lcd.setCursor(9,1);
    lcd.print(RH,1);
  delay(5000);;
 }
                                                         
 
void RunHumidity()                                        // Humidity sub routine
 {        
if (RH < (RHsetpoint-RHdeadzone))                         // compare humidity against the setpoint and deadzone and turn on relay
    {
    digitalWrite(humidityRelay, Humidifier_ON);
    }
 
if (RH >= RHsetpoint)                                    // if it is at the setpoint then the relay will turn off
    {
    digitalWrite(humidityRelay, Humidifier_OFF);
    }
}
 
void RunTemperature()                                       // Temperature sub routine
{        
if (Temp < (TempSetpoint-TempDeadzone))                       // compare temperature against the setpoint and deadzone and turn on relay
    {
    digitalWrite(tempRelay, Heater_ON);
    }
 
if (Temp >= TempSetpoint)                                  // once temperature is met the relay will turn off
    {
    digitalWrite(tempRelay, Heater_OFF);
    }
}
 
void RunEnvironment(){ RunTemperature(); RunHumidity(); }               // Humidity and Temperature sub routine combined

The main issues I’m running into now:

  • Using sub routines such as RunHumidity and RunTemperature and combining them into RunEnvironment. Right now they seem so close to being able to work but I don’t think they’re in the right order or something.

  • Trying to clean up my variables and such and put them in the global scope without there being ‘not declared in scope’ errors.

  • Trying to keep all time related things in the loop() and all relay related things in the subroutines.

This is compiling correctly, but it’s glitching my LCD / Arduino out and not working. I just wanted to potentially get an idea of what’s going on! Thanks for any help! I hope I’m using this forum correctly.

Have you tried it with LEDs standing in for any relays and other things attached?

Glitches can be software, this might be power supply or electrical noise problem.

a7

alto777:
Have you tried it with LEDs standing in for any relays and other things attached?

Glitches can be software, this might be power supply or electrical noise problem.

a7

I haven't quite done it with LED's, but I started the project in a simple sketch where I was simply turning the relay on and off based on a constant temperature and humidity read. I even then worked in a 'threshold' workflow for timed functions. Everything was working fine with that! It seems that trying to lump these things into their own subroutines is making it wonky. Or at least..trying to implement these 'deadzones' and 'setpoints' is messing things up somehow. I just don't think I have it organized properly, but I don't know how else to format it.

Post a wiring diagram showing how everything is connected. Are the humidifier and heater connected to the relays yet?

JCA34F:
Post a wiring diagram showing how everything is connected. Are the humidifier and heater connected to the relays yet?

Here’s essentially what the wiring diagram is

The humidifier and heater are NOT connected yet. The tests I’m doing are at my desk so it’s just turning the LED on and off the relay itself with a little ‘switch’ noise so I know they’re working. I’ve plugged in the heater and humidifier, but it was before I tried to implement the ‘deadzone’ idea. That’s what’s giving me the most issue now! I don’t want the humidifier to constantly be turning back on as soon as it hits 74.9% if the setpoint is 75%. Hope that makes sense?

You are not updating humidity or temp data in loop(), try this mod:

// New Version June 15th
#include <Wire.h>
#include "DFRobot_SHT20.h"              // establish sensor library
#include "RTClib.h"                     // establish RTC library
#include <LiquidCrystal_I2C.h>          // establish LCD library
// #include "SD.h"                      // potential SD card use
 
DFRobot_SHT20    sht20;                 // Define SHT20 Temperature and Humidity Sensor
LiquidCrystal_I2C lcd(0x27,20,4);       // set the LCD address to 0x27 for a 16 chars and 2 line display
RTC_DS1307 RTC;                         // Define RTC
 
int humidityRelay = 7;                                      
int tempRelay = 8;
int hourOn1 = 8;
int minOn = 0;
int hourOn2 = 20;
int Secs;
int Minutes;
int Hours;
 
 
float RH = sht20.readHumidity();                        
float Temp = sht20.readTemperature();
 
float RHsetpoint;
float RHdeadzone;
float TempSetpoint;
float TempDeadzone;
 
String myTime;
 
#define Humidifier_ON LOW
#define Humidifier_OFF HIGH
#define Heater_ON   LOW
#define Heater_OFF HIGH
 
 
void setup() {
 
// SETUP RTC, SHT20, LCD Screen, and set Relay Pins and start reading the hardware
  Serial.begin(9600);
  Wire.begin();
 
  RTC.begin();                                        // initialize RTC 24H format
 
// Datalogger RTC
   if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  
// SHT20
  sht20.initSHT20();                                  // Init SHT20 Sensor
  delay(100);                                         // Think about using this for potential wiggle room zone
  sht20.checkSHT20();                                 // Check SHT20 Sensor
 
// I2C LCD SCREEN VOID SETUP
  lcd.init();                                         // Initialize LCD
 
// Format Screen
  lcd.backlight();                                    // Fill Screen with contant variables
  lcd.setCursor(1,0);
  lcd.print("Time: ");
  lcd.setCursor(0,1);
  lcd.print("T:");
  lcd.setCursor(7,1);
  lcd.print("H:");
  lcd.setCursor(13,1);
  lcd.print("%");
  
// RELAY VOID SETUP
  pinMode(humidityRelay, OUTPUT);                    // Set relay pins as output
  pinMode(tempRelay, OUTPUT);
  //digitalWrite(humidityRelay, Humidifier_OFF);       // Turn relays off initially.  
  //digitalWrite(tempRelay, Heater_OFF);
}
 
 
 
void loop() {
                                                     // Main void loop which calls the RunEnvironment sub and establishes time parameters  
  DateTime now = RTC.now();
  Secs = now.second();
  Hours = now.hour();
  Minutes = now.minute();
  myTime = myTime + Hours +":"+ Minutes +":"+ Secs ;
 
  if(now.hour() >= hourOn1 && now.minute() == minOn)        // RunEnvironment with these settings at 8:00 AM              
  {
    RHsetpoint = 77;
    RHdeadzone = 7;
    TempSetpoint = 29.5;
    TempDeadzone = 2;
    RunEnvironment();
  }
 
  if(now.hour() == hourOn2 && now.minute() ==minOn)         // RunEnvironment with these settings at 8:00 PM
  {
    RHsetpoint = 80;
    RHdeadzone = 5;
    TempSetpoint = 24;
    TempDeadzone = 2;
    RunEnvironment();
  }
                                                            //  LCD SCREEN VOID LOOP                                                          
    lcd.setCursor(0,0);
    lcd.print(myTime);
    myTime = "";
    lcd.setCursor(2,1);                                     // Display Temperature and Humidity on LCD Screen
    lcd.print(Temp,1);
    lcd.setCursor(9,1);
    lcd.print(RH,1);
  delay(5000);;
 }
                                                        
 
void RunHumidity()                                        // Humidity sub routine
   {
RH = sht20.readHumidity(); 
      
if (RH < (RHsetpoint-RHdeadzone))                         // compare humidity against the setpoint and deadzone and turn on relay
    {
    digitalWrite(humidityRelay, Humidifier_ON);
    }
 
if (RH >= RHsetpoint)                                    // if it is at the setpoint then the relay will turn off
    {
    digitalWrite(humidityRelay, Humidifier_OFF);
    }
}
 
void RunTemperature()                                       // Temperature sub routine
{
Temp = sht20.readTemperature();
        
if (Temp < (TempSetpoint-TempDeadzone))                       // compare temperature against the setpoint and deadzone and turn on relay
    {
    digitalWrite(tempRelay, Heater_ON);
    }
 
if (Temp >= TempSetpoint)                                  // once temperature is met the relay will turn off
    {
    digitalWrite(tempRelay, Heater_OFF);
    }
}
 
void RunEnvironment(){ RunTemperature(); RunHumidity(); }               // Humidity and Temperature sub routine combined

Thanks again for the reply! So it seems like you added the RH= and Temp= into the sub routines as the main change? I'll try that, but let me ask this potentially dumb question:

Why would the " float RH = sht.20.readHumidity(); " variable stated in the global scope in the beginning not allow the subroutines to work properly?

I appreciate the help.

So I’m getting a really weird flaw in the system:

Here’s my “test sketch” where I’m simply controlling the relays based on the levels read by the sensor.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DFRobot_SHT20.h"
#include "RTClib.h"


DFRobot_SHT20 sht20;

LiquidCrystal_I2C lcd(0x27,20,4);

RTC_DS1307 RTC;

int humidityRelay = 7;                                     //I know this defines the pin at which the relays are on, but hourOn is also initially set at 8. Does this interfere? 
int tempRelay = 8;

#define Humidifier_ON LOW
#define Humidifier_OFF HIGH
#define Heater_ON LOW
#define Heater_OFF HIGH

void setup() {
                                                      // put your setup code here, to run once:
  Serial.begin(9600);
  RTC.adjust(DateTime(__DATE__, __TIME__));           //setup rtc
  
  sht20.initSHT20();                                  // Init SHT20 Sensor
  delay(100);
  sht20.checkSHT20();
  lcd.init();                                         // Initialize LCD
 
                                                      // Format Screen
  lcd.backlight();                                    // Fill Screen with contant variables
  lcd.setCursor(1,0);
  lcd.print("Temp: ");
  lcd.setCursor(12,0);
  lcd.print((char)223);
  lcd.setCursor(13,0);
  lcd.print("C");
  lcd.setCursor(1,1);
  lcd.print("RH: ");
  lcd.setCursor(10,1);
  lcd.print("%");
  
  RTC.begin();
  
  pinMode(humidityRelay, OUTPUT);                   // Set relay pins as output
  pinMode(tempRelay, OUTPUT);
  digitalWrite(humidityRelay, Humidifier_OFF);                 // Turn relays off initially. Relays are activeLOW so turning them on really turns the device off. 
  digitalWrite(tempRelay, Heater_OFF);

}

void loop() {
  // put your main code here, to run repeatedly:

  
  DateTime now = RTC.now();
  float RH = sht20.readHumidity();                        
  float Temp = sht20.readTemperature();
  
  
  if(RH < 75){                                       // SECRETLY THIS MEANS IF < 75
   
  digitalWrite(humidityRelay,Humidifier_ON);
            }
  else { digitalWrite(humidityRelay,Humidifier_OFF);
            }


  if(Temp < 25){                                    // SECRETLY THIS MEANS IF > 27
        digitalWrite(tempRelay,Heater_ON);
            }
            else { digitalWrite(tempRelay,Heater_OFF);
            }

    lcd.setCursor(7,0);                            // Display Temperature and Humidity on LCD Screen
    lcd.print(Temp,1);
    lcd.setCursor(5,1);
    lcd.print(RH,1);
    
            delay(5000);;
          }

And this sketch uploads and works perfectly fine in relation to the sensors and relays. However, when I upload this sketch containing the subroutines and such- it just craps out the connection to the relays. It’s not controlling them whatsoever! So weird, as almost everything is identical except for the subroutines and what not. Sorry if I’m talking in circles!

Why would the " float RH = sht.20.readHumidity(); " variable stated in the global scope in the beginning not allow the subroutines to work properly?

Because that code only initializes the variables and you never update them afterwards, they will always be the same.
Post the code you are running now but first type [CNTRL] + [ T ] to auto format it.

Thanks for these replies!

So here’s what I would ideally like to build off of, but I’ve started it from scratch so many times I’m not opposed to doing that either! [also thanks for the CTL + T tip]

// New Version June 15th
#include <Wire.h>
#include "DFRobot_SHT20.h"              // establish sensor library
#include "RTClib.h"                     // establish RTC library 
#include <LiquidCrystal_I2C.h>          // establish LCD library
// #include "SD.h"                      // potential SD card use

DFRobot_SHT20    sht20;                 // Define SHT20 Temperature and Humidity Sensor
LiquidCrystal_I2C lcd(0x27, 20, 4);     // set the LCD address to 0x27 for a 16 chars and 2 line display
RTC_DS1307 RTC;                         // Define RTC

int humidityRelay = 7;
int tempRelay = 8;
int hourOn1 = 8;
int minOn = 0;
int hourOn2 = 20;
int Secs;
int Minutes;
int Hours;


float RH = sht20.readHumidity();
float Temp = sht20.readTemperature();

float RHsetpoint;
float RHdeadzone;
float TempSetpoint;
float TempDeadzone;

String myTime;

#define Humidifier_ON LOW
#define Humidifier_OFF HIGH
#define Heater_ON   LOW
#define Heater_OFF HIGH


void setup() {

  // SETUP RTC, SHT20, LCD Screen, and set Relay Pins and start reading the hardware
  Serial.begin(9600);
  Wire.begin();

  RTC.begin();                                        // initialize RTC 24H format

  // Datalogger RTC Setup
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

  // SHT20 Setup
  sht20.initSHT20();                                  // Init SHT20 Sensor
  delay(100);                                         // Think about using this for potential wiggle room zone
  sht20.checkSHT20();                                 // Check SHT20 Sensor

  // I2C LCD SCREEN VOID SETUP
  lcd.init();                                         // Initialize LCD

  // Format Screen
  lcd.backlight();                                    // Fill Screen with contant variables
  lcd.setCursor(1, 0);
  lcd.print("Time: ");
  lcd.setCursor(0, 1);
  lcd.print("T:");
  lcd.setCursor(7, 1);
  lcd.print("H:");
  lcd.setCursor(13, 1);
  lcd.print("%");

  // RELAY VOID SETUP
  pinMode(humidityRelay, OUTPUT);                    // Set relay pins as output
  pinMode(tempRelay, OUTPUT);
  //digitalWrite(humidityRelay, Humidifier_OFF);       // Turn relays off initially.
  //digitalWrite(tempRelay, Heater_OFF);
}



void loop() {
  // Main void loop which calls the RunEnvironment sub and establishes time parameters
  DateTime now = RTC.now();
  Secs = now.second();
  Hours = now.hour();
  Minutes = now.minute();
  myTime = myTime + Hours + ":" + Minutes + ":" + Secs ;

  if ( (now.hour() > 8) && (now.hour() < 20) ) {     // RunEnvironment with these settings at 8:00 AM

    RHsetpoint = 77;
    RHdeadzone = 7;
    TempSetpoint = 29.5;
    TempDeadzone = 2;
    RunEnvironment();
  }
  //if(now.hour() == hourOn2 && now.minute() ==minOn)         // RunEnvironment with these settings at 8:00 PM
  else {
    RHsetpoint = 80;
    RHdeadzone = 5;
    TempSetpoint = 24;
    TempDeadzone = 2;
    RunEnvironment();
  }
  //  LCD SCREEN VOID LOOP
  lcd.setCursor(0, 0);
  lcd.print(myTime);
  myTime = "";
  lcd.setCursor(2, 1);                                    // Display Temperature and Humidity on LCD Screen
  lcd.print(Temp, 1);
  lcd.setCursor(9, 1);
  lcd.print(RH, 1);
  //  Serial.print(" T:");                            // Serial Print the Temperature and Humidity to check outputs
  //   Serial.print(Temp, 1);
  //  Serial.print("C");
  //Serial.print(" RH:");
  //Serial.print(RH, 1);
  //Serial.print("%");
  // Serial.println();
  //    Serial.print(now.year(), DEC);
  //  Serial.print('/');
  //   Serial.print(now.month(), DEC);
  //  Serial.print('/');
  //  Serial.print(now.day(), DEC);
  //  Serial.print(' ');
  //  Serial.print(now.hour(), DEC);
  //  Serial.print(':');
  //  Serial.print(now.minute(), DEC);
  //  Serial.print(':');
  //  Serial.print(now.second(), DEC);
  //  Serial.println();
  delay(5000);;
}


void RunHumidity()                                        // Humidity sub routine
{
  RH = sht20.readHumidity();

  if (RH < (RHsetpoint - RHdeadzone))                       // compare humidity against the setpoint and deadzone and turn on relay
  {
    digitalWrite(humidityRelay, Humidifier_ON);
  }

  if (RH >= RHsetpoint)                                    // if it is at the setpoint then the relay will turn off
  {
    digitalWrite(humidityRelay, Humidifier_OFF);
  }
}

void RunTemperature()                                       // Temperature sub routine
{
  Temp = sht20.readTemperature();
  if (Temp < (TempSetpoint - TempDeadzone))                     // compare temperature against the setpoint and deadzone and turn on relay
  {
    digitalWrite(tempRelay, Heater_ON);
  }

  if (Temp >= TempSetpoint)                                  // once temperature is met the relay will turn off
  {
    digitalWrite(tempRelay, Heater_OFF);
  }
}

void RunEnvironment() {
  RunTemperature();  // Humidity and Temperature sub routine combined
  RunHumidity();
}

Hope this pasted clearly. Like I said, the main issue now is that the relays just won’t turn on or off at all now? As soon as I upload my “test” sketch, everything is back to working properly.

Thank you again!

Is this statement true? Line 89.

if ( (now.hour() > 8) && (now.hour() < 20) ) {

What does LCD and serial monitor say?

So now I’m even more confused than before…
When I upload that sketch…my serial monitor becomes completely blank and simply doesn’t populate with anything. And again, that’s THIS code:

// New Version June 15th
#include <Wire.h>
#include "DFRobot_SHT20.h"              // establish sensor library
#include "RTClib.h"                     // establish RTC library 
#include <LiquidCrystal_I2C.h>          // establish LCD library
// #include "SD.h"                      // potential SD card use

DFRobot_SHT20    sht20;                 // Define SHT20 Temperature and Humidity Sensor
LiquidCrystal_I2C lcd(0x27, 20, 4);     // set the LCD address to 0x27 for a 16 chars and 2 line display
RTC_DS1307 RTC;                         // Define RTC

int humidityRelay = 7;
int tempRelay = 8;
int hourOn1 = 8;
int minOn = 0;
int hourOn2 = 20;
int Secs;
int Minutes;
int Hours;


float RH = sht20.readHumidity();
float Temp = sht20.readTemperature();

float RHsetpoint;
float RHdeadzone;
float TempSetpoint;
float TempDeadzone;

String myTime;

#define Humidifier_ON LOW
#define Humidifier_OFF HIGH
#define Heater_ON   LOW
#define Heater_OFF HIGH


void setup() {

  // SETUP RTC, SHT20, LCD Screen, and set Relay Pins and start reading the hardware
  Serial.begin(9600);
  Wire.begin();

  RTC.begin();                                        // initialize RTC 24H format

  // Datalogger RTC Setup
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

  // SHT20 Setup
  sht20.initSHT20();                                  // Init SHT20 Sensor
  delay(100);                                         // Think about using this for potential wiggle room zone
  sht20.checkSHT20();                                 // Check SHT20 Sensor

  // I2C LCD SCREEN VOID SETUP
  lcd.init();                                         // Initialize LCD

  // Format Screen
  lcd.backlight();                                    // Fill Screen with contant variables
  lcd.setCursor(1, 0);
  lcd.print("Time: ");
  lcd.setCursor(0, 1);
  lcd.print("T:");
  lcd.setCursor(7, 1);
  lcd.print("H:");
  lcd.setCursor(13, 1);
  lcd.print("%");

  // RELAY VOID SETUP
  pinMode(humidityRelay, OUTPUT);                    // Set relay pins as output
  pinMode(tempRelay, OUTPUT);
  //digitalWrite(humidityRelay, Humidifier_OFF);       // Turn relays off initially.
  //digitalWrite(tempRelay, Heater_OFF);
}



void loop() {
  // Main void loop which calls the RunEnvironment sub and establishes time parameters
  DateTime now = RTC.now();
  Secs = now.second();
  Hours = now.hour();
  Minutes = now.minute();
  myTime = myTime + Hours + ":" + Minutes + ":" + Secs ;

  if ( (now.hour() > 8) && (now.hour() < 20) ) {     // RunEnvironment with these settings at 8:00 AM

    RHsetpoint = 77;
    RHdeadzone = 7;
    TempSetpoint = 29.5;
    TempDeadzone = 2;
    RunEnvironment();
  }
  //if(now.hour() == hourOn2 && now.minute() ==minOn)         // RunEnvironment with these settings at 8:00 PM
  else {
    RHsetpoint = 80;
    RHdeadzone = 5;
    TempSetpoint = 24;
    TempDeadzone = 2;
    RunEnvironment();
  }
  //  LCD SCREEN VOID LOOP
  lcd.setCursor(0, 0);
  lcd.print(myTime);
  myTime = "";
  lcd.setCursor(2, 1);                                    // Display Temperature and Humidity on LCD Screen
  lcd.print(Temp, 1);
  lcd.setCursor(9, 1);
  lcd.print(RH, 1);
  Serial.print(" T:");                            // Serial Print the Temperature and Humidity to check outputs
  Serial.print(Temp, 1);
  Serial.print("C");
  Serial.print(" RH:");
  Serial.print(RH, 1);
  Serial.print("%");
  Serial.println();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  delay(5000);;
}


void RunHumidity()                                        // Humidity sub routine
{
  RH = sht20.readHumidity();

  if (RH < (RHsetpoint - RHdeadzone))                       // compare humidity against the setpoint and deadzone and turn on relay
  {
    digitalWrite(humidityRelay, Humidifier_ON);
  }

  if (RH >= RHsetpoint)                                    // if it is at the setpoint then the relay will turn off
  {
    digitalWrite(humidityRelay, Humidifier_OFF);
  }
}

void RunTemperature()                                       // Temperature sub routine
{
  Temp = sht20.readTemperature();
  if (Temp < (TempSetpoint - TempDeadzone))                     // compare temperature against the setpoint and deadzone and turn on relay
  {
    digitalWrite(tempRelay, Heater_ON);
  }

  if (Temp >= TempSetpoint)                                  // once temperature is met the relay will turn off
  {
    digitalWrite(tempRelay, Heater_OFF);
  }
}

void RunEnvironment() {
  RunTemperature();  // Humidity and Temperature sub routine combined
  RunHumidity();
}

If you see near line 97:

if(now.hour() == hourOn2 && now.minute() ==minOn)

That’s the original setting I was trying to use but I got confused in “making a time range” I guess, so I just used the > 8 and < 20 to implement a “if it’s later than 8am but earlier than 8pm”

I hope all that makes sense?

Basically I’m not sure how to get this time function to work properly, but would that cause the whole program to crap out and not populate even the serial monitor?

This is the “practice” code I’m using, and again- this one works perfectly as intended with the relays turning off and on:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DFRobot_SHT20.h"
#include "RTClib.h"


DFRobot_SHT20 sht20;

LiquidCrystal_I2C lcd(0x27, 20, 4);

RTC_DS1307 RTC;

int humidityRelay = 7;                                     //I know this defines the pin at which the relays are on, but hourOn is also initially set at 8. Does this interfere?
int tempRelay = 8;

#define Humidifier_ON LOW
#define Humidifier_OFF HIGH
#define Heater_ON LOW
#define Heater_OFF HIGH

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  RTC.adjust(DateTime(__DATE__, __TIME__));           //setup rtc

  sht20.initSHT20();                                  // Init SHT20 Sensor
  delay(100);
  sht20.checkSHT20();
  lcd.init();                                         // Initialize LCD

  // Format Screen
  lcd.backlight();                                    // Fill Screen with contant variables
  lcd.setCursor(1, 0);
  lcd.print("Temp: ");
  lcd.setCursor(12, 0);
  lcd.print((char)223);
  lcd.setCursor(13, 0);
  lcd.print("C");
  lcd.setCursor(1, 1);
  lcd.print("RH: ");
  lcd.setCursor(10, 1);
  lcd.print("%");

  RTC.begin();

  pinMode(humidityRelay, OUTPUT);                   // Set relay pins as output
  pinMode(tempRelay, OUTPUT);
  digitalWrite(humidityRelay, Humidifier_OFF);                 // Turn relays off initially. Relays are activeLOW so turning them on really turns the device off.
  digitalWrite(tempRelay, Heater_OFF);

}

void loop() {
  // put your main code here, to run repeatedly:


  DateTime now = RTC.now();
  float RH = sht20.readHumidity();
  float Temp = sht20.readTemperature();


  if (RH < 75) {                                     // Turn on if less than 75%

    digitalWrite(humidityRelay, Humidifier_ON);
  }
  else {
    digitalWrite(humidityRelay, Humidifier_OFF);
  }


  if (Temp < 26) {                                  // Turn on if less than 26 C
    digitalWrite(tempRelay, Heater_ON);
  }
  else {
    digitalWrite(tempRelay, Heater_OFF);
  }

  lcd.setCursor(7, 0);                           // Display Temperature and Humidity on LCD Screen
  lcd.print(Temp, 1);
  lcd.setCursor(5, 1);
  lcd.print(RH, 1);

  Serial.print(" T:");                            // Serial Print the Temperature and Humidity to check outputs
  Serial.print(Temp, 1);
  Serial.print("C");
  Serial.print(" RH:");
  Serial.print(RH, 1);
  Serial.print("%");
  Serial.println();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  delay(5000);;
}

I would most definitely make use of the opto-isolation the relay module can provide. Especially when controlling AC inductive loads.

To to do this, you'll need a separate DC supply for the relay module. First remove the JD-VCC jumper then connect the supply to JD-VCC and GND. Then remove the ground wire from the relay board to Arduino (this is important). Voila!

Now you'll need 2 MOVs (rated at 150VAC+ continuous). One connected across each relay COM to NO terminals. Done!

Now all those weird issues like glitches and lockups (especially when any relay turns OFF, right?) will magically disappear!

dlloyd:
I would most definitely make use of the opto-isolation the relay module can provide. Especially when controlling AC inductive loads.

To to do this, you'll need a separate DC supply for the relay module. First remove the JD-VCC jumper then connect the supply to JD-VCC and GND. Then remove the ground wire from the relay board to Arduino (this is important). Voila!

Now you'll need 2 MOVs (rated at 150VAC+ continuous). One connected across each relay COM to NO terminals. Done!

Now all those weird issues like glitches and lockups (especially when any relay turns OFF, right?) will magically disappear!

Wow. Well this is quite a lot of information that I've never even tiptoed with. I'll have to individually look all these up and see how it goes! I figured that having these extension cords spliced the way they are would allow the power to be supplied to everything efficiently.

Also- like in the comment above where I show my 'testing' sketch. I've ran that for days with the relays plugged in and it worked fine. I'm mainly trying to figure out how to get the timing to work, along with the deadzone values and subroutines.

I don't know what the 'glitches and lockups' are that you're talking about- are you referencing the fact that when I upload the sketch the serial monitor doesn't populate with anything, and it seems that the whole program doesn't work. This is just with the USB plugged in to test. Hope all this makes sense!

I meant that the relays could work fine if no AC load is connected, but when an inductive AC load is connected and being switched by the relays, the contact arcing/EMI can cause issues. More chance of issues happening when the contacts go from closed to open.

An added benefit of a separate supply for the relay board is there'll be more usable current available from the Arduino 5V regulator.

Huh. I see what you're saying now. And your previous comment better lays out some of the general wiring and modding to do. I'll look into it for sure, and thank you for explaining!!

As for the code, do you know any reason why it'd be leaving the serial monitor blank in my main sketch? Something in there just isn't talking friendly.

As for the code, do you know any reason why it'd be leaving the serial monitor blank in my main sketch? Something in there just isn't talking friendly.

This line is terminated twice which would give unexpected results.

delay(5000);;

It would be good to loose the delay all together and just print in time intervals from the millis() timer, or just print if any condition changes.

Also, your conditions for operating the relays don't have any hysteresis. This could cause unnecessary operations (ON-OFF) oscillations. To add hysteresis, you would need 2 conditions for each relay, something like:

Turn heater on if less than 26 C
Turn heater off if greater than 28 C

Turn humidifier on if less than 75%
Turn humidifier off if greater than 77%