Pulsing Relays

Hello all! Firstly I want to introduce myself to you, my name is Chris. I am from Germany and I usually code a bit in C# every leap year. Currently I run an extruder project with a friend. In our project we want to use arduino and some max6675 temperature sensors and some relays to control the band heaters.

But I have a tiny problem in my code.

I post here a simplified code which replicates my problem:

#define RELAY 14

float setTemp = 30;


void setup() 
{
    	pinMode(RELAY1, OUTPUT);
}

void loop()
{
        temp = temp.read_temp(10);


	if (temp > setTemp)			
	{		 	
		digitalWrite(RELAY, HIGH);		
		delay(100);
	}

        else
        {
                digitalWrite(RELAY, LOW);
                delay(100);
        }
}

The problem I have is that everytime it repeats the void loop scope it sends a LOW or HIGH even the pin 14 is already set to HIGH or LOW. Of course this code does so because I did not code there a condition like if pin 14 is set to HIGH then do not set it to HIGH again and skip it. And that is my problem. How can I code such condition. I can't just write something like...

	if (temp > setTemp)			
	{		 	
                if(RELAY == LOW)
                {
		      digitalWrite(RELAY, HIGH);		
		      delay(100);
                }
	}

...because RELAY is always 14 and 14 is never equal to LOW and so I still get the pulsing relay problem. So how can I fix this problem? I'd appreciate any tiny hint!

krissp20:

if(RELAY == LOW)

{
  digitalWrite(RELAY, HIGH);
 delay(100);
}

You'd better use:

if(digitalRead(RELAY) == LOW)
{
  digitalWrite(RELAY, HIGH); 
  delay(100);
}

Or avoid "delay()".

I am still just a beginner but I see a few issues..

It doesn't matter if you have a relay turned high and you set it high again.. It'll just stay high.. I don't think that is your problem..

How are you reading the temperature? Is that reading buffered? You need the buffer the reading.. Take 30 reads and average them.. Then make the decision to turn the relay on or off.. If you want to see what I mean throw a debug serial print in there to print the temp.. I think you'll see your temp reading jumping around above and below your limit..

Instead of delays try using the flash without delay code and modifying it to to a temperature read every couple of seconds instead of hundreds of times a second, that should slow the chatter..

Also and this gets a little bit out of my realm, but you may need to change your on/off code a little bit.. There should be an adjustable overshoot..

Say your aiming for 50 degrees.. You want to turn the heater on and let it run.. You turn it off at 50 degrees.. It'll probably overshoot a little bit.. You then wait until it gets to 48 degrees before you turn it on again... I don't know how stable you need the temperature, that'll decide how your going to do this but if you turn it on and off with 1 degree precision it will be turning on and off pretty rapidly, again if you limit your reads to every few seconds this won't be such a great concern..

You could go totally nuts and use a ssr to pulse the heater using some sort of pid algorithm if you need exact precise temperature control but that is quite a bit different programming and over my head.. If you need to go that route hopefully someone else can help you out with that..

It doesn't matter if you have a relay turned high and you set it high again.. It'll just stay high.. I don't think that is your problem..

Correct. The op's code is not causing the pin to switch states.

Whil it could be a problem with the test it's most likely the op is powering the relay and/or its load from the Arduino and causing the Arduino to reset.

Add the following to setup() to test this.

Serial,println("I must never use a relay again");
delay(500);

Mark

@tekk19: the readings are bufferd, the code i posted here is just a very simplified code

@holmes4: Arduino is not resetting, Setup Scope just runs one time. The Relay uses an external power.

@all: Thanks for your hints... I think it is an wiring issue

There is absolutely no problem with repeatedly overwriting the state of the pin,
this is a perfectly normal approach. However if you want to control the timing
and rate of updates then you will need to manage a state machine to handle the
transitions, so that's where to look next.

However the PID loop control is probably wise - better temperature control
is possible. You will need to think about the timing issue with a relay, they
have a limited life in terms of operation cycles.

Perhaps a relay isn't the best component here.