Key Takeaways
- Temperature loops are slow, so run PID in a cyclic or periodic task, not in free-running logic.
- Use REAL tags for setpoint, process value, and output; keep BOOL enable bits and WORD/INT status tags separate.
- Start conservative: low proportional gain, long integral time, and little or no derivative.
- Filter noisy temperature inputs before they reach the PID block, especially if you use derivative action.
- Prevent overshoot by limiting output, tracking manual mode, and resetting or clamping integral accumulation.
- Trend PV, SP, and CV during tuning; don’t guess from one sample.
Structured Text PID Loop Example for Temperature Control
A temperature loop is one of the best places to learn PID control in a PLC. Thermal processes are forgiving enough to teach the fundamentals, but they are also slow enough to punish sloppy execution. If you run the loop in the wrong task, use aggressive gains, or feed noisy analog values directly into derivative action, you will get overshoot, oscillation, and unstable control.
In this article, I’ll show you how to build a basic temperature PID loop in Structured Text, how to wire it into a cyclic task, and how to tune it without chasing your tail. I’ll also point out the most common mistakes I see on real projects in industrial manufacturing, food and beverage, and water and wastewater.
What a Temperature PID Loop Is Doing
A PID controller compares setpoint to process value and drives an output to reduce the error. In temperature control, that output might be heater power, a valve position, or a time-proportioning signal to an SSR. The control law is:
\[ Output = K_p \cdot e(t) + K_i \int e(\tau) d\tau + K_d \frac{de(t)}{dt} \]
What each term means
- Proportional responds to current error.
- Integral removes steady-state offset.
- Derivative anticipates change and damps overshoot.
For temperature, derivative is often used lightly or not at all because thermal systems are slow and sensor noise can make the D term jumpy.
If you want the broader theory behind this, review PLC programming basics and structured text programming.
Why Execution Rate Matters
Do not execute a temperature PID every scan unless your scan is perfectly deterministic and intentionally timed. Use a periodic task or cyclic interrupt.
Good execution timing
- Siemens: cyclic OB such as OB35
- Rockwell: periodic task
- Typical temperature loop period: 500 ms to 2 s
A 1-second loop is common for ovens, tanks, and jacketed vessels. Faster updates are not automatically better. If you update too quickly on a slow process, you tend to amplify noise and make tuning harder.
Suggested I/O and Data Types
Use clean tag mapping. Keep raw analog input separate from engineering units.
| Signal | Data Type | Typical Range | Notes | |---|---:|---:|---| | Setpoint | REAL | 0.0 to process max | Engineering units, e.g. °C | | Process Value | REAL | Sensor-scaled value | Filtered if needed | | Controller Output | REAL | 0.0 to 100.0% | Or direct actuator demand | | Enable | BOOL | TRUE/FALSE | Drives loop state | | Status | WORD | Bit-coded | Alarm, manual, limit, fault |
This is especially important when the PID is tied to a PLC I/O module or an HMI/SCADA system.
Basic Structured Text PID Example
Here is a simple vendor-neutral pattern for a temperature loop. It is not a complete product-ready controller, but it shows the logic clearly.
Generic Structured Text example
VAR SetPoint : REAL := 75.0; ProcessValue : REAL; Error : REAL; Integral : REAL := 0.0; PrevError : REAL := 0.0; PID_Output : REAL := 0.0; Kp : REAL := 2.0; Ki : REAL := 0.01; Kd : REAL := 0.2; CycleTime : REAL := 1.0; ( seconds ) OutMin : REAL := 0.0; OutMax : REAL := 100.0; END_VAR
Error := SetPoint - ProcessValue;
Integral := Integral + (Error * CycleTime); IF Integral > 1000.0 THEN Integral := 1000.0; END_IF; IF Integral < -1000.0 THEN Integral := -1000.0; END_IF;
PID_Output := (Kp * Error) + (Ki * Integral) + (Kd * ((Error - PrevError) / CycleTime));
IF PID_Output > OutMax THEN PID_Output := OutMax; END_IF; IF PID_Output < OutMin THEN PID_Output := OutMin; END_IF;
PrevError := Error;
This version works as a teaching example, but in production I prefer a proven vendor block like PID_Compact, PIDE, or a built-in PID function block. Vendor blocks usually include bumpless transfer, anti-windup behavior, and status reporting.
For heater or valve applications, also look at the relevant variable frequency drives or contactors and motor starters if your output stages drive pumps or fans.
Siemens TIA Portal Example
If you are on a Siemens S7-1200 or S7-1500, the usual pattern is to call the PID in a cyclic OB.
Example configuration
- Add OB35 and set the interval to 500 ms
- Insert PID_Compact or FB865
- Create an instance DB for tuning and retained values
- Map PV, SP, and output to REAL values
Structured Text-style call
VAR Temp_PID : FB865; END_VAR
Temp_PID( SetPoint := 75.0, ManualEnable := FALSE, ManualValue := 0.0, OutputLimitLower := 0.0, OutputLimitUpper := 100.0, P := 2.0, TI := 120.0, TD := 20.0, ProcessValue := TempPV, Output => HeaterOutput );
The exact interface changes by firmware and library version, so verify the current Siemens documentation before commissioning. If your loop is part of a larger PLC automation panel, put the analog signal wiring, shielding, and grounding details into the panel design package early.
Rockwell and Other PLCs
Rockwell users normally place PIDE in a periodic task and work with REAL tags for PV, SP, and CV. AutomationDirect, Beckhoff, Omron, Schneider, ABB, and Mitsubishi all have similar patterns: deterministic task, real-valued tags, and a tuned PID block.
Practical comparison
| Brand | Typical Tooling | Execution Method | Notes | |---|---|---|---| | Siemens | TIA Portal | Cyclic OB | Strong ST and PID library support | | Rockwell | Studio 5000 | Periodic Task | Common in process and machine control | | AutomationDirect | Productivity Suite | PID blocks | Good for quick setup and autotune | | Beckhoff | TwinCAT 3 | Cyclic task | Very flexible for advanced control |
If your temperature loop drives a fan, pump, or compressor, you may need panel hardware from a motor control center or soft starter panel, not just a PID block.
Tuning the Loop Without Creating Overshoot
Temperature systems usually have lag. Heat takes time to reach the sensor, and the sensor itself may be slow. That is why aggressive tuning often causes overshoot.
Start with conservative settings
- Set derivative to zero.
- Increase proportional gain until the response is firm but not oscillatory.
- Add integral slowly to remove offset.
- Only add derivative if the process is sluggish and stable enough to benefit.
Practical tuning advice
- Use a long integral time at first.
- Keep Kp modest.
- Use small D or none at all.
- Trend PV, SP, and output together.
- Test at the real operating load, not just on an empty system.
Ziegler-Nichols as a starting point
If you need a starting point, Ziegler-Nichols can work, but it is rarely the final answer for temperature. Use it as a reference, then soften the settings if the loop overshoots.
Filtering Noise Before PID
Noise on the process value is one of the fastest ways to ruin a temperature loop. Thermocouples, long cable runs, and loose analog wiring can add jitter. That noise gets especially ugly when derivative action is enabled.
Simple moving average example
VAR PV_Raw : REAL; PV_Filtered : REAL; END_VAR
PV_Filtered := (PV_Filtered 0.9) + (PV_Raw 0.1);
This is a basic low-pass filter. It is not perfect, but it is often enough for temperature loops. Use stronger filtering only if the process can tolerate added delay. Do not filter so heavily that the loop becomes sluggish and sloppy.
For instrumentation and scaling guidance, the PLC communication protocols and field device wiring matter just as much as the code.
Common Problems and How to Fix Them
Overshoot
Usually caused by too much Kp, too much integral, or both.
Fix it by:
- reducing Kp
- lengthening integral time
- adding output limits
- checking whether the sensor is mounted too far from the heat source
Integral windup
This happens when the output saturates but the integral keeps growing.
Fix it by:
- clamping the integral term
- using anti-windup in the vendor PID block
- resetting or tracking the integral during manual mode
Oscillation
If the PV hunts around setpoint, the loop is too aggressive.
Fix it by:
- reducing Kp
- slowing the integral
- checking scan time consistency
- verifying that the control element is not sticking
No output or frozen output
This often comes from:
- disabled enable bit
- bad task execution
- incorrect PV scaling
- a status fault in the PID block
Input and Output Scaling Example
A temperature loop should always use engineering units internally. Never tune against raw counts if you can avoid it.
Example scaling concept
- Analog input 4–20 mA = 0–200 °C
- Output 0–100% = heater demand or valve command
That way the operator sees a real setpoint, the maintenance tech sees a real process value, and the programmer tunes against meaningful numbers.
If you’re building the control hardware from scratch, the panel layout often belongs in a main distribution board or dedicated process skid panel depending on power architecture.
External References
- IEC 61131-3 overview and PLC programming model: IEC 61131-3
- Siemens PID guidance and library behavior: InstrumentationTools article
- Rockwell process control and PIDE usage: Rockwell literature
- AutomationDirect PID block behavior and autotune: AutomationDirect help file
- PID configuration concepts for PLC loops: Maple Systems technical note
Next Steps
If you want to go deeper, read these related articles next:
- Structured Text Programming
- Ladder Logic Design Patterns
- PLC Troubleshooting Guide
- PLC Communication Protocols
- PLC Automation Panel
- Variable Frequency Drive
Need a custom PLC or automation panel built to spec? Patrion's engineering team can help — [email protected]