Embedded PID Temperature Limit Control for a LED Lamp

Thread Starter

pedroalbaladejo

Joined Apr 9, 2018
15
Hi, we are working on a LED lamp and we need a PID to limit the temperature. The LEDs are PWM controlled by a microprocessor and the temperature is read with a thermistor.

Before to start with the PID design I would like to clarify some doubts.

1/ We do not have to reach a target temperature, but limiting the temperature to protect the user and electronics. We can do it by progressively dimming the LEDs intensity when it approach or pass the temperature limit, but we do not need the PID to increase the power to reach the temperature limit! :D So, how can we consider this in the PID design?

2/ The power of the lamp can be adjusted by the user and we plan to adjust the gains modelling the system with a 50% step input (at 100% the lamp may get too hot). Will the PID work well when the lamp is operated at different power values? Or do we need to consider it in the design somehow?

Beside the questions above we would really appreciate a guide or advice on the required steps.

Thank you very much.
Pedro (very beginner)

P.D.: I am refering to Robert's great article: https://www.allaboutcircuits.com/pr...trol-part-3-implementation-and-visualization/
 

ebeowulf17

Joined Aug 12, 2014
3,307
Hi, we are working on a LED lamp and we need a PID to limit the temperature. The LEDs are PWM controlled by a microprocessor and the temperature is read with a thermistor.

Before to start with the PID design I would like to clarify some doubts.

1/ We do not have to reach a target temperature, but limiting the temperature to protect the user and electronics. We can do it by progressively dimming the LEDs intensity when it approach or pass the temperature limit, but we do not need the PID to increase the power to reach the temperature limit! :D So, how can we consider this in the PID design?

2/ The power of the lamp can be adjusted by the user and we plan to adjust the gains modelling the system with a 50% step input (at 100% the lamp may get too hot). Will the PID work well when the lamp is operated at different power values? Or do we need to consider it in the design somehow?

Beside the questions above we would really appreciate a guide or advice on the required steps.

Thank you very much.
Pedro (very beginner)

P.D.: I am refering to Robert's great article: https://www.allaboutcircuits.com/pr...trol-part-3-implementation-and-visualization/
My gut feeling is that PID is overkill if all you need is protection against overheating.

Nevertheless, if you want to use PID, and this is microprocessor project already, there should be a number of ways to get it done.

One approach would be a simple conditional statement outside of the PID settings. Basically, maintain two PWM duty cycle variables at all times - one which represents the brightness setting you would target if heat weren't an issue, and the other which represents the PID loop's chosen output in order to hold a steady temperature.

Your conditional statement simply takes the lower of the two values at each pass. That way, if the system is sufficiently cool, you get your chosen brightness, but if it starts to heat up too much, the PID output will drop to compensate, becoming the lower value and dimming the LED as needed.
 

ebeowulf17

Joined Aug 12, 2014
3,307
Those conditional statements can be considered a form of Fuzzy Logic control.
Fuzzy logic can be particularly useful for control of non-linear elements, as compared to PID, which assumes the system is linear.
Here's some info on that, if interested.
Fuzzy logic may be the better solution here. One fear I have with my first suggestion is that, during any time when the temperature is low enough for the conditional statements to bypass the PID control, the PID algorithm may continue trying to increase its output in order to seek its set point. If the PID algorithm gets "saturated" at an extreme value, I'm not sure how well it will transition into well regulated control if/when the temperature rises and the PID takes over.

I suspect that the problem I've described could be at least partially alleviated by resetting the time dependent variables within the PID system when it is activated, but I'm not 100% sure how that would be done. I know some implementations of PID code include options for stopping and starting active control while still monitoring the process variables, but I don't remember what the tricks were.

Personally, I've had such good luck with fuzzy logic, and so many struggles with PID, that I would go pure fuzzy logic if this were my project. The down side is it's a lot more custom code writing up front. The upside is that it's so much easier to configure once it's written.

One final note, within the link you provided are two more links. The Seattle Robotics one is an amazing resource which taught me most of what I know. I can't remember what the other one held, but it appears to be a dead link now. I checked my records and I don't seem to have a copy of that specific info. I'm suddenly feeling a strong urge to save copies of the Seattle Robotics pages so I don't lose them too!

***EDIT:
I didn't see Dana's post come through before I typed this. This wasn't meant as a reply to, or argument against, Dana's info. Just thinking out loud about how I'd approach this and what pitfalls I'd watch out for.
 
Last edited:

Thread Starter

pedroalbaladejo

Joined Apr 9, 2018
15
Fuzzy logic may be the better solution here. One fear I have with my first suggestion is that, during any time when the temperature is low enough for the conditional statements to bypass the PID control, the PID algorithm may continue trying to increase its output in order to seek its set point. If the PID algorithm gets "saturated" at an extreme value, I'm not sure how well it will transition into well regulated control if/when the temperature rises and the PID takes over.

I suspect that the problem I've described could be at least partially alleviated by resetting the time dependent variables within the PID system when it is activated, but I'm not 100% sure how that would be done. I know some implementations of PID code include options for stopping and starting active control while still monitoring the process variables, but I don't remember what the tricks were.

Personally, I've had such good luck with fuzzy logic, and so many struggles with PID, that I would go pure fuzzy logic if this were my project. The down side is it's a lot more custom code writing up front. The upside is that it's so much easier to configure once it's written.

One final note, within the link you provided are two more links. The Seattle Robotics one is an amazing resource which taught me most of what I know. I can't remember what the other one held, but it appears to be a dead link now. I checked my records and I don't seem to have a copy of that specific info. I'm suddenly feeling a strong urge to save copies of the Seattle Robotics pages so I don't lose them too!

***EDIT:
I didn't see Dana's post come through before I typed this. This wasn't meant as a reply to, or argument against, Dana's info. Just thinking out loud about how I'd approach this and what pitfalls I'd watch out for.
Hi guys, thanks a lot for all your comments and suggestions. I have work out a draft of what could be a PID + fuzzy logic.

The difficulties I try to solve are to have a smooth transition when the PID enter in action and the rate of change to have a smooth intensity change.

Please take a look a let me know your thinking.

*********************

/*Define the error

Error = Setpoint_Temp - corrected_temp;

/*We restrict the integral error to reasonable values.

Error_Integral = Error_Integral + Error;
if(Error_Integral > Error_Integral_Limit)
Error_Integral = Error_Integral_Limit;
else if(Error_Integral < -Error_Integral_Limit)
Error_Integral = -Error_Integral_Limit;

Error_Derivative = Error - Previous_Error;
Previous_Error = Error;

PID_Output = (K_proportional*Error) + (K_integral*Error_Integral) + (K_derivative*Error_Derivative);

/*We restrict the PID output to be between 50~100 % of PWM

if(PID_Output > 100)
PID_Output = 100;
else if(PID_Output < 50)
PID_Output = 50;

/*If the PID Output is higher than the current flood % setting, we override it with the setting value and reset the integral error

if(PID_Output > driver_flood_set)
PID_Output = driver_flood_set; Error_Integral = 0;

/*We restrict the PID max change between iterations

if(driver_flood_run - PID_Output > PID_Change_Limit)
PID_Output = driver_flood_run+PID_Change_Limit;
else if(driver_flood_run - PID_Output < -PID_Change_Limit)
PID_Output = driver_flood_run-PID_Change_Limit;
 

crutschow

Joined Mar 14, 2008
34,280
Instead of trying to kludge together a hybrid of PID and Fuzzy Logic, I would just go with Fuzzy Logic.
I think it will be easier and give you better results in the long run.
 

Thread Starter

pedroalbaladejo

Joined Apr 9, 2018
15
The fact is that I want the control to consider the error change speed, so at the end of the day I would be doing the derivative control with conditional statements what can be also very hard... My plan is tuning the PID constants to smoothly reach the temperature limit when it is operated at max power, and then try that the conditional statements limit its action when the user change the settings or the environmental temperature or airflow change. Currently I work with HW engineer and SW engineer so I hope they can help to do the right adjustments after we define a good strategy.
 

ebeowulf17

Joined Aug 12, 2014
3,307
The fact is that I want the control to consider the error change speed, so at the end of the day I would be doing the derivative control with conditional statements what can be also very hard...
It's no harder with fuzzy logic than it is with PID. In fact, in my experience, it's much easier to tune the system because you can work in real world units, like degrees/sec, and you can individually adjust the response for different stages - rising temps can be treated differently than falling temps, etc.
IMG_5108.GIF
This is a direct link to the page which shows an example of a rule matrix.
http://www.seattlerobotics.org/encoder/mar98/fuz/fl_part3.html#THE RULE MATRIX

And here's the link to the index so you can read through from the beginning if you're interested:
http://www.seattlerobotics.org/encoder/mar98/fuz/flindex.html

I'm sure you can achieve what you want with PID and some clever conditional coding, but like I said in post 6, the more I think about this scenario, the more I think it would be easier to get right with fuzzy logic.
 

ebeowulf17

Joined Aug 12, 2014
3,307
The difficulties I try to solve are to have a smooth transition when the PID enter in action and the rate of change to have a smooth intensity change.
PID is not my strong suit. I've played with it a lot, and often gotten acceptable results, but I've never tuned anything to work as well as I thought it should be able to with just PID (thus my love of fuzzy logic.)

So, take my PID opinions with a grain of salt.

That said, I think your approach of keeping the integral at zero, while still tracking the derivative, any time the PID isn't in control seems to make sense. I'm really not sure whether I think that will do the trick, but it sounds like a worthwhile experiment. Let us know how things turn out with that and/or fuzzy logic research (I'd encourage you to read the fuzzy logic info linked earlier even if you don't use it on this project - it's just great stuff to know about!)
 

kjj

Joined Mar 30, 2018
31
Hello there. As someone who has been working a lot with PID, i just have to say that this is NOT the time to use PID. There simply is no setpoint to track. What you really need is a simple IF-test which starts to dim the LED if the temperature is above a limit. End of story. Even though this technically could fall under the term "fuzzy logic", i would not even start to define rules/rule matrix etc.

Since the user sets the brightness of the lamp from 0-100%, my solution is to multiply this value by a correction factor (floating point) between 0 and 1 based on the temperature. Something like this pseudo-code:

temperature=analogRead(tempPin); // convert to celsius also?
user_setting=analogRead(settingPin); //convert to 0-100% also?
//temp limits in celsius
limit1=75;
limit2=80;

if (temperature<limit1) {
k=1;}
else if (t>=limit1 && t<limit2) {
k=1-1/(limit2-limit1)*(t-limit1);}
else{
k=0;}
end

analogWrite(ledPin,k*user_setting);

Now of course, i actually simulated this controller in Simulink (because i can lol). Some small assumptions about the first order plant. At 100% power, the LED will get hot (reason for the problem), i just assume 120C steady state (gain of 1.2 from power to celsius). I also assume the system will reach steady state in a couple seconds. The plant parameters does not actually affect the controller that much, which i will show in the pictures below. I also added measurement noise.
simulation model.png power setting vs temperature.png correction factor between 0 and 1.png power setting vs corrected setting 0 to 100 percent.png different plant but same results.png plot of correction factor if test.png

I hope you can see the pictures. As seen, the temperature never goes above 80C, which is my upper temperature limit, even with 2 different plants models.

And that's it. A simple IF-test to solve the problem without trying to use over complicated fancy stuff.
 

Thread Starter

pedroalbaladejo

Joined Apr 9, 2018
15
Hello there. As someone who has been working a lot with PID, i just have to say that this is NOT the time to use PID. There simply is no setpoint to track. What you really need is a simple IF-test which starts to dim the LED if the temperature is above a limit. End of story. Even though this technically could fall under the term "fuzzy logic", i would not even start to define rules/rule matrix etc.

Since the user sets the brightness of the lamp from 0-100%, my solution is to multiply this value by a correction factor (floating point) between 0 and 1 based on the temperature. Something like this pseudo-code:

temperature=analogRead(tempPin); // convert to celsius also?
user_setting=analogRead(settingPin); //convert to 0-100% also?
//temp limits in celsius
limit1=75;
limit2=80;

if (temperature<limit1) {
k=1;}
else if (t>=limit1 && t<limit2) {
k=1-1/(limit2-limit1)*(t-limit1);}
else{
k=0;}
end

analogWrite(ledPin,k*user_setting);

Now of course, i actually simulated this controller in Simulink (because i can lol). Some small assumptions about the first order plant. At 100% power, the LED will get hot (reason for the problem), i just assume 120C steady state (gain of 1.2 from power to celsius). I also assume the system will reach steady state in a couple seconds. The plant parameters does not actually affect the controller that much, which i will show in the pictures below. I also added measurement noise.
View attachment 150305 View attachment 150306 View attachment 150307 View attachment 150308 View attachment 150309 View attachment 150310

I hope you can see the pictures. As seen, the temperature never goes above 80C, which is my upper temperature limit, even with 2 different plants models.

And that's it. A simple IF-test to solve the problem without trying to use over complicated fancy stuff.
Hello, thanks a lot for your detailed answer. It is awesome that you even have simulated it! Please let provide some additional information that is actually the reasons why I initially doubt about a simple IF-test.

Firstly the LEDs are mounted on an aluminium case that work as a heat sink. I did not do the test yet but I agree your assumption that the stade state temperature could be about 120C, nevertheless it does not take just 2 seconds but about 8~10 minutes.

Also the user could start the lamp at maximum power (unit step) and we need to avoid that the the lamp power change instantaneously from 100% to 60%, we would maximum allow to dim the power a a rate of 20%/min. So if we wait until the lamp's temperature rises over the limit to take action it would widely pass the temperature limit.

Thanks again for your help.
 

Sensacell

Joined Jun 19, 2012
3,432
Start simple, add complexity as it's really needed.

I would start with a certain temp threshold value where you want the slope-down to begin,
subtract this value from the actual current temp, if the result is negative- do nothing, it's no too hot.

If it's over the threshold, multiply the difference by a constant. (this sets the slope of the fall off)
Take the result and subtract it from the value driving the dimmer.

Try it on the real hardware, adjust the threshold and slope as necessary.
Do it using integer math, floating point is a waste of CPU cycles for something simple like this.
 

kjj

Joined Mar 30, 2018
31
Hello, thanks a lot for your detailed answer. It is awesome that you even have simulated it! Please let provide some additional information that is actually the reasons why I initially doubt about a simple IF-test.

Firstly the LEDs are mounted on an aluminium case that work as a heat sink. I did not do the test yet but I agree your assumption that the stade state temperature could be about 120C, nevertheless it does not take just 2 seconds but about 8~10 minutes.

Also the user could start the lamp at maximum power (unit step) and we need to avoid that the the lamp power change instantaneously from 100% to 60%, we would maximum allow to dim the power a a rate of 20%/min. So if we wait until the lamp's temperature rises over the limit to take action it would widely pass the temperature limit.

Thanks again for your help.
Hello again. Just to clarify the IF-test i made. It does NOT instantly turn down the power if the temp is over 80 Celsius. I have 2 limits, which i set to 75C and 80C, so that the power is GRADUALLY turned down. The function is plotted on the last image from my previous answer.

But anyway, since your system takes a couple of minutes to heat up, my guess is that the control only gets easier. As for the requirement of power rate 20%/min, this is "kind of" built in to the IF-test (which again, is a gradual change). If you REALLY want to ensure that the output doesn't change fast, i would just put an output filter before it is sent to the LED lamp.

I have some pictures with the output filter. It works both for a fast and slow system response.
sim2_slow.png sim2_fast.png
We can see that the corrected power setting moves more slow now.

But just to really show what my simple IF-test can do, i set limit1=60C and limit2=90C. And now we can see that the corrected output moves very slow, even without the output filter! So my first reply should actually fulfill all your requirements if we just adjust limit1 and limit2 accordingly. :)
sim2_slow_no_filter.png
 

Thread Starter

pedroalbaladejo

Joined Apr 9, 2018
15
Hello again. Just to clarify the IF-test i made. It does NOT instantly turn down the power if the temp is over 80 Celsius. I have 2 limits, which i set to 75C and 80C, so that the power is GRADUALLY turned down. The function is plotted on the last image from my previous answer.

But anyway, since your system takes a couple of minutes to heat up, my guess is that the control only gets easier. As for the requirement of power rate 20%/min, this is "kind of" built in to the IF-test (which again, is a gradual change). If you REALLY want to ensure that the output doesn't change fast, i would just put an output filter before it is sent to the LED lamp.

I have some pictures with the output filter. It works both for a fast and slow system response.
View attachment 150319 View attachment 150320
We can see that the corrected power setting moves more slow now.

But just to really show what my simple IF-test can do, i set limit1=60C and limit2=90C. And now we can see that the corrected output moves very slow, even without the output filter! So my first reply should actually fulfill all your requirements if we just adjust limit1 and limit2 accordingly. :)
View attachment 150321
Hi again, it looks really interesting. I am really considering your proposal.

I have a couple of questions and please excuse me if I understand something wrong. Looking at your graphs for the slow system with filter, the output of the filter goes from 100% to 50% in about 40 seconds, what seems too fast. Could you please check what are the results considering a step 100% input (not ramp) and a filter to limit the change to 0.33%/second?

Regarding the limit for 60C, we cannot do that because 60~70C are really normal working temperatures, and we would like do not start to dim the lamp if it is working at 60% and the temperature is steady state around 65~70C. That's where the PID will help because is looking at how fast the temperature is changing.
 

kjj

Joined Mar 30, 2018
31
Hi again, it looks really interesting. I am really considering your proposal.

I have a couple of questions and please excuse me if I understand something wrong. Looking at your graphs for the slow system with filter, the output of the filter goes from 100% to 50% in about 40 seconds, what seems too fast. Could you please check what are the results considering a step 100% input (not ramp) and a filter to limit the change to 0.33%/second?

Regarding the limit for 60C, we cannot do that because 60~70C are really normal working temperatures, and we would like do not start to dim the lamp if it is working at 60% and the temperature is steady state around 65~70C. That's where the PID will help because is looking at how fast the temperature is changing.
Just to clarify, what are the temperature limit you have set for this lamp? I will simulate in my next reply.

Regarding the filter, it will have a exponential waveform, due to its nature, and the "rate of change" is not constant, see this figure: https://farm8.staticflickr.com/7196/14095683411_3fe8e97d84_o.png
Are you familiar with transfer functions and the differential equations they are based on?

If you really want the rate of change to be constant, at 0.33%/second, you can of course use a so called rate limiter. This exists in Simulink, although i am not sure how to implement it with code in C/C++, python, arduino etc. Regarding the rate of change you specified, why is this value chosen? With a "slow" rate of change, you might encounter a situation where the temperature goes above the limit because the power does not get turned down "fast enough".
 

Thread Starter

pedroalbaladejo

Joined Apr 9, 2018
15
A good temperature limit (for the sensor inside the lamp) would be 85C.

Thanks for the clarification about the filter. For the filter code, I think it could be something like this (from post #8):

if(driver_flood_run - PID_Output > PID_Change_Limit)
PID_Output = driver_flood_run+PID_Change_Limit;
else if(driver_flood_run - PID_Output < -PID_Change_Limit)
PID_Output = driver_flood_run-PID_Change_Limit;

The reason why we want to limit the rate of change is that this is a wearable lamp and it can be very annoying for the user to have the light power going up and down too fast or "looking" random when it going first down and then up if the control allows bouncing.

Yes, with a IF-test control we worry that the temperature will go above the limit.
 

Externet

Joined Nov 29, 2005
2,200
Respecting the complexity that a design may call for implementing PID ; never understood the habit of pushing LEDs to their maximum design limits risking their thermal/current survival.

Want more light ? Use two/multiple LEDs at half their rated power and forget complications, will last forever and reliably cool.
 
Top