I am looking for the best/accepted way to control temperature with a differential or deadband. In other words I want to keep the temperature between a setpoint of 120 with a diffential/deadband of 5.
So the temperature needs to stay between 115 and 120.
It seems that I must first test to see if the TEMP is below (setpoint minus the diffential) and then keep the program flow contained inside a while loop until the temp is up to setpoint.
I posted some sample code below (that works) but I just wanted to see how others would handle this.
Any thoughts/code/examples would be appreciated.
Thanks
Frank
if (TEMP < SPT - DIFF) //temp is below setpoint minus diff
{digitalWrite (TE, HIGH); //turn heat on
while (TEMP < SPT)
{
TEMP = sensorsTOP.getTempFByIndex(0); //update TEMP
if (TEMP < SPT)
{
do nothing
}
else
{
digitalWrite (TE, LOW) //turn the heat off
}
}
}
Since the temperature will continue to rise after the heating stops, and continue to fall after the heating starts, the PID library would be a good place to start. It allows you to predict when the heat needs to be turned on or off to maintain the desired temperature.
PaulS
Thanks for the reply but I dont think a PID controller is needed in this case . The application is an electric heating element in a large volume of water(think electric hot water tank).
My question is.....What is the best coding methodoly to keep the temperature between the 'setpoint' and the 'setpoint minus the differential'. IE between 120 and 115.
Just turning the heat on when the temp is less that setpoint - diff, and turning it off when it gets to setpoint may result in the temperature falling more that diff below setpoint, because the water will continue to cool. Unless the heater is way more powerful than it needs to be, the temperature will not recover in time to prevent falling too far below setpoint. If the heater IS that powerful, you'll have a hard time preventing the temperature from exceeding setpoint + diff, unless diff is very large.
The PID function is designed to handle situations like this.
But, if you don't want to use it, I don't mind. Your code will turn the heater on when the temperature drops more than diff below setpoint, and will turn it off as soon as temperature gets to setpoint.
Well a 'deadband' should be expressed as a allowed variation + or - from setpoint. So if your desired maximum variation is 5 degrees and your setpoint is 120 then your decision points to turn on or off heat would be 117.5 and 122.5 respectively. However over and under shoot will most likely cause the temperature to not stay within those values, so you probably will have to tighten the control points to say 119 and 121 to stay in a 5 degree gap.
If the process mass (the water) is indeed slow enough, then this kind of bandgap controller can be used rather then the more complex PID control, much like the simple thermostat control used in most central heating systems.
You will simply have to play with the decision values to see if you can maintain your desired temperature gap, as the mass of the process Vs power of the heating element has an effect on the over/under shoot amount. PID deals with these in a proven manner (assuming you have the patience to tune the PID terms properly), but not always needed. Play and see.
Your code will turn the heater on when the temperature drops more than diff below setpoint, and will turn it off as soon as temperature gets to setpoint.
That is what I wanted to know. I was just looking for different/more efficient/better ways to achieve the end result. I am new to coding and the sample code in my first post is the ONLY way I could figure to do it. Is there another way? How would you do it? (without using PID)