PID Autotuning using Relay Method

PID controllers are widely-used nowadays due to their ease of implementation and deployment, with many commercial products available on the market. To achieve desired performance, the controller gains need to be adjusted properly. Among many tuning schemes in the literature, the so-called Ziegler Nichols Frequency Domain (ZNFD) approach is one that can easily be understood and applied to a physical system. In this article we demonstrate simulation of manual ZNFD PID tuning, and then extend to an autotuning procedure using relay feedback. Basic theory can be read from [1] and our previous article Digital PID Controllers*.

*The result below is simply the Scilab version of the simulation in that article, using the same plant, but now with Xcos model files provided for your convenience.

PID-related articles at

Manual ZNFD Method

Consider a third-order lag plant with transfer function

(1)   \begin{equation*}  P(s)=\frac{1}{(s+1)^3} \end{equation*}

with PID feedback control in parallel form

(2)   \begin{equation*}  C(s)=K_p + \frac{K_i}{s} + K_ds \end{equation*}

Using Xcos model pid_feedback.zcos shown in Figure 1, we can simulate step response of this closed-loop system. In order to do so, a user may want to achieve a good-enough set of PID parameters to begin with.

Figure 1 an Xcos PID simulation diagram [pid_feedback.zcos]

A manual tuning method generally requires the user to adjust the controller parameters by herself and measure a couple of quantities related to plant dynamics, then compute PID gains using those values.

For the ZNFD method, the manual procedure is as follows

  1. Turn off the I and D terms by setting K_i an K_d to zero. The control law reduces to just a proportional gain; i.e., u(s) = K_pe(s).
  2. Increase K_p from a small value and observe the step response. Try to obtain the value that causes plant output oscillation. At this point the closed-loop system is marginally stable. Further K_p increment destabilizes the system. Write down this K_p value as ultimate gain K_u.
  3. Measure the oscillation period as ultimate period T_u.
  4. With the resulting values of K_u and T_u, compute the controller gains using Table 1.
Controller Form K_p K_i K_d
P 0.5K_u
PI 0.4K_u 0.5K_u/T_u
PID 0.6K_u 1.2K_u/T_u 0.075K_uT_u

Table 1: PID parameter adjustment using ZNFD method

We can experiment this ZNFD procedure on the plant (1) using pid_feedback.zcos. Double click on the PID controller to set PID parameters. Put value 0 in the Integral and Derivation fields. Put in some value for Proportional and simulate. Try adjusting proportional gain until the plant output oscillates indefinitely (not growing larger or dying out) as shown in Figure 2.

Figure 2 plant output oscillation when K_p = 8

You would find that the ultimate gain K_u for this proportional feedback system equals 8. Now measure the oscillation period T_u by zooming in the plot. This assessment does not have to be very precise. From Figure 3 we see that the period is approximately 3.5.

Figure 3 oscillation period T_u = 3.5

So from Table 1 we achieve K_p = 4.8, K_i = 2.74, K_d = 2.1. This set of parameters yields the step response in Figure 3. It is well-known that ZNFD method normally yields a response with large overshoot. If that becomes a problem one may want to reduce K_i.

Figure 4 step response from PID controller tuned by ZNFD method

One obvious problem with manual ZNFD procedure is to find the ultimate gain. Cranking K_p a little more results in closed-loop instability, which might be hazardous. We would prefer to obtain plant output oscillation without destabilizing the system. A relay feedback helps us achieve just that.

PID Autotuning based on ZNFD method

When a relay is put in place of a controller, it could cause the plant output to oscillate. This is true for a plant with its Nyquist plot crossing the negative real axis. If this is not the case, a relay with hysteresis may be used. For the third-order lag plant (1), a simple relay can be applied.

A relay is a nonlinear device. To analyze relay feedback as a linear system, a technique called describing function can be applied. See our previous article, or [1] for detailed analysis. Here we only summarize the result.

For a non-hysteresis relay, its describing function is

(3)   \begin{equation*}  N(a) = \frac{4d}{\pi a} \end{equation*}

Where d and a are the relay amplitude and plant output magnitude of oscillation, respectively. At the oscillation point, the Nyquist curve must intersect the negative axis at -1/N(a). It concludes that the ultimate gain K_u equals N(a). T_u can then be measured as before.

Figure 5 shows an Xcos diagram of a modified PID feedback. pid_autotuning.zcos, equipped with a relay that can be switched in place of controller during tuning phase. Note that the relay is built using SIGN block in series with a gain block to adjust its output magnitude. At the relay input a small bias is needed so that the relay operation could start initially when the plant output is zero.

Figure 5 PID control with relay autotuning function

Autotuning is performed when the switch is connected to input 1. Setting the relay output amplitude to 1; i.e., d = 1, yields the oscillation in Figure 6 (adjust the y-axis of scope if necessary to see the full oscillation waveform.)

Figure 6 plant output oscillation induced by a relay

From figure 6, the plant output magnitude after the oscillation is steady is roughly measured as a = 0.16. So from (3)

(4)   \begin{equation*}  K_u = N(0.16) = \frac{(4)(1)}{\pi(0.16)} = 7.96 \end{equation*}

which matches the value from manual ZNFD obtained earlier. By zooming in the oscillation waveform, it can be verified that oscillation period T_u is close to 3.5 sec. So PID parameters can be computed form Table 1 as before.

To summarize, the relay autotuning method helps us achieve the same K_u and T_u values as the manual ZNFD, without the risk of destabilizing the system. In a real application, K_u and T_u can be computed from data captured in real-time during the tuning phase, and the PID parameters computed according to Table 1. C code example is provided in our previous article [2].

Xcos files used in this article


  1. K.J.Astrom and T. Hagglund, PID Controllers, 2nd ed., Instrument Society of America, 1995.
  2. V.Toochinda, Digital PID Controllers,, 2011.



Comments are closed.