PID Loop

What connects temperature readings to fan speed settings is a Proportional-Integral-Derivative (PID) control loop. The equation underlying the calculation is

where
Co = controller output (the fan drive)
Kp = proportional drive gain
Ki = integral drive gain
Kd = derivative drive gain
E = error, computed as reading – setpoint
t = time
DE /Dt = change in E over time

The code is a relatively direct implementation of the equation, integrating the error over a predefined number of periods and controlling the computations to maintain accuracy and avoid overflows.

The first element of the control loop to understand is the proportional drive, which simply computes a drive value as a factor times the error. The more the measured value differs from the set point, the greater the drive. Negative error speeds up the fan, while positive error slows it down.

The problem with using only proportional control is that it produces zero drive for zero error — when the measured temperature is equal to the set point temperature. If the fire can’t maintain the set point temperature with zero drive, the system temperature will drift off. You can fix that with a bias drive component, but it’s subject to error. Integral control drives that error out.

Finally, neither proportional nor integral control responds rapidly to more dramatic changes (such as opening the smoker lid) without overcontrolling the system. Derivative control solves that problem by adding drive in response to the change from one sample to the next.

If you plan to use the values with Excel, you might want to revise the code to output in comma separated value (CSV) form.