Before we can get cooking we have to solve a simple-sounding problem: how to keep the temperature of the water in the cooker at a set temperature for a long time. Its really easy to get water to boiling (just heat it until it starts to boil) or freezing (cool it till it solidifies) but keeping a water bath stable is a little tougher. The water cools off as it sits, and as it heats up the food, but how fast it cools depends on the amount of water, the temperature in the room, the temperature of the food and how much there is. Its hard to do by 'hand' we will automate it using PID Feedback Control
What's a PID?
We need to have our microcontroller control the heater to keep the temperature stable. It can heat the water by turning on the rice cooker (control
), and it can measure the temperature with our waterproof sensor (feedback
). The stuff in the middle is the the algorithm that ties these together. The PID algorithm is a type of feedback control. In this application, the temperature measurement
is compared with the setpoint
and the difference between them is called the error
. The error
is used to calculate an adjustment to the output
which controls the heating element.
The PID name comes from the three terms in the equation used to calculate the output:
P - The Proportional term looks at the present state of the process. Its value is proportional to the current error.
I - The Integral term looks at the history of the process. Its value is the integral of past errors.
D - The Derivative tries to predict the future of the process. Its value is the derivative or rate of change in the error..
These three terms are assigned weights known as tuning parameters: Kp, Ki
The three terms are summed to produce the control output.
The basic PID equation is not that difficult to implement and there are many PID implementations out there. But there are a lot of 'gotchas' in making a PID perform well in a real-world application.
These gotchas have been expertly addressed by Brett Beauregard in his Arduino PID Library
. And clearly documented in his blog post: Improving the Beginners PID