# Temperature control algorithm?

Discussion in 'The Projects Forum' started by wayneh, Jun 22, 2012.

1. ### wayneh Thread Starter Expert

Sep 9, 2010
16,102
6,219
I have a thermostat controlling the temperature of a device, and it's rock steady at ±0.1°C or so, which is great.

But now I want to move the set-point temperature of the thermostat in response to ambient temperature, to maintain a constant ∆T of my device versus ambient. For instance, hold 10°C below room temperature even while the room varies maybe 10° during the day.

I have the electronics and control system working but I need a software "smoothing" routine (to implement in Excel). An attempt to simply control the set-point on every sampling cycle worked, but "over"-controlled the system and actually increased variation.

Both temperatures are measured at least every 30 seconds. I'm guessing I need to trend the last several measurements and maybe not change the set-point at all unless the error drifts out of a range.

I could experiment with all this but I'm hoping someone can steer me to the best approach.

2. ### ifixit AAC Fanatic!

Nov 20, 2008
650
118
Hi Wayneh,

You need to integrate the signal from the ambient sensor in order to filter out short increases and decreases in temperature.

A simple thing to try first is to mount the ambient sensor on a large heatsink. Now the ambient temperature must change consistantly for a long period to change the heatsink temperature and thus the sensor temperature. This would act like a big capacitance would in the electrical world.

Try a CPU heatsink, but any block of metal would work... the more mass... the more smoothing effect.

Regards,
Ifixit

3. ### wayneh Thread Starter Expert

Sep 9, 2010
16,102
6,219
The sensor is an LM35. Its reported value is actually not moving much, so I think I'd like to try a purely software solution first.

When you say "integrate", how is that normally done? I was planning on just doing a linear regression over, say, 5 data points and then using the forecasted next temp as the value to use for adjusting the set point. I'm sure that would work, but I also know I'm re-inventing the wheel. Temperature control loops are pretty well understood.

4. ### MrChips Moderator

Oct 2, 2009
19,139
6,150
The standard engineering algorithm is called PID controller where:

P = Proportional
I = Integral
D = Derivative

I have implemented successfully a TEC (thermo-electric cooler) temperature controller using PI only.

Integration means summing average values over time, i.e. a moving average calculation.

5. ### Markd77 Senior Member

Sep 7, 2009
2,796
595
I found it hard to find a simple explanation. The graphs on this page and the pseudo-code at the bottom sum it up quite well.
It's probably not worth bothering with the derivative. Setting upper and lower limits for the integral term helped me get it working better (I used it for voltage control on a boost converter). If you can, add a couple of potentiometers to analog inputs, so that you can play with the constants without reprogramming the thing constantly.

6. ### wayneh Thread Starter Expert

Sep 9, 2010
16,102
6,219
I'm quite comfortable with the math that might be involved.

And I don't need pots because it's already fully under computer control.

Last edited: Jun 22, 2012
7. ### wayneh Thread Starter Expert

Sep 9, 2010
16,102
6,219
I'm familiar with that. In so many plant start-ups, I can recall the engineers always fretting over their PID charts. I've always been fuzzy on the implementation. I just went and read the wiki article and now see where my fuzziness is - tuning. The concept is clear enough but there's not much guidance on how to tune; what time window to use and so on.

Anyway, thanks for reminding me. I'll be sure to look at my data thru a PID lens, so that I can see how the error term is changing over time. I've already proved that a proportional factor of "1" applied at every sampling point is not the right approach.

8. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,430
1,311
That's such an unusual application I'm racking my brain trying to work out WHY you want to precisely temperature control somethinga few degrees cooler than room temp? Is it some de-humidifier type application?

9. ### wayneh Thread Starter Expert

Sep 9, 2010
16,102
6,219
It's a calorimeter (in a completed project I posted), and I'm interested in determining the heat formed within the chamber. Heat formed inside the chamber is removed by a TEC, and I can tell how much heat is produced by watching the duty cycle of the cooler.

The chamber is very well insulated and I have great control over the temperature, but it's thermally linked to the outside thru the TEC cooler and its heat exchanger.

After many runs, I've come to realize that cooling provided by the TEC is determined by the ∆T of the chamber versus ambient, and depends little on the actual temperature inside the chamber. The duty cycle I'm measuring depends heavily on ∆T, not T.

So to eliminate ∆T as a source of variation, I want to hold it constant during a run, so I can focus on the TEC's duty cycle without having to correct for a moving ∆T. I can't really hold ambient constant as easily as I can move the chamber temperature to compensate a move in ambient.

Oh, and I can't really divulge yet why I'm using a calorimeter. Hoping to write up a patent one of these days.

10. ### crutschow Expert

Mar 14, 2008
23,130
6,860
As Markd77 stated an integrator can be done with a digital computer as a moving (running) average. Just take a percentage of the difference between the previous average value and the new measured value and add/subtract that from the previous average to get the new average. The percentage you use for the addition/subtraction and how often you sample determines the time-constant (gain) of the integrator.

11. ### Markd77 Senior Member

Sep 7, 2009
2,796
595
Oops, here it is.
http://en.wikipedia.org/wiki/PID_controller

12. ### jimmy101 New Member

Jun 23, 2012
9
2
My first post ...

For a temperature controller I suspect that you need a PD controller. It is the derivative term that is the most important when working with heaters (or any device with a significant lag time). When the controller switches on the heater you have committed a certain amount of heat to the system. Given the fairly long time it takes for heat to dissipate fully throughout the system a proportional only (or Proportional + integral) controller will tend to overshoot. That is, by the time the temp sensor detects the systems is too hot you have already added too much energy to the system and it is too late to turn the heater off, the temperature is going to overshoot. Even if you do turn off the heater the systems temp will continue to rise for a time.

The derivative term in a PID controller is specifically meant to handle systems with significant lag. The derivative is just the rate of change of the error. The derivative term tells the controller to increase the correction when the error is increasing and decrease the correction when the error is decreasing. When properly tuned a PD controller will control heaters very well. When the error is getting worse the derivative term works with the proportional term to increase the response. When the error starts to drop the derivative term "anticipates" getting to the set point and the derivative term starts to work against the proportional term.

The only tricky part of the derivative term is that it is sensitive to noise. Often though, with fast enough sampling, the noise gets averaged out reasonably well.

It is actually the integral term that is generally the least useful in a PID controller. The integral is really best for correcting small errors, ones that are approaching the sensitivity of the detectors. In a temperature control setting the integral term will try to balance the positive errors with the negative errors, which is often what you want for an oven. (Other controllers don't use the integral term at all, or tweak it in specific ways. For example, you don't want a car speed control to use much of an integral term. Just because you spent some time 5 MPH below the set point doesn't mean you should spend the same amount of time 5 MPH above the set point.)

strantor likes this.
13. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,430
1,311
Thanks for taking the time to post an explanation.

Since the ambient temp and the chamber temp won't go through any fast changes adn entire process is likely to take some time, I would just use a setpoint controller.

It will oscillate around your setpoint temp differential and you can still read the overall duty% so it will get the job done.

The complexities of PID are more warranted when you have rapid changes in setpoint or in the load, neither situation exists in your calorimeter which is more a steady state process.

14. ### wayneh Thread Starter Expert

Sep 9, 2010
16,102
6,219
Yes, I think just using some modest smoothing, instead of adjusting to every measurement, will work fine. I'll try a few things and post back what I find.

15. ### jimmy101 New Member

Jun 23, 2012
9
2
PIDs aren't that complex, especially if the system already has a digital sensor and some kind of processing. But I agree, if you just have a a thermostat and a switch and a heater then a set point controller might be enough. They key issue though is the lag time between "heater on" and "temperature rise at the sensor". A well setup setpoint will work great, if the lag insn't to severe, at least until you open the system for loading (for example). To get back to the target temp might take a while with a setpoint controller. A mediocre PID will get you back to the right temp quickly and with minimal overshoot.

For an oven system like this tuning a PID should take very little time and is very straight forward. Just crank up Kp, set Ki and Kd to zero and log the temp versus time as the temperature oscillates about the set point. From that data you can calculate pretty decent Kp, Ki and Kd values and get a rock steady controller even with very large lag times in the system.

16. ### wayneh Thread Starter Expert

Sep 9, 2010
16,102
6,219
Well the current problem is...current. I'm having trouble with grounds. My LabJack controller is ultimately powered by my computer's PSU via USB. The calorimeter is powered by a separate PSU scavenged from an old Mac. When the cooling TEC kicks in (about 4A), grounds move around. Now that I have the LabJack putting a voltage into the setpoint of the thermostat - previously it was only taking readings through high-impedance inputs - the shifting grounds are messing up clean switching of the TEC. The comparator oscillates. I'll see if I can fix this today.

The calorimeter controls tightly at any setpoint, so there's really no issue with overshoot and such. If I could always have the right setpoint, set even once every 5 minutes or so, things would be great. The problem is how to adjust the setpoint given the bounce and trend of ambient temperature. My first attempt was to just track ambient point by point, and I don't think that's a good idea. The second approach is to just use a moving forecast, trended over the last 10 minutes or so. I think that will work fine once I fix the other issue, which may be a bigger problem.

Gosh I hate "ground".

17. ### BMorse AAC Fanatic!

Sep 26, 2009
2,675
241
I hate to bump this thread but, @ Wayneh: I have recently started building my PID controller for my Outdoor wood burning gassification boiler, and I had received some sample Thermocouple to Digital converter IC's from Maxim IC's (MAX31855) these IC's are easy to use (I have a bitbanged SPI routine written in Hi-Tech C to extract the data), the one thing I wanted to mention to you is that the IC has an internal temperature that it uses for the cold junction calculations, but you may be able to use this device's internal temp readings (which is normally equivalent to the IC's ambient temperature) to do what you wanted to... just a thought....

18. ### wayneh Thread Starter Expert

Sep 9, 2010
16,102
6,219
I'm now using a "simple" set-point control. It took a while playing with tuning PID parameters before I realized that all I needed was a careful control of the set-point. The learning was that I did NOT want any response to what was going on in the cooler - it was controlling itself - I just needed a slow-moving adjustment of the set-point. My cooler already worked very well to hold a precise temperature without any PID control. Responding to any error by changing the set-point just made things worse.

So I trended the ambient temperature over maybe 20 points and now adjust the set-point on only that. The second learning was that I had to include an offset voltage in converting temperature to a set-point voltage. Once I got that right, I now have very good control to a fixed °C below ambient.

19. ### strantor AAC Fanatic!

Oct 3, 2010
5,025
3,116
FYI Jimmy101 knows what he's talking about.

FYI2 I have a few Eurotherm PID heater controllers; PM me if interested.