acciones-proporcional-integral-pid

The Proportional and Integral Actions of a PID

  • 6 min

A PID controller is an algorithm that calculates a control action from the error by combining three terms: proportional, integral, and derivative.

In this article, we’ll start with the first two, the proportional action (P) and the integral action (I). They are the two components you’ll use most in real systems because a simple PI can control ovens, motors, power supplies, tanks, pumps, and half of the industry.

The underlying idea is very simple. We measure what we want to control, compare it with the target, and calculate the error:

Where r(t) is the reference or setpoint, and y(t) is the actual output measured by the sensor. Based on this error, the controller decides how much to push.

Proportional Action

The proportional action is the part of the controller that responds directly to the current error. If the error is large, it pushes hard. If the error is small, it pushes lightly.

Mathematically, it’s the simplest part of the PID:

Kp is the proportional gain. It is literally how much force we apply per unit of error.

If we are controlling an oven’s temperature and we want 200 °C, but we measure 180 °C, we have an error of 20 °C. With Kp = 5, the controller will send a proportional action of 100 output units, as long as the units of Kp are consistent.

float error = setpoint - current_temperature;
float output_p = Kp * error;
Copied!

So far, no mystery. It’s the classic “the further away I am, the harder I accelerate.”

What P Accomplishes

The proportional action makes the system react quickly. Increasing Kp usually causes the plant to reach the target faster because the controller pushes more aggressively.

But it has a limit. If Kp is too small, the system will be sluggish and take a long time to reach the target. If Kp is too large, it will start to overshoot, oscillate, and can even become unstable.

Increasing Kp is not a universal solution. At first, everything seems to improve, until just a little more gain turns your motor into a nervous blender.

P is fast, intuitive, and cheap to compute. That’s why it’s almost always the first action tested.

The Problem of Steady-State Error

Now comes the tricky detail. A purely proportional controller can leave steady-state error.

Suppose we want to maintain a motor at a certain speed. When the motor gets close to the target, the error becomes small. Since the proportional output is Kp * error, the control action also becomes small.

A point can be reached where the motor needs, for example, a 20% PWM just to overcome friction. But if the error is already small, the P doesn’t send enough force. Result: the system stays close to the target but doesn’t reach it completely.

We wanted 1000 rpm, but it stays at 970 rpm.

And the worst part is that it remains there quite contentedly. For the proportional action, that small error is exactly what it needs to continue generating the force that compensates for the load.

The Integral Action

The integral action is the part of the controller that accumulates the error over time. If a small but persistent error exists, the integral sums it up until it becomes large enough to correct it.

Mathematically, it is written like this:

The integral is the controller’s memory. The P looks at the present. The I looks at the past and asks: “How long have we been making a mistake?”

integral_accumulated += error * dt;
float output_i = Ki * integral_accumulated;
Copied!

Thanks to this, if the system stays below the target for a long time, the integral grows little by little until it adds the missing force. That’s why we say the integral action eliminates steady-state error.

The Dangerous Side of the Integral

The integral is very useful, but it has character. Because it accumulates error, it can also accumulate problems.

If Ki is too high, the integral grows too fast. The system reaches the target, but the integral carries so much “accumulated inertia” that it keeps pushing when it should already stop. Then overshoot and oscillations appear.

The integral corrects steady-state error, but it also tends to increase overshoot and settling time. It’s a great tool, but it shouldn’t be used excessively.

Furthermore, when the actuator saturates, the integral can continue to grow even though the hardware can no longer deliver more power. This problem is called Integral Windup, and we’ll look at it in detail later.

Control P vs PI

A pure proportional controller has this form:

A PI controller adds the memory of the error:

In practice, the PI is a very common combination because it distributes the work well:

  • P provides a fast reaction to the current error.
  • I eliminates the steady-state error that P cannot remove.

For thermal systems, speed control, pressure, flow, or level, a well-tuned PI is usually sufficient. The D is not always needed, and in fact, it is often avoided because it is sensitive to noise.

Basic Implementation of a PI

A discrete PI controller, for Arduino or ESP32, can look like this:

float Kp = 2.0;
float Ki = 0.5;

float integral_accumulated = 0.0;

float calculatePI(float setpoint, float measurement, float dt) {
    float error = setpoint - measurement;

    float P = Kp * error;

    integral_accumulated += error * dt;
    float I = Ki * integral_accumulated;

    return P + I;
}
Copied!

This code is simple, but it already contains the main idea. In each cycle, we calculate the error, apply the proportional action, and add a small rectangle of error to the integral.

Note the dt. It must represent the real time between samples and always be positive. If the period varies a lot or we use an incorrect value, the integral will no longer represent the area well, and the controller’s behavior changes.

The proportional action is the part that says “The further away I am, the harder I push.” It is fast, simple, and very effective, but it can leave steady-state error.

The integral action says “If we’ve been failing for a long time, I’m going to correct it.” Thanks to it, we eliminate steady-state error, although we pay the price of more overshoot and potential saturation problems.