QuickPID Library for Arduino

QuickPID Library for Arduino

This API (version 2.03) follows the Arduino PID library, however there have been some significant updates. as follows:

  • Runs a complete PID cycle (read-compute-write) faster than just an analogRead() command in Arduino
  • Quicker hybrid fixed/floating point math in compute function
  • POn parameter controls the setpoint weighting of Proportional on Error and Proportional on Measurement
  • Reorganized and more efficient PID algorithms
  • micros() timing resolution
  • Faster analog read function
  • GetError() function added for diagnostics and control benefits


outputSum += (kpi * error) - (kpd * dInput);

QuickPID Constructor

QuickPID::QuickPID(int16_t* Input, int16_t* Output, int16_t* Setpoint,
                   float Kp, float Ki, float Kd, float POn, bool ControllerDirection)
  • Input, Output, and Setpoint are pointers to the variables holding these values.
  • Kp, Ki, and Kd are the PID proportional, integral, and derivative gains.
  • POn is the Proportional on Error weighting value (0.0-1.0). This controls the mix of Proportional on Error (PonE) and Proportional on Measurement (PonM) that's used in the compute algorithm. Note that POn controls the PonE amount, where the remainder (1-PonE) is the PonM amount. Also, the default POn (if not specified) is 0 (100% PonM, 0% PonE).

ControllerDirection Either DIRECT or REVERSE determines which direction the output will move for a given error. DIRECT is most common.

The QuickPID library should soon be available in the Library Manager (request has been made).

New version 2.1.0

QuickPID now has a built-in AutoTune function.

Included is an example using an RC filter. With this, AutoTune takes about 12-sec to determine critical gain, period and tuning parameters, then apply and use Kp, Ki and Kd in the sketch ...


New version 2.2.0 has enhanced AutoTune, 8 tuning rules to choose from and new GetKu() and GetTu() functions to display ultimate gain and period. There's now the opportunity to automatically set the sample period based on the Tu determined by AutoTune. Use the updated AutoTune RC Filter example for experimentation.

This auto tuner runs very fast ... it only takes a bit longer than 2 x Tu.



Another update with:

  • Even faster AutoTune function
  • AutoTune now determines the controllability of the process
  • Added AMIGO_PID tuning rule
  • Added GetTd() function to display dead time|500x101

New Version of QuickPID released (2.2.2).

  • Added compatibility with the ESP32 Arduino framework.
  • Added full featured AnalogWrite methods for ESP32 to control up to 9 PWM and 2 DAC signals.

QuickPID 2.2.7

  • Fixed REVERSE acting controller mode.
  • now using src folder for source code
  • replaced defines with enumerated types and inline functions

Controller Action

If a positive error increases the controller’s output, the controller is said to be direct acting (i.e. heating process). When a positive error decreases the controller’s output, the controller is said to be reverse acting (i.e. cooling process). Since the PWM and ADC peripherals on microcontrollers only operate with positive values, QuickPID only uses positive values for Input, Output and Setpoint. When the controller is set to REVERSE acting, the sign of the error and dInput (derivative of Input) is internally changed. All operating ranges and limits remain the same. To simulate a REVERSE acting process from a process that’s DIRECT acting, the Input value needs to be “flipped”. That is, if your reading from a 10-bit ADC with 0-1023 range, the input value used is (1023 - reading). See the example AutoTune_RC_Filter.ino for details.

1 Like

I’m trying to use this library to control a line follower.

I’m having an issue with getting the library to behave properly. With Kp = 1, Ki = 0, Kd = 0, I cannot get the controller to maintain a steady output. With a steady error, the output winds up to the output limit, despite having zero ki.

Am I missing something?

This also appears to be an issue with the original PID library that you started from. I find it hard to believe that I’m the only one who sees this, so what am I missing?

Thank you for your replies.

You're right that there's some windup with Kp which seems to provide a beneficial effect (better response to step change of setpoint) for PID or PI controllers. Note that the windup reduces to zero as the error approaches zero, so it doesn't directly cause oscillations or overshoot issues. I may need to re-visit the algorithm and do more testing for P or PD controllers.

After searching "Arduino PID line follower", I haven't found any that use the Arduino PID library ... they all use their own implementation usually of a PD controller.

This example looks promising, they have the source code in the first comment if you expand it.

Another example with code.

EDIT: Some questions

  1. Have you tried a much lower value for Kp such as 0.05?
  2. Are servo type motors used?
  3. What value are you using for SetSampleTimeUs()?
  4. Are there other delays in your code that could impact performance?

EDIT: I stumbled upon this where there's some comments on using a line follower.

QuickPID 2.2.8

  • AutoTune function is now non-blocking, no timeouts are required, exists in a sketch and uses Input and Output variables directly.