fan speed control algorithm - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: April 10, 2013, 12:33:01 pm
content: I'm writing a program for a PWM controller for fans. Now my main concern is to not have the fan keep "hunting" I want to aim to stay around a set temperature but my sensor will not be too near the newly cooled fluid (radiator) so I'm thinking of checking temperature at regular intervals so that is has a chance to see the effect of what is just set to and can look at changes over time and react.At the moment my basic algorithm is to to make the speed proportional to the difference in temperature between now and 5 seconds ago.Anyone have further insight ?

author: airiclenz
author_rank: Contributor
timestamp: April 10, 2013, 12:43:21 pm
content: Why not store the min and the max values of your temperature sensor. Every time you reach a new max value, you update your max variable (and store it to the eeprom). The same for the min value. This way your system is kind of self calibrating over time. You may want to set the full fan speed to start somewhere below the max temperature to be on the safe side...Then take an array of (e.g.) 10 values to store the last 10 measurements - measured every 3 seconds or so. The PWN value that then goes to the fan is just the average of these 10 last measurements. This "smoothens" the path of the fan following the temperature.I once realized a battery display that way but I can't see why this should not work fort this case too.Airic.

author: Simon
author_rank: Unknown
timestamp: April 10, 2013, 12:51:14 pm
content: This is bearing in mind that the heat to dissipate may go up or down so too much history may be useless and just slow down the response or make it run too fast for too long and over cool.I was thinking of considering T-Tset and using a correcting figure based on the current T and the last T so that it reacts to the absolute temperature without over reacting.

author: airiclenz
author_rank: Contributor
timestamp: April 10, 2013, 12:57:16 pm
content: Yeah, on a second thought - the self calibration is probably not the best solution. Especially because the surrounding temperature plays a role here too.I did not know that you have access to the current that is drawn. You could also mix this somehow with the boot-time-sensor temperature (which could be considered as the minimum for the current environment).This minimum temp + a defined offset which you want to allow then defines the temperature range in which the fan is ramped up or down...Airic

author: Simon
author_rank: Unknown
timestamp: April 10, 2013, 01:10:27 pm
content: I'm told the fan has to run at a minimum speed to clear any crap out that may accumulate and jam it should it stop so i say on start at 20-50%. Then I have two ways I think:1) measure current temperature and multiply it by a correction factor derived from the temperature over time difference. That way I account for how far out I am now and how fast it is happening.2) I look at the temp difference and use that to generate a value to add or subtract from the existing speed setting, that way the faster it rises the faster it goes to compensate without just bolting to 100% or running full blast then just cutting to low.The idea is to self regulate around the unknowns of: Ambient temp., coolant temp., cooling power of the radiator that will get worse as it "ages" due to clogging of the fins with rubbish, speed of coolant, speed of increase or decrease in heat load due to changes in operating requirements

author: jahonen
author_rank: Super Contributor
timestamp: April 10, 2013, 01:26:56 pm
content: Why not just stick to simple PI(D) algorithm, I think that there is no need to try to re-invent the wheel (it looks like you are already thinking something like it already). In simplest form, i.e. proportional controller, PWM is just measured temperature minus setpoint temperature, and result of this is then multiplied by some suitable constant (P-coefficient of PID).For eliminating the steady state error, add I-controller, which integrates (sums) the error calculated by the P-controller, multiplied by the I-coefficient.Then enforce some limits to calculated PWM duty cycle to account real-life constraints.Best way to counteract the fan jamming and start threshold would be to have two nested control loops, inner loop for controlling the fan speed, and outer loop setting the fan speed setpoint based on the measured and setpoint temperature. But that might be an overkill.Regards,Janne

author: Simon
author_rank: Unknown
timestamp: April 10, 2013, 01:28:37 pm
content: yes it is like a pid, I didn't know you can just go find some pid algorithm, where do I find them ?

author: fcb
author_rank: Super Contributor
timestamp: April 10, 2013, 01:28:55 pm
content: PID algorithm, and enjoy wasting a day tuning it.

author: fcb
author_rank: Super Contributor
timestamp: April 10, 2013, 01:33:43 pm
content: Apart from the obvious and vaguely rude "google it" answer, there is a really great old IC by NatSemi called an LM629, the datasheet for this certainly used to contain a really good explanation of PID and has some great explanations for things like slowing down the I term accumulation and anti-windup.

author: Simon
author_rank: Unknown
timestamp: April 10, 2013, 01:40:21 pm
content: At just 70 quid a pot I'll go the "DIY" route, but I'll read the datasheet to try and grasp some of the maths (says the dyslexic)

author: fcb
author_rank: Super Contributor
timestamp: April 10, 2013, 01:50:48 pm
content: I wasn't suggesting you actually use an LM629When I started doing motor control many moons ago, this was a great IC that the competition used, and we couldn't afford to put three of them in a box - so I rolled my own version using PIC16C73 (at the time the biggest cored 16 in the series).The datasheet was invaluable.

author: jahonen
author_rank: Super Contributor
timestamp: April 10, 2013, 01:57:43 pm
content: Here are some code:http://www.codeproject.com/Articles/36459/PID-process-control-a-Cruise-Control-examplePID calculations are quite simple, IMO biggest difficulty is that if you must use integer arithmetic, to figure out the scalings while avoiding overflows etc.Regards,Janne

author: Kremmen
author_rank: Super Contributor
timestamp: April 10, 2013, 02:03:27 pm
content: You are discussing things that are very close to a classical controller. But lets start from basics.You need to control temperature to agree with a target setpoint. For this you need a controller. The basic thing to keep in mind is that a controller applies a defined control law on the difference between setpoint and actual value, and produces the plant command variable as the result. The command variable in this case is the duty cycle of the pwm signal controlling the fan speed, from 0 to 100%.We will get to elaborations but that is all the actual controller does. I am sure you don't want to involve matrix calculations so lets keep it simple and use well defined and understood control laws only. That would then be the proportional-integral or PI. There are 2 easily understood terms here.Proportional control or P-term is just a defined ratio between error signal and control signal, i.e.C = Kpe, wheree is the error signal (Setpoint - Actual),KPis the multiplier or constant of proportionality.Proportional control has a shortcoming in that in order to produce a command signal there must exist an error signal. This is easy to see from the equation, since if e = 0, so does the command signal. You can make the necessary e smaller by increasing the gain, i.e. the P value but as that increases the gain of the feedback loop, there will be a limit after which the system is no longer stable.Enter the integrative or I-term. This is the time integral of the error signal multiplied by a gain, i.e.C = Ki/Ti* integral( e) dt, whereKiis the integration gain,Tiis the integrator time constantThe integral action will reset to zero the steady state error left by the P term. A PI controller is just the sum of the P and I-terms.It is a known fact that tuning a PI(D) controller can be somewhat challenging as setting the gains too high is a quick route to oscillations or command runaway. This risk is significantly increased in the presence of time lags in the process, and this seems to be the case with your application. So too much should not be expected from the control dynamics but this scheme will work.Now, there were some process disturbances. All of these will be handled by the PI controller but if you need better dynamics than what you can get by tuning the PI, there are 2 more things you can do: introduce the D term and/or use feedforward control.The derivative or D-term is the time derivative of the error signal i.e. it reacts to the slope of e, or in other words changes. The equation isC = Kd* de/dt, whereKdis the derivation gain,de/dt is the first derivative of the error signal.This term will make the controller more responsive to changes in setpoint or actual value, increasing the control dynamics. Same cautions regarding stability as above.The final trick is to use feedforward control. This method has saved the bacon several times for me in situations where classical PID dynamics just cannot cope. Using this method you bypass the contol loop and apply a direct command signal based on either the setpoint or the actual value. Say you know that if the coolant temp is X degrees, the fan must run at Y% of full speed to cope. So why not feed the actual temp value back to the command signal with suitable gain to make it so.So the feedforward correction signal would becomeC = Y%/Xdeg (provided the speed is directly proportional to the temperature).Any errors this calculation makes will be easily corrected by the controller - actually far easier than the case where you feed no signal, because then the controller must remove all error instead just the residual one.An analog PID controller is easily constructed out of an op amp, 2 caps and 2 resistors and some auxiliary stuff. For a software controller it is a bit more complex as you will be going discrete time, but the wonderland of Inet is full of implementation examples, some of which actually work. I seem to recall that even the Arduino provides a PID controller, so that might be worth lookin into. as the source code is freely available.

author: Simon
author_rank: Unknown
timestamp: April 10, 2013, 02:31:55 pm
content: yes what you say makes sense. My main controlling factor should be the difference between current and desired temperature. I want to bring in the change speed to help it react fast enough to sudden increases but not over cool on slow decreases. I'll go looking for the PID library for arduino.

author: gautamdamodar
author_rank: Contributor
timestamp: April 10, 2013, 02:53:10 pm
content: Temperature being a slow process first option is ON/OFF control (no good tracking). Since the there may be considerable sensor lag(dead time), tracking may not be best even if you use PID through PWM. If you are using PID control, the variable being temperature, I (integral action) doesn't have much role and can be neglected. If you have access, please refer the book "Process Control Instrumentation Technology" by Curtis D. Johnson

author: Simon
author_rank: Unknown
timestamp: April 10, 2013, 03:02:52 pm
content: they already have thermostatic control. I'm supposed to be bettering it.

author: airiclenz
author_rank: Contributor
timestamp: May 29, 2013, 08:03:07 am
content: Hello Simon,I am wondering if there are any news regarding your fan-control. Did you find a nice PID library for Arduino?A.

author: Rerouter
author_rank: Super Contributor
timestamp: May 29, 2013, 08:40:00 am
content: something like this?%=% +1*((Average- Setpoint)*I + (Reading- Setpoint)*P + (Reading- Last)*D

author: Simon
author_rank: Unknown
timestamp: May 29, 2013, 11:58:49 am
content: uh I didn't worry about a full blown PID library in the end but just something that bumps the speed up and down as the temperature changes. I used the difference between the minimum and the temperature change since last read to get a bit of reaction speed in for when the temperature rises suddenly.I'm considering taking more samples and using the average every so often but not sure if it will help or complicate matters. I'm yet to set up a test rig although I got all the bits now.

author: airiclenz
author_rank: Contributor
timestamp: May 29, 2013, 12:06:47 pm
content: Ah, ok. Thanks for the update. Let us know how your test went.A.

author: miceuz
author_rank: Frequent Contributor
timestamp: May 29, 2013, 04:57:36 pm
content: There is an Arduino PID library written by a guy who does PID as his dayjob. It has many usual PID pitfalls (like integral windup) solved already. Just check and reuse his code, it's mainly microcontroller agnostichttps://github.com/br3ttb/Arduino-PID-Library

author: IanB
author_rank: Super Contributor
timestamp: May 29, 2013, 05:41:04 pm
content: Quote from: Simon on May 29, 2013, 11:58:49 amIt often helps to have a bit of filtering on the measured input to a control loop. Otherwise the control algorithm will respond to the input noise as well as the desired signal. Since noise is effectively random, this will tend to result in random control moves on the output, which is typically undesirable.Another useful approach to PID control implementation is the so called "velocity algorithm". If we consider PI control (which is sufficient for many purposes), we have:Error = Measure - SetpointOutput = Kp x [Error + 1/Ti x Integral(Error, dt) ]Kp is the proportional gain and Ti is the integral time.Such an algorithm is usually implemented on a microcontroller in discrete time steps. So we can consider the change in output from one step to the next:Change-in-output = Kp x [Change-in-error + Dt / Ti x Error]Dt is the cycle time of the control algorithm.If the setpoint is not moving, then:Change-in-output = Kp x [Change-in-measure + Dt / Ti x Error]Even if the setpoint does get moved, it often helps to ignore it in the proportional action since it may produce smoother control responses.The calculation above is very close to what you have been arriving at during your investigations. This puts it on a more rigorous footing. If you apply the calculation above, you have two constants to adjust in order to tune the controller, Kp and Ti. If Error and Output are both normalized so they are expressed in percent of scale, then typical values of Kp would be between 0.1 and 10, and typical values of Ti would be between 5 seconds and 500 seconds (where smaller numbers give stronger control action).

author: 4to20Milliamps
author_rank: Regular Contributor
timestamp: May 30, 2013, 01:58:09 am
content: A digital temperature sensor with programmable hysterisis?http://www.eetimes.com/design/power-management-design/4011599/Get-beyond-ON-OFF-fan-controlhttp://www.datasheetcatalog.org/datasheet/microchip/21674b.pdf