# PID temperature controller

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.

• Look at integral windup mitigation - The easiest approach is to simply clamp the integral output. For the reverse output take some inspiration from playground.arduino.cc/Code/PIDLibraryConstructor - note the parameter "Direction". If I were you, I would start with drawing a pid control block diagram. It makes the task a lot easier. – Sorenp Nov 20 '19 at 6:44
• By the way, I used Brett Beauregard's PID library for temperature on Arduino, and it never fit that use case. Yes, it was beautiful to have a process temperature that was even more stable than my thermometer, but thermostat logic would have worked better in every way that mattered. – piojo Nov 20 '19 at 6:49

A device that enables/disables power outside a certain range is a thermostat. If you want to cut off power at 95 degrees, you need a thermostat. It seems like you want to smoothly reduce power up to that point since you are using PWM. If you want to reduce power in a way that is effective but less smooth, cutting off the current based on a comparator without hysteresis will do that. Thermal noise will end up changing the duty cycle to a point where the temperature sits right at your chosen maximum. Any time the temperature rises a bit, the comparator will be triggered more often and the duty cycle will decrease. Is it important to use PWM, and do you expect the circuit to have reduced power at 90 degrees, or should it be full power as close to 95 degrees as possible?

In any case, it sounds like PID is the wrong type of control, since PID is great for keeping a very precise average temperature and overcoming disturbances without too much overshoot. However, it builds up significant error when it's first turned on or when a big change is introduced from outside the system, then the controller overshoots significantly. There are workarounds such as modifying the algorithm or using a PD controller instead, but it sounds like PID is the wrong tool for the job. If your circuit needs very smooth power changes, please add that to the question (and maybe you will actually need a sophisticated controller).