Skip to content

PID Tuning

DryBox Settings Page

DryBox v2 uses a simple Proportional-Integral-Derivative (PID) algorithm to control how much power should be applied to the heater.
The idea behind the PID Controllers is very clever and they have so many applications that make them very interesting and widely used and I encourage you to learn more about them if you are interested.

I assume most people reading this are mainly interested in tuning their DryBox, so I will try to keep things at a very high level.

What KP, KI and KD coefficients do?

The PID controller has three coefficients that are assigned to each P, I and D components and we name them as KP, KI and KD respectively.

KP Coefficient

The proportional term is calculated as target_temperature / current_temperature it will then get multiplied by the KP coefficient.
This will then be added to our final PWM value along with the other two terms.

Note

When the dry box has just started and the current heater temperature is 'far away' from the target temperature, the proportional term of the PID controller will try to drive the output (heater) to get there as fast as possible.
As we approach the target temperature, the error is smaller and KP term should dominate less.
Too high value of KP will cause excessive response and overshoot.

KI Coefficient

The integral term is calculated by sampling current temperature many times over a 'longer' period of time. We then average those samples and compare that average to the target_temperature.
Finally the difference between the two will get multiplied by the KI coefficient.
This will then be added to our final PWM value along with the other two terms.

Note

The integral term helps us by accumulating (keeping track of) the error over longer period of time and correct for it.

Applying too much integral when the error is small and decreasing will lead to overshoot. After overshooting, if the controller were to apply a large correction in the opposite direction and repeatedly overshoot the desired position, the output would oscillate around the setpoint in either a constant, growing, or decaying sinusoid.

KD Coefficient

The derivative element of the PID controller only cares about the rate of change during a time period (which is calculated as (temperature_at_the_end - temperature_at_the_beginning) / time_period).
Its goal is to bring that rate of change to zero which would basically means that we are always right at our target_temperature.
This value will get multiplied by the KD coefficient and added to the final PWM value along with the other two terms.

Note

The Derivative term tries to bring the rate of change to zero which will essentially flatten the output's trajectory (keep our temperature fixed at a set temperature).

How is PWM calculated

After each each term is calculated (proportional, integral and derivative), they will get multiplied by their coefficient.
Then all of these values will get summed up and this gives us our new PWM value.

The PWM value is calculated as PWM = KP*P + KI*I + KD*D
Our PWM range is from 0 - 1023 and it translates to
0 → heater is fully off and
1023 → heater is fully on.

If the result of KP*P + KI*I + KD*D is higher than the 1023 (this means we need the heater to be more than 100% on), the PWM value will get clipped to 1023 (maximum).
Or if the result is negative value (this means that we should actually "take away" the heat from the heater, which we can't do) we will turn off the heater by clipping the PWM to 0.

Deviation from the 'standard' PID controller

Standard PID controllers expect your KP, KI and KD to be a decimal value in range [0-1].
However in our controller we are using integers that are in range [0-1000].

How to tune KP, KI and KD?

Precisely tuning a PID controller would have to take into account our heater size, heater power, enclosure volume, thermal conductivity, external temperature fluctuations, thermal leakage and many more variables.
Luckily for our dry box use-case, the temperature can not instantaneously jump by a very large amount or vice-versa, instantaneously drop by a very large amount.
Also, being so spot on that you can calibrate other thermometers with our dry box is nice if we easily get there, but is it absolutely required...
For example, if we set the dry box to hold the enclosure temperature at 45°C, for me ±1°C is more than acceptable and don't need to be absolutely at 45.000°C.

While you are free and encouraged to chase microns, below approach should bring you 99% of the way and probably be more than enough for most use-cases.

Start by finding the KP

Start by setting the Integral (KI) and Derivative (KD) values to 0.
Then keep increasing the proportional (KP) value until the controller starts to become unstable and oscillates.

A controller where the oscillations are smaller is still considered a “stable” controller, as eventually it will stabilize.
When the oscillations start getting larger then the controller is considered ”unstable”.

Once the proportional value (KP) that causes the controller to oscillate is found, take this value and divide it in half.
This will be the starting KP value.

What is acceptable?

You should define what smaller and larger means for your dry box and what is an acceptable or unacceptable amount of over/under-shoot for your dry box and tune for that performance.
"Chasing microns" is fun but define your "I'm happy with this" point and recognize when you are spending more and more time tuning the dry box while the performance gain becomes more and more negligible.
What is this point? Is it ±2°C or ±1°C or ±0.1°C? That's for you to decide.

Move onto finding the KI

Once the KP value is found, we can start to tune the KI.
Always start with small steps when adjusting a PID controller, and give time between each adjustment to see how the controller reacts.

Increase the integral gain in small increments, and with each adjustment, change the set point to see how the controller reacts.

The goal of tuning the integral value, is to achieve an adequate controller response or reaction time (after the initial response from the proportional is set).

If the controller starts to oscillate, or become unstable, adjust the KI value in the opposite direction until the controller becomes stable again.
Once the controller is stable, and responding desirably, this is our KI value.

Now we have both KP and KI values.

Finally find KD (if even necessary)

With the above two steps complete, you should have a working PI controller (instead of PID). And for a lot of scenarios, this is already going to be an over-kill.

But if you want to also find the KD parameter and add some dampening effect of the derivative then start by increasing the derivative value slowly, changing the set point, and allowing time for the controller to stabilize.

The purpose of the KD value, is to monitor the ramp rate of the process value, and prevent it from overshooting the set point.

Continue to change the set point and increase the derivative until the overshoot has been dampened to an acceptable level. If the controller starts to react in a negative way (unexpected changes in the output, poor control, or oscillation) lessen the KD value until the controller is stable again.

Be careful when adjusting the derivative value, as a higher value is tempting because we all want a smooth controller, but too high of a derivative will start to affect the output in a negative way and "fight" what the KP and KI values are trying to accomplish.

The other concern with a high KD value is if there is noise or distortion of the process value feedback, the derivative may see this as a fast change in the ramp rate, and bias the output at an undesired time.

Tuning by ear...

There are no one size fit all KP, KI and KD values. Every dry box will likely have slightly different ideal values.
Luckily from what I have experienced so far, while it's important to have "good" KP, KI and KD values, the dry box is fairly forgiving and you don't have to be spot-on with your tuning.

This is because we are not tuning a PID of a balancing robot or a self-driving motorcycle. Instead temperature of our heater or the inside of our dry box can not violently change and at random.
The heat/temperature will take time to build up and like-wise it will take time to cool down.
So as long as your KP, KI and KD values are in the right ball-park, your dry box should work just fine.