Controlling a Humidifier with a Grove Mini Temp/Humidity Sensor

I have tried several demo's and I have done extensive research into this already and I keep hitting a brick wall. I am a novice at this but I have learned a lot in a short amount of time. Perhaps not so much with complex coding but understanding functions. I have a grove mini temperature and humidity sensor that I have wired into an Arduino Mega 2560 which also has an RTC attached. I can read temp and humidity no problem and I can turn on a relay no problem. My problem is once the humidity begins to climb, the loop freezes. The relay stays HIGH until either the reset button is pushed or a serial monitor window is opened. I watched the process through the serial monitor as well and I see the same result. As soon as the humidity begins to rise, the serial connection freezes as well. Clear the screen and nothing happens. If I close the window and open it again, it will reset itself and start running again but it will do the same process each time. I have to reset each time. I set up another relay exactly the same way using the temperature and it seems to work fine. I have tried changing pins, changing relays, changing power supplies. I have exhausted all of my ideas and am now reaching out for help. If there is someone out there who feels they can help, I can share what code I have but even just loading the very basic TH02 demo and adding the simple relay command when the humidity hits <40, causes it to stop.

I can share what code I have but even just loading the very basic TH02 demo and adding the simple relay command when the humidity hits <40, causes it to stop.

Does that happen with the unmodified example code too or only if you add your relay code? In the latter case you have to post your code (don't forget the code tags!) and a wiring diagram of your setup (may be hand drawn, but all connections should be visible).

I will work on posting the demo code today, its straight out of the library. One thing that I noticed today while troubleshooting is that the serial stream and thus the loop freezes when the humidity begins to climb. Very strange. If I load my entire program and remove just the humidifier related items, everything runs fine. Hopefully when I post the wiring and code later, it will jump out at someone.

This happens even when the humidifier is taken out or left in the program. I am wondering if I may have a defective sensor. Right now the humidifier has no connections to the arduino and it will still freeze the loop.

I also tried changing relays and output pins and the problem follows.

I am new to posting so it will likely take me a while to figure this all out, but I will get the code and drawing posted at some point today.

I thought I saw instructions somewhere earlier when I was signing up so I'll look around.

#include <Arudino.h>
#include <TH02_dev.h>
#include "Wire.h"

#define HUMIDIFIER 3

void setup(){

   Serial.begin(57600);
   setSyncProvider(RTC.get);
   delay(150);
   TH02.begin
   pinMode(HUMIDIFIER, OUTPUT);
   digitalWrite(HUMIDIFIER, LOW);
   Wire.begin();
}

void loop()
{
  Humidity_Control();
  Display_Humidity();
}

void Humidity_Control () {

  float humidity = TH02.ReadHumidity();
    if(humidity < 45)
   {
      digitalWrite(HUMIDIFIER, HIGH);
   }
   else
   {
      digitalWrite(HUMIDIFIER, LOW);
   }
}

void Display_Humidity()
{
  float humidity = TH02.ReadHumidity();

     Serial.print("Humidity: ");
     Serial.print(TH02.ReadHumidity());
     Serial.println("%");
     Serial.print("\t");
}

ArduinoLayout.pdf (419 KB)

Post links to the used hardware and it's schematics. My guess is that the fly-back diode on the relay is missing and the negative spike on the 5V supply kills the Arduino supply for a short moment. Does it happen if you run the same code but don't connect the relay?

Temp/Hum Sensor - Grove - Temperature&amp;Humidity Sensor (High-Accuracy &amp;Mini) v1.0 | Seeed Studio Wiki

Timing Relay - http://www.haljia.com/download/board/timerdelayswitch/timerdelayswitch.pdf

30A SPDT Relay - https://easyeda.com/seeedstudio/Grove_SPDT_Relay30A-kiz5nFXf9

Here is a real brain teaser... I disconnected the SPDT relay from the rest of the system so that the only remaining wires left on it are the contact closure. While using a serial monitor, the system still freezes as soon as the humidity begins to rise, with no connections to the humidifier. I am starting it manually and not through the system. All code was removed as well and it still stops the loop.

I ordered a new sensor in the event I may have damaged it. When the humidifier remained on the first round, it soaked everything in the room, including the unprotected sensor hanging from the ceiling. The code seemed simple and should have worked. It doesn't seem to matter what I code, it freezes every time.

I installed the timing relay to make up for the bounces of on/off when the humidity range is close to turning on and I am also using one for a heater. This one is so that the heater doesn't get turned on and off quickly as most units don't like that. So this way, it stays on for a specified amount of time, outside of the code.

Or perhaps its as simple as a needed resistor somewhere.

Do you have the relay connected and does it trigger when the humidity climbs? Its possible that you reach a set point that triggers the relay. The relay or sudden rush of current as you turn on the humidifier or heater is causing the main power rail to drop in voltage (or ground to bounce) and giving you a signal integrity problem. If that's the case, you might need to isolate your ground, add more capacitors, and/or have more current capacity on your power rails. Mainly you have to solve a signal integrity problem which you can google for various solutions.

What Voltage does the humidifier run on? If 120 or 240 VAC, you need a snubber across the relay contacts, if DC you need a free wheeling diode across the power leads. Get rid of the timing relay, you can do that with the Arduino.

void Humidity_Control () {

  float humidity = TH02.ReadHumidity();
    if(humidity < 45)
   {
      digitalWrite(HUMIDIFIER, HIGH);
   }
   if(humidity >= 50
   {
      digitalWrite(HUMIDIFIER, LOW);
   }
}

Also, the coil current for that Songle relay is 185 mA, a big load for the Mega's voltage regulator, I would use a separate 5V supply of at least 300 mA.