• Forums
• Posts
Latest Posts
Active Posts
Recently Visited
Search Results
• Page Extras
• Forum Themes
• AVR Freaks

### Hot!PID for temperature control

Author
GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
0

# PID for temperature control

Hello, I am creating a Li-ion battery discharger. Only thing left is temperature control loop right now and I want to use PID for that purpose. I want to keep the temperature inside the device at max 95 Celsius. I am using PWM to set the discharging current. Which is 4500 for 2 Ampere ( maximum rate) and 9999 is the minimum discharging rate which is 0 ampere. So I will be discharging the battery at 2 ampere till temperature ( if it will ) reaches the 95 Celsius then I want to increase PWM values according to PID which will decrease the discharging current. However I cannot figure out how PID will be used for this purpose. I though following a way like this :
if(temperature<90){}
else{
PID Loop
.
.
.}
Will this approach break the PID loop? If yes then what kind of path I should follow. I am worried about integral part. It will keep increasing when the temperature is less than 95. But since the discharge current is constant 2 amp for this case, temperature will not rise and integral part will be increasing and increasing. And in case of any heat rise, piled up integration will break up the PID term in this case. I am looking forward for all the suggestions. Thanks beforehand.

Mysil
Super Member
• Total Posts : 3675
• Reward points : 0
• Joined: 2012/07/01 04:19:50
• Location: Norway
• Status: offline
Re: PID for temperature control 2019/11/20 04:20:39 (permalink)
5 (2)
Hi,
In this case, you have a regulation goal that is not linear.
You may try to change  the  PID regulator setpoint value for temperature, to be the same as the actual temperature measured, as long as  the temperature is below the maximum (95 degree C).

But I think there are 2 regulation loops here, one is for the temperature,
the other is for current 2 Ampere  or PWM  duty cycle 4500,
in order for the PID regulator algorithm to give any output at all.

You might try to make a scaled combination of temperature and current for PID input and setpoint,
something like
PID Input = Temperature * ScaleT  -  Current * ScaleC;
PID Setpoint = minimum(Temperature, 95degreeC) * ScaleT - 2Ampere * ScaleC;

I did put in a minus sign in the scaling expression for the current,
because of the inverted behaviour of your PWM code, higher PWM input giving less current.
A negative scaling factor would have the same effect.

For testing regulation settings, you might try to run with a lower temperature limit,
maybe 35 degree C, temporarily to study how the regulation works.
And maybe put in some independent safety code,
to shut down completely in case the measured temperature exceed setpoint temperature + 10 degreeC.

Maybe using PWM regulation in this application might be overkill,
you might get by with something like:
if (temperature < 90)
PWM = 4500;
else if (temperature < 91)
PWM = 5500;
else if (temperature < 92)
PWM = 6500;
else if (temperature < 93)
PWM = 7500;
else if (temperature < 94)
PWM = 8500;
else if (temperature < 95)
PWM = 9500;
else
PWM = 9999;

But integrating and testing  PID regulation code in your program, will be a more useful learning experience.

Regards,
Mysil
GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
Re: PID for temperature control 2019/11/20 05:22:36 (permalink)
0
Thank you Mysil, that suggestions will help me a lot. Especially dynamic setpoint suggestion. Is it same to make scaling in PID input or PID output? I was thinking of making the scaling at the output. Something like this:

PID output = (error * Kp + integral * Ki*dt+ derivative * Kd /dt)

And setting up the Kp, Kd and Ki so that at 95 Celsius PID output will be 4500, and as it increases PID output will be decreasing. But that seems like a wrong way actually.
post edited by GunkutA - 2019/11/20 05:27:54
Mysil
Super Member
• Total Posts : 3675
• Reward points : 0
• Joined: 2012/07/01 04:19:50
• Location: Norway
• Status: offline
Re: PID for temperature control 2019/11/21 07:14:32 (permalink)
4 (1)
No,
Scaling PID input, and scaling PID output will have different effects.
Scaling PID input will be done in units and measurement range of your sensors,
scaling PID output will work in the units and range of your PWM Input.
Anyway, the PID regulation loop will try to make the error signal value smaller,
regardless of any input or output scaling you make.
That is unless there is a mistake in a sign somewhere, then it surely will run away.

There are different scaling effects in work already,
both on PID input caused by the properties of the temperature sensor,
and on PID output, caused by properties of the PWM code.

I suspect that you may have some difficulty to get PID output to be 4500 at 95 Celsius,
by manipulating Kp, Ki and Kd  only.
I think you may make a translation of PID output like this,
either:   PWM input = PID output + 4500;
or maybe: PWM input = 4500 - PID output;
beware, changing the sign here will have an effect on the sign of input scaling required.

Mysil
NKurzman
A Guy on the Net
• Total Posts : 18848
• Reward points : 0
• Joined: 2008/01/16 19:33:48
• Location: 0
• Status: online
Re: PID for temperature control 2019/11/22 09:25:54 (permalink)
4 (1)
For Classic PID your Out put is 0 to 100%
Rather than try to force the PID math to match your control signal, you should have a separate function to do the translation .
That said this application does not really call for a PID loop.  logic to limit the current if the Temp is too high would probably work just as well, and not require tuning.

" I am worried about integral part. It will keep increasing when the temperature is less than 95"
This is called "integral wind up"  you can google to find one of the many solutions for it.