This is an update/refactor of https://github.com/br3ttb/Arduino-PID-Library, since the original library does not seem to be actively maintained.
For an explanation of many of the implementation details see the excellent series of posts by the original library author: http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/
This is the output of the PID_HeaterSimulation example in the serial plotter (in the Arduino IDE "Tools> Serial Plotter"), showing an (unoptimized) PID controller, heating a simulated boiler to the required temperature
#include <PID_v2.h>
#define PIN_INPUT 0
#define PIN_OUTPUT 3
// Specify the links and initial tuning parameters
double Kp = 2, Ki = 5, Kd = 1;
PID_v2 myPID(Kp, Ki, Kd, PID::Direct);
void setup() {
myPID.Start(analogRead(PIN_INPUT), // input
0, // current output
100); // setpoint
}
void loop() {
const double input = analogRead(PIN_INPUT);
const double output = myPID.Run(input);
analogWrite(PIN_OUTPUT, output);
}
Compared to the already excellent original PID library, V2 introduces the following changes:
- A new, simpler, interface (while maintaining backwards compatibility).
- The PID algorithm will now for the actual elapsed time instead of the predetermined interval.
- Added example with boiler system to play around with to get a feel for the PID controller.
- Helper class to limit the output changes of the PID controller. This can be useful when, for example, you want a servo to move gradually, when the input or setpoint changes.
- Minor bugfixes & code cleanup.
- Brett Beauregard br3ttb@gmail.com brettbeauregard.com
- Max Ignatenko
- Thijs Elenbaas