Bird
Raised Fist0
PLC Programmingscripting~10 mins

PID function block for process control in PLC Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - PID function block for process control
Start
↓
Read Setpoint
↓
Read Process Variable
↓
Calculate Error = Setpoint - Process Variable
↓
Calculate Proportional Term
↓
Calculate Integral Term
↓
Calculate Derivative Term
↓
Sum PID Output = P + I + D
↓
Apply Output to Actuator
↓
Wait for Next Cycle
↩Back to Read Setpoint
The PID function block reads inputs, calculates error, computes P, I, D terms, sums them, applies output, then repeats.
Execution Sample
PLC Programming
SETPOINT := 50;
PV := 45;
ERROR := SETPOINT - PV;
P := KP * ERROR;
I := I + KI * ERROR;
D := KD * (ERROR - PREV_ERROR);
OUTPUT := P + I + D;
This code calculates the PID output based on setpoint and process variable inputs.
Execution Table
StepVariableValueCalculation/ActionOutput/Effect
1SETPOINT50Set desired target valueNo output
2PV45Read current process variableNo output
3ERROR5Calculate error = 50 - 45Error used for PID terms
4P10P = KP(2) * ERROR(5)Proportional term calculated
5I5I = Previous I(0) + KI(1) * ERROR(5)Integral term updated
6D4D = KD(0.8) * (ERROR(5) - PREV_ERROR(0))Derivative term calculated
7OUTPUT19Sum P(10) + I(5) + D(4)Output sent to actuator
8PREV_ERROR5Store current error for next cycleReady for next iteration
9--Wait for next cycleCycle repeats with new inputs
💡 Cycle repeats continuously for process control
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6After Step 7Final
SETPOINT0505050505050
PV0454545454545
ERROR0555555
P001010101010
I0005555
D0000444
OUTPUT000001919
PREV_ERROR0000005
Key Moments - 3 Insights
Why do we calculate the error as Setpoint minus Process Variable?
Because the error shows how far the process is from the target. This difference drives the PID terms, as seen in step 3 of the execution table.
Why is the integral term updated by adding the error each cycle?
The integral term sums past errors to correct steady-state offset. Step 5 shows I increasing by KI times the current error, accumulating over time.
What does the derivative term represent and why use the difference of errors?
The derivative term predicts future error by measuring error change rate. Step 6 calculates D using the difference between current and previous error to dampen oscillations.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What is the value of ERROR?
A5
B45
C50
D0
💡 Hint
Check the 'Value' column for ERROR at step 3 in the execution table.
At which step is the OUTPUT value calculated?
AStep 4
BStep 5
CStep 7
DStep 6
💡 Hint
Look for the step where OUTPUT is assigned the sum of P, I, and D.
If the previous error was 3 instead of 0, how would the D term at step 6 change?
AIt would be KD * (3 - 5) = -1.6
BIt would be KD * (5 - 3) = 1.6
CIt would remain 4
DIt would be zero
💡 Hint
Use the formula D = KD * (ERROR - PREV_ERROR) with ERROR=5 and PREV_ERROR=3.
Concept Snapshot
PID Function Block:
- Inputs: Setpoint (target), Process Variable (current)
- Calculate Error = Setpoint - PV
- Compute P = KP * Error
- Update I = I + KI * Error
- Compute D = KD * (Error - Previous Error)
- Output = P + I + D
- Repeat cycle continuously
Full Transcript
This visual execution shows how a PID function block works in process control. It starts by reading the setpoint and process variable. Then it calculates the error as the difference between setpoint and process variable. Next, it computes the proportional term by multiplying the error by KP. The integral term accumulates over time by adding KI times the error each cycle. The derivative term predicts future error by calculating KD times the difference between current and previous error. These three terms are summed to produce the output, which is sent to the actuator. The previous error is stored for the next cycle. This cycle repeats continuously to maintain control of the process.

Practice

(1/5)
1. What is the main purpose of a PID function block in process control?
easy
A. To keep a process variable close to a desired target value
B. To store data for later analysis
C. To display process values on a screen
D. To manually adjust valves and switches

Solution

  1. Step 1: Understand the role of PID blocks

    PID blocks automatically adjust outputs to keep a process variable near a setpoint.
  2. Step 2: Compare options to this role

    Only To keep a process variable close to a desired target value describes this automatic control function; others describe unrelated tasks.
  3. Final Answer:

    To keep a process variable close to a desired target value -> Option A
  4. Quick Check:

    PID controls process variable = keep near target [OK]
Hint: PID blocks control variables automatically, not manually [OK]
Common Mistakes:
  • Thinking PID stores data instead of controlling
  • Confusing PID with display or manual control
  • Assuming PID only measures values
2. Which of the following is the correct way to declare a PID function block instance named pid1 in a PLC program?
easy
A. pid1 : PID;
B. pid1 = PID();
C. PID pid1;
D. pid1 -> PID();

Solution

  1. Step 1: Recall PLC function block declaration syntax

    In PLC languages like Structured Text, function blocks are declared with name : Type;.
  2. Step 2: Match options to this syntax

    pid1 : PID; matches the correct syntax; others use incorrect assignment or C-like syntax.
  3. Final Answer:

    pid1 : PID; -> Option A
  4. Quick Check:

    Function block declaration uses colon and semicolon [OK]
Hint: Use colon and semicolon to declare function blocks [OK]
Common Mistakes:
  • Using equal sign instead of colon
  • Using C or Java style declarations
  • Using arrow operator incorrectly
3. Given the following PLC code snippet using a PID block:
pid1.Kp := 2.0;
pid1.Ki := 0.5;
pid1.Kd := 0.1;
pid1.SetPoint := 100;
pid1.ProcessVariable := 90;
pid1();
Output := pid1.ControlOutput;

What is the expected behavior of Output after this code runs once?
medium
A. Output will be negative to decrease the process variable
B. Output will be zero because PID needs multiple cycles
C. Output will be a positive value to increase the process variable
D. Output will equal the SetPoint value

Solution

  1. Step 1: Analyze PID inputs and error

    The error is SetPoint (100) minus ProcessVariable (90) = 10, positive error.
  2. Step 2: Understand PID output direction

    Positive error means output should increase to raise the process variable, so output is positive.
  3. Final Answer:

    Output will be a positive value to increase the process variable -> Option C
  4. Quick Check:

    Positive error -> positive output to correct [OK]
Hint: Positive error means output pushes variable up [OK]
Common Mistakes:
  • Assuming output is zero on first run
  • Confusing output direction with error sign
  • Thinking output equals setpoint directly
4. Identify the error in this PID block usage snippet:
pid1.Kp := 1.5;
pid1.Ki := 0.3;
pid1.Kd := 0.05;
pid1.SetPoint := 50;
pid1.ProcessVariable := 55;
Output := pid1.ControlOutput;
pid1();
medium
A. Assigning ProcessVariable before SetPoint
B. Setting Kp, Ki, Kd values too low
C. Missing declaration of pid1
D. Calling the PID block function pid1() after reading output

Solution

  1. Step 1: Check order of function call and output read

    The PID block must be called (executed) before reading ControlOutput to update output.
  2. Step 2: Identify the mistake in code order

    Here, Output is assigned before pid1() is called, so output is stale or invalid.
  3. Final Answer:

    Calling the PID block function pid1() after reading output -> Option D
  4. Quick Check:

    Call PID block before reading output [OK]
Hint: Always call PID block before reading output [OK]
Common Mistakes:
  • Reading output before calling PID block
  • Confusing parameter order
  • Ignoring function block declaration
5. You want to tune a PID block to control temperature in a tank. The temperature rises too slowly and overshoots the target. Which tuning change should you make to reduce overshoot and improve response?
hard
A. Decrease Kp and increase Ki
B. Decrease Kp and increase Kd
C. Increase Kp and decrease Kd
D. Increase Ki and Kd equally

Solution

  1. Step 1: Understand overshoot and slow rise causes

    High Kp causes overshoot; low Kd means less damping; slow rise means Kp might be too low.
  2. Step 2: Choose tuning to reduce overshoot and improve speed

    Decreasing Kp reduces overshoot; increasing Kd adds damping to reduce oscillations and overshoot.
  3. Final Answer:

    Decrease Kp and increase Kd -> Option B
  4. Quick Check:

    Lower Kp + higher Kd = less overshoot, better control [OK]
Hint: Lower Kp and raise Kd to reduce overshoot [OK]
Common Mistakes:
  • Increasing Kp causes more overshoot
  • Ignoring derivative gain effect
  • Changing Ki without addressing overshoot
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard