Need quick help with relay

Hello everyone,

Situation: i have DHT11 temp and humidity sensor which I get data from perfectly. Thing is, that I need to turn on first relay if temp is higher than 22, and other relay if humidity is more than 50. Problem is that it turns on and then after about 0.5 seconds turns off and it repeats like that. I’m transfering my data from arduino to matlab and plot temperature and humidity graphs with it but I don’t think it has anything to do with this problem.

Here’s my arduino code (some words are from my country but I think you should be able to understand everything):

#include <DHT11.h>
int pin=8;
DHT11 dht11(pin);
int dregmeON = 7;
int tempON = 6;
float tempRiba = 22;
float humiRiba = 50;

void setup()
{ 
  pinMode(dregmeON, OUTPUT);
  pinMode(tempON, OUTPUT);
  Serial.begin(9600);
  digitalWrite(dregmeON, HIGH);
  digitalWrite(tempON, HIGH);
}

void loop()
{
  int err;
  float temp, humi;
  
  if((err=dht11.read(humi, temp))==0)
  {
    //Serial.print("temperatūra:");
    Serial.print(temp);
    //Serial.print(" drėgmė:");
    Serial.print(humi);
    if(temp>tempRiba) {
      Serial.print(1);
    }
    if(temp<tempRiba) {
      Serial.print(0);
    }
    if(humi>humiRiba) {
      Serial.print(1);
    }
    if(humi<humiRiba) {
      Serial.print(0);
    }
    if(temp==tempRiba) {
      Serial.print(0);
    }
    if(humi==humiRiba) {
      Serial.print(0);
    }
    Serial.println();
  }
  else
  {
    Serial.println();
    Serial.print("Error No :");
    Serial.print(err);
    Serial.println();    
  }
   if(temp>tempRiba) {
      digitalWrite(tempON, LOW);

    }
    if(temp<tempRiba) {
      digitalWrite(tempON, HIGH);

    }
    if(humi>humiRiba) {
      digitalWrite(dregmeON, LOW);
 
    }
    if(humi<humiRiba) {
      digitalWrite(dregmeON, HIGH);
    
    }
    if(temp==tempRiba) {
      digitalWrite(tempON, HIGH);
     
    }
    if(humi==humiRiba) {
      digitalWrite(dregmeON, HIGH);
     }

  
  delay(1000);
  }

and here is my matlab code which I dont think has anything to do with the problem:

s = serial('COM6');
laikas=100;
i=1;
while(i<laikas)

fopen(s)
%%fprintf(s, 'Čia perduodami duomenys')
out = fscanf(s);

Temp(i)=str2num(out(1:5));
 subplot(211);
 plot(Temp,'g');
 axis([0,laikas,10,50]);
title('DHT11 Temperatūros rodmuo');
xlabel('---> Laikas*1,83 s');
ylabel('---> Temperatūra °C');
grid


Humi(i)=str2num(out(6:10));
 subplot(212);
 plot(Humi,'m');
axis([0,laikas,20,100]);
title('DHT11 Drėgmės rodmuo');
xlabel('---> Laikas*1,83 s');
ylabel('---> Drėgmė %');
grid

Temp(i)
Humi(i)
fclose(s)
i=i+1;
drawnow;
end
delete(s)
clear s

The word you need to think about is "hysteresis". A thermostat should never turn on and off at the same temp. That lead to the kind of cycling you're seeing. It gets above the temp so the cooler or whatever comes on and that drops it below so it goes off so the temp comes up so it comes back on etc etc. instead, let it come on and stay on for a certain time or until the temp reaches some lower limit.

Well I know how a real thermostat works because I work with them alot it is just that I work with arduino not much and I need this project to be done tomorow. Thing is either way it SHOULD NOT work like this, I know what you mean by saying hysteresis but it should still work because if I increase humidity to 70 % lets say than every second my relay turns on for 0.5sec and turns off and it repeats every second and then if I lower it to 49 it stays off all the time - it's a problem with code not with logic.

Nevermind fixed it - delay was to small so propably data couldn't go through and give some random numbers that's why my relay was turning off..

delay was to small so propably data couldn't go through

That is complete nonsense. A longer delay will NOT "let data do through".

Yeah that makes zero sense. Maybe it just left it on long enough for the reading to stabilize.

PaulS:
That is complete nonsense. A longer delay will NOT "let data do through".

Not necessarily. The maximum sampling rate for the DHT11 is 1Hz. There in nothing in the spec sheets about what happens if you exceed this limit but I suspect that garbled data is a distinct possibility.

With a delay(1000) in the loop and a 16Mhz clock rate it is possible that the sketch is pushing the limit quite tightly. Extending the delay somewhat could ensure the OP gets clean data from the DHT11.

Ian

The other thing you need to consider while letting your data go through, is that the off-the-shelf relay that is commonly sold/used (the cute little blue cube) betrays us into thinking you can just plug it in and go. Read the datasheet and you'll find in the not-so-small print the the coil requires 'typical' 80mA of current to hold the engaged position. While this is not a huge amount, it's certainly more that you'll get from an Arduino pin. You may luck out given the tolerances of inexpensive components and find combinations that work, nut the bottom line is that the relay coil needs to be driven by a high (power) circuit. Either that or get a solid state relay or a thyristor or a SCR.