Erratic behavior controlling relay

I built a system to control a mushroom fruiting room. It detects humidity, CO2 and temperature, and controls a humidifier and an exhaust.

The exhaust is timer controlled and only turns on when the humidifier is off. Also, when anything is turned on, an O is displayed on the LCD.

Two issues:

1- Often, if even there's a symbol on the LCD, the exhaust is not on. And I know it's a coding issue because meanwhile the solid state relay's LEDs are off.

2- The symbol was supposed to be an O, but it's kinda random what shows up.

Can someone more experienced indicate if there's something wrong in these lines? If necessary I can post the whole thing.

unsigned long ExhaustOff = 600000;   
unsigned long ExhaustOn  = 1600000;
unsigned long Now = 0;    
unsigned long prevExh = 0;

byte ExhaustState = LOW;
byte HumState = LOW;
#define ON   0   
#define OFF  1
#define Hum  9   
#define Exh  10 
#define Fans 11

//Exhaust
 if (ExhaustState == LOW) {
  if (HumState == LOW) {
      if (Now - prevExh >= ExhaustOff) {
       ExhaustState = HIGH;   
       prevExh +=  ExhaustOff;  
       digitalWrite(Exh,ON);
       digitalWrite(Fans,ON);
       lcd.setCursor(19,0);  
       lcd.write("O");
       lcd.setCursor(19,3);
       lcd.write("O");
       delay(5000);
    }
    }
 }

Thanks for using code tags but please post your complete sketch so that the snippets that you have posted can be seen in context

Sorry, I thought any error would surely be in that section and wanted to avoid bloating the post. Here's the full code with a few changes.

#include "DHT.h" 
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
#include "MQ135.h"
#define DHTPIN 6    
#define DHTTYPE DHT22  
#define PIN_MQ135 A0

Adafruit_LiquidCrystal lcd(8, 7, 5, 4, 3, 2);  
DHT dht(DHTPIN, DHTTYPE);
MQ135 gasSensor = MQ135(A0);

float temperature; 
float humidity;

//Exhaust timer controls

unsigned long ExhaustOff = 600000;
unsigned long ExhaustOn  = 1600000;
unsigned long Now = 0;    
unsigned long prevExh = 0;

byte ExhaustState = LOW;
byte HumState = LOW;

//Relay codes  
#define ON   LOW 
#define OFF  HIGH
#define Hum  9   
#define Exh  10 
#define Fans 11

void setup() {
  Serial.begin(9600);
  dht.begin();
  lcd.begin(20, 4);

  pinMode(Hum,OUTPUT); 
  pinMode(Exh,OUTPUT);  
  pinMode(Fans,OUTPUT);  
  digitalWrite(Hum,OFF);  
  digitalWrite(Exh,OFF); 
  digitalWrite(Fans,OFF);

}

void loop() {
  Now = millis();  
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  float rzero = gasSensor.getRZero();
  float ppm = gasSensor.getPPM();

  lcd.setCursor(0,0);
  lcd.print("CO2: ");
  lcd.print(ppm);
  lcd.print("ppm   ");
  lcd.setCursor(0,1);
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print(" C");
  lcd.setCursor(0,2);
  lcd.print("Hum: ");
  lcd.print(humidity);
  lcd.print("%");
  lcd.setCursor(0,3);  
  lcd.print("Fans: ");
  delay(1500);

//Controls
//Humidity
  if (humidity < 88 && ExhaustState == LOW) { 
    HumState = HIGH;  
    digitalWrite(Hum,ON);
    digitalWrite(Fans,ON);
    lcd.setCursor(18,2);  
    lcd.write("O");
    lcd.setCursor(18,3);
    lcd.write("O");
    delay(5000);
    }

else if (humidity > 95){
    HumState = LOW;
    digitalWrite(Hum,OFF);
    digitalWrite(Fans,OFF);
    lcd.setCursor(18,2);
    lcd.print(" "); 
    lcd.setCursor(18,3);
    lcd.write(" ");
    delay(5000);
   }

//Exhaust
 if (ExhaustState == LOW && HumState == LOW) {
       if (Now - prevExh >= ExhaustOff) {  
       ExhaustState = HIGH;   
       prevExh +=  ExhaustOff;  
       digitalWrite(Exh,ON);
       digitalWrite(Fans,ON);
       lcd.setCursor(18,0);  
       lcd.write("O");
       lcd.setCursor(18,3);
       lcd.write("O");
       delay(5000);
       }
    }
  else {      
         if (Now - prevExh >= ExhaustOn) { 
         ExhaustState = LOW;
         prevExh += ExhaustOn;
         digitalWrite(Exh,OFF);
         digitalWrite(Fans,OFF);
         lcd.setCursor(18,0);
         lcd.write(" ");
         lcd.setCursor(18,3);
         lcd.write(" ");
         delay(5000);
         } 
       }
}

The error may well be in that section, but without seeing the context it may be difficult to tell. Factors such as how and where variables are declared and defined can be very important. As to bloating the post, using code tags makes the post shorter and the code much easier to deal with as it is in a scrolling window and can be copied for examination in an editor with a single click

1 Like

Hi,
Have you got ALL your low voltage and signal wires away from the mains power wires that you are switching.

Can you please post a circuit diagram and include your power supply?

This sounds like a corruption of data.
Even though your code malfunctions, does it still respond to change points in humidity, temperature and CO2 as normal?
Do you display the CO2 and temperature and humidity levels on the display as well?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

I used to have a regular (mechanic?) relay and when the AC exhaust turned on/off the LCD would be corrupted, so I put the power wires away from the rest of the setup. Back then everything ran fine but I did not have the fans running (maybe the problem lies therein).

The relay broke and I got an SSR, and since then there's been no more corruption of the display, so I moved the power wires back to make it all a bit more organized. I'll try moving it away from the low voltage wires once again.

I'll try to make a diagram, but I've never done it before.

It reads correctly from the sensors and responds to them as programmed.