# 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