Note that in the programming system of Siemens PLCs (especially TIA Portal), the more professional and accurate terms are Temporary Variables and Static Variables, which typically play the role of “intermediate variables.” The term “intermediate variable” is more of a functional description. Below, I will elaborate on its core concepts.
—
1. Principle: The “Draft Paper” and “Notebook” of Data
To understand the principle of intermediate variables, it is crucial to distinguish the data storage areas they reside in.
1. Temporary Variables (Temp)
· Storage Location: Located in the local stack (L Stack) of the PLC’s work memory.
· Operation Cycle: Valid only during the execution period of the program block it resides in (such as FC, FB). When the program block starts executing, the operating system allocates a temporary memory space for it; when the program block execution ends, this memory is released, and its value is lost. The next time this program block is called, the temporary variable will be reallocated, and its initial value is uncertain (it could be any value).
· Vivid Analogy: It is like a draft paper you use to solve a complex math problem. You write down intermediate results on the draft paper during the calculation process, but once the problem is solved, this draft paper is taken away or discarded. For the next problem, you will get a new blank draft paper.
2. Static Variables (Static)
· Storage Location: Located in the instance data block.
· Operation Cycle: Coexists with the instance data block it belongs to. As long as the data block is not deleted, the value of the static variable will remain until it is modified by the program. It retains its value between multiple calls of its associated FB.
· Vivid Analogy: Like your personal notebook. You can record information that needs to be continuously monitored (such as the cumulative value of a project) each time you work, and the previously recorded information will still be there the next time you work, allowing you to continue using and updating it.
2. Function: The “Core” of Structured Programming
The role of intermediate variables goes far beyond merely “storing data”; they are the core of achieving structured, readable, and maintainable programs.
1. Storing Intermediate Calculation Results
· This is the most basic function. In a complex logic or mathematical operation, breaking down multi-step calculations and storing each step’s result in a temporary variable for the next step’s calculation avoids writing lengthy and hard-to-read nested expressions.
· Example: Result := (Value1 + Value2) * (Value3 – Value4) / Factor; can be broken down into:
“`
#Temp_Sum := #Value1 + #Value2;
#Temp_Diff := #Value3 – #Value4;
#Result := #Temp_Sum * #Temp_Diff / #Factor;
“`
· Example: Calculate (A + B) * C – D. · Step 1: Store the result of A + B in the temporary variable #Temp1. · Step 2: Store the result of #Temp1 * C in the temporary variable #Temp2. · Step 3: Subtract D from #Temp2 to get the final result, which can be assigned to a static variable or output.
This logic is clearer and makes it easier to observe each step’s results during debugging.
2. Improving Program Readability and Maintainability
· Assigning a meaningful variable name (such as #bMotorOverload, #rActualSpeed) to the result of a complex expression allows the program reader to immediately understand the physical significance of this value, rather than struggling to interpret an address (such as M50.0 or MD100).
3. Achieving Encapsulation and Simplified Interfaces for Program Blocks
· By extensively using temporary and static variables in functions (FC) and function blocks (FB), complex internal logic can be “hidden.” Only necessary input (In) and output (Out) parameters are exposed externally. This makes the program block an independent, clearly defined “system,” greatly facilitating code reuse and team collaboration.
4. Maintaining State Within Scan Cycles (for Static Variables)
· Static variables serve as the “memory” of the FB. They can be used to implement functions that require data to be maintained across multiple scan cycles, such as counters, timers, and status bits. For example, defining a static variable #StartupCounter in the FB can be used to record the number of operating cycles after the device starts.
3. Detailed Explanation and Analysis: In-Depth Comparison of Temp and Static
Key Analysis Points:
· Misconceptions about Temporary Variables: The most common mistake is reading uninitialized temporary variables. Since their initial value is random, this can lead to unpredictable program behavior and make debugging difficult. Best practice: assign a value before use.
· Advantages of Static Variables: They provide “memory” capability for the FB, embodying the object-oriented programming concept in PLCs. A FB combined with its static variables can effectively simulate the behavior and state of a device object (such as a motor or valve).
· Differences from Global Variables (M area, global DB):
· Global Variables: Accessible by any program block, which undermines program encapsulation, easily leading to “spaghetti code” and causing unpredictable interactions between different parts of the program, a major taboo in large projects.
· Intermediate Variables (Temp/Static): Their scope is local, limited to the program block that declares them. This aligns with the software engineering principles of high cohesion and low coupling, forming the foundation of structured programming.
4. Summary of Characteristics
1. Locality: The scope is limited to the program block in which it is declared, avoiding naming conflicts and accidental modifications.
2. Structuring: It is a key tool for achieving modular and functional programming.
3. Flexibility:
· Temp: Lightweight, used and discarded as needed, suitable for instantaneous operations.
· Static: Provides persistent storage, the source of the FB’s “memory” function.
4. Safety: Compared to global variables, the use of local variables greatly enhances program stability and predictability.
5. Efficiency: Proper use of temporary variables can optimize program structure, and sometimes the compiler can optimize their storage as well.
5. Core Requirements and Best Practices
1. Mandatory Initialization: For temporary variables (Temp), they must be assigned a value in any possible execution path of the program before their value is read. This is a golden rule for writing reliable PLC programs.
2. Minimize Scope: Prefer using local variables (Temp/Static) over global variables. If a piece of data is only used within a specific FB, it should never be placed in a global DB or M area.
3. Clear Naming: Use Hungarian notation or similar methods to indicate the data type and purpose through variable name prefixes (e.g., i_TempCount, r_StaticSpeed, b_StatusOK), enhancing code readability.
4. Reasonable Type Selection:
· Use Temp for intermediate results that are only needed within the current scan cycle.
· Use Static for data that needs to be maintained across multiple calls of the FB.
5. Avoid Misuse of Static: While Static is very useful, it should not be used as a substitute for global variables. Its data still belongs to a specific FB instance and should be strictly used for functions related to that instance.
—
Conclusion:
The “intermediate variables” in Siemens PLCs (represented by temporary and static variables) are by no means dispensable supporting roles; they are core elements in building modern, structured, and maintainable automation programs. A deep understanding and correct application of them is a key step in transforming from a “wiring technician” programming mindset to a “software engineer” system design mindset.

Long press this QR code to follow and share more electromechanical knowledge.