In-Depth Analysis of Siemens PLC Programming: From Concept to Practice

In-Depth Analysis of Siemens PLC Programming: From Concept to Practice

Learning and mastering Siemens PLC programming cannot be limited to just instructions and software operations. It is an art of designing and implementing automation control logic. The core idea can be summarized as: translating the actions of physical devices and process flows into deterministic logical relationships that the PLC can understand and execute.

1. Core Thinking Method: Think Like a “System Designer”

Before writing the first line of code, the real programming work has already begun. We need to establish the following core ways of thinking:

1. Task Decomposition and Modular Thinking:

· Idea: Do not view the entire control system as a large, indivisible whole. Instead, decompose it into several relatively independent sub-tasks or subsystems with clear interfaces.

· Example: An automated filling production line can be decomposed into modules such as [Conveyor Control], [Bottle Detection], [Filling Valve Control], [Weighing Measurement], [Cap Control], etc. Each module is programmed and debugged independently, and finally coordinated through the main program.

· Benefits: The program structure is clear, making it easy to write, debug, maintain, and extend functionality. When the filling valve needs to be upgraded, you only need to modify the corresponding module without affecting other parts.

2. State Machine Thinking:

· Idea: Many devices or processes can be described as a collection of “states”, with transitions between states triggered by specific “conditions”.

· Example: A simple motor control can have states such as stopped, starting, running, and fault. The transition condition from stopped to starting is “start button pressed”, and from starting to running is “start delay time reached and no fault”.

· Benefits: This way of thinking makes the program logic very rigorous, avoiding ambiguity, especially suitable for handling sequential control processes.

3. Event-Driven and Periodic Scanning Thinking:

· Idea: Understanding how the PLC works is its essence. The PLC program is executed in a loop, with one scan cycle including: input sampling -> program execution -> output refresh.

· In-Depth Analysis: This means that the value of the input point (e.g., I0.0) you read in the program is a “snapshot” fixed at the beginning of the cycle. Throughout the program execution, even if the actual I0.0 changes, the program’s I0.0 will not change until the next scan cycle. Similarly, your write operation to the output point (e.g., Q0.0) will not immediately change the physical output but will be updated collectively at the end of the cycle during the “output refresh” phase.

· Insight: This working method explains why “logical lag” occurs and guides us on how to avoid pitfalls like dual coil outputs. You must think about the program execution flow from the perspective of the entire scan cycle.

4. Signal Flow and Data Flow Thinking:

· Idea: View the program as a system that processes information. Focus on where the “signals” come from (sensors, buttons), how they are logically processed (your program), and where they ultimately go (actuators, indicator lights, HMI).

· Example: In a level control system, a level sensor (signal source) sends a “high level” signal, and your program (processing center) determines that it will send a “close” signal to the water inlet valve (execution endpoint).

2. Systematic Programming Steps Explained

Following a rigorous process is key to project success.

Step 1: Requirement Analysis and Functional Specification Definition (the most important step)

· What to do: Engage in deep communication with mechanical and electrical engineers and end users to thoroughly understand what the control object is, what actions need to be completed, and what the process flow diagram looks like.

· Deliverables:

· IO Point Table (in mind): Even if you don’t list it in a table, you must clearly understand: what inputs (e.g., buttons, sensors, limit switches) are there? What outputs (e.g., contactors, indicator lights, solenoid valves) are there? Which PLC addresses do they correspond to?

· Control Logic Description: Clearly describe the prerequisites, execution process, and termination conditions for each action in words or flowcharts. For example: “When the start button SB1 is pressed and there is no emergency stop alarm, the conveyor motor M1 starts until it stops after reaching the limit SQ1.”

Step 2: Hardware Configuration and Address Planning

· In the software (e.g., TIA Portal), configure the hardware based on the actual PLC model and expansion modules. This step is equivalent to building a “hardware stage” for your program.

· Plan the symbol table/variable table: This reflects good programming habits. Do not just use absolute addresses like “I0.0”, “Q0.1”, but assign meaningful symbolic names to them, such as Start_Button, Motor_Run. This greatly improves the readability of the program.

Step 3: Program Architecture Design

· Based on the modular thinking from the first step, design the overall structure of the program.

· Typical structure (taking Siemens S7-300/400/1500 as an example):

· OB1 (Main Cycle Organization Block): The “core” of the program, which cyclically calls other function blocks. Usually, the calling instructions for each module and the main coordination logic are placed here.

· FC/FB (Function/Function Block): The core that implements specific functional modules. FC is used for non-memory functions (e.g., calculations), while FB is used for memory functions (e.g., motor control, valve control, as it has a dedicated data block DB to store state).

· DB (Data Block): Stores program data, such as recipes, device parameters, intermediate states, etc. It is the data center of the program.

· Other OBs: Such as OB100 (Startup Organization Block), OB35 (Cyclic Interrupt Organization Block), OB82 (Diagnostic Interrupt Organization Block), etc., used to handle specific events.

Step 4: Modular Programming and Debugging

· Tackle one by one: According to the designed architecture, start writing each FC/FB.

· Programming Language Selection: Siemens PLC supports various languages, the most commonly used are:

· LAD (Ladder Diagram): Intuitive, easy to understand, suitable for simple logical control.

· SCL (Structured Control Language): Similar to high-level languages (like Pascal), very suitable for complex mathematical calculations, data processing, loop operations, and algorithm implementations.

· Internal Debugging: For each module written, use the simulation function of TIA Portal (PLCSIM) to test, simulate input signals, and observe whether the output and internal variable changes meet expectations.

Step 5: System Integration and Coordination

· Call and integrate all debugged FC/FB in OB1 or through other means.

· Handle module interfaces: Ensure that the output of one module can correctly serve as the input for another module.

· Overall simulation and on-site debugging: Conduct a complete system simulation, then perform power-on coordination on real devices to resolve issues arising from hardware-software integration.

Step 6: Documentation and Maintenance

· Write Comments: Add detailed and clear comments for networks, variables, and blocks in the program.

· Maintenance Manual: Document the program structure, key logic descriptions, and common troubleshooting methods.

3. In-Depth Analysis of Core Principles

1. The essence of programming is “mapping”:

The PLC program is a mapping of physical logic in the real world to the virtual world. Your task is to establish an accurate and reliable mapping relationship. The pressing of a button (physical event) maps to the conduction of an input point (electrical event), then maps to a Boolean variable’s value of 1 (software event), and through logical operations, ultimately maps to the conduction of an output point (electrical event), which then maps to the engagement of a contactor and the rotation of a motor (physical event).

2. The core of data types is “meaning” and “range”:

· Bool (bit): Represents a switch quantity, only 0/1, True/False. Its meaning lies in “state”, such as start, stop, fault.

· Int / Word / DWord (integer/word/double word): Represents quantity, numbering, set values. Its meaning lies in “value” and “comparison”.

· Real (floating point): Represents analog quantities, such as temperature, pressure, flow. Its meaning lies in “precision” and “process”.

Choosing the correct data type is not only a syntactical requirement but also a reflection of logical rigor. Using a Word to store a switch state is wasteful, and using a Bool to represent a temperature value is incorrect.

3. The fundamental difference between functions (FC) and function blocks (FB):

· FC (no memory): Like the “sine function Sin(X)” in a calculator. Every time you call Sin(30°), it returns 0.5. It does not remember what you called last time. FC processes data and outputs results without storing any internal state. Suitable for pure calculations, type conversions, etc.

· FB (with memory): Like the “cruise control” in your car. When you first press the “SET” button (calling FB), it sets the speed and maintains it (memory state). Later, when you press “ACC” to accelerate (calling the same FB instance again), it increases based on the previously remembered speed. This “memory” is stored in its dedicated background data block (Instance DB). Therefore, FB is very suitable for controlling a stateful device, such as a motor.

Conclusion

Siemens PLC programming is by no means a simple stacking of instructions. It requires engineers to possess:

· Top-level design capabilities (modularization, state machines),

· Understanding of underlying operating mechanisms (periodic scanning),

· Rigorous logical thinking (signal flow, data types),

· And an engineering practice method (a complete process from analysis to debugging).

When you begin to view a control task through the eyes of a “system designer”, depict its behavior with “state machine” thinking, and deeply understand the core workings of PLC “periodic scanning”, you will truly grasp the essence of Siemens PLC programming, enabling you to design stable, efficient, and maintainable industrial automation systems.

In-Depth Analysis of Siemens PLC Programming: From Concept to Practice

Long press this QR code to share more electromechanical knowledge

Leave a Comment