Syntax, Examples, and Best Practices

Introduction
Structured Text (ST) is one of the five languages defined in the IEC 61131-3 standard for programmable logic controllers (PLCs). As modern automation systems grow in complexity, ST has become an increasingly popular choice among engineers for its powerful syntax, clarity, and ability to handle complex logic, especially when traditional ladder logic becomes cumbersome.
As an automation expert with 30 years of experience, I’ve used Structured Text to streamline control strategies in industries ranging from petrochemical to pharmaceuticals. In this article, I’ll break down the essentials of ST: its syntax, usage examples, and best practices. Whether you’re transitioning from ladder logic or starting from scratch, this guide will help you write robust and scalable PLC code using ST.
Table of Contents
- What Is Structured Text?
- Why Use Structured Text for PLCs?
- Basic Syntax and Structure
- Common Programming Constructs
- Real-World Examples
- Best Practices for ST Programming
- When to Use ST vs Ladder Logic
- Conclusion
What Is Structured Text?
Structured Text is a high-level, text-based programming language similar to Pascal or C. It allows complex tasks such as data manipulation, array handling, state machines, and math operations that would be difficult to manage in ladder diagrams.
It is typically used in:
- Batch control systems
- Process automation
- Advanced motion control
- Complex data evaluation
Why Use Structured Text for PLCs?
| Benefit | Description |
|---|---|
| Powerful Logic | Easily handles nested IF/ELSE, FOR loops, CASE statements |
| Compact Code | Fewer lines than equivalent ladder logic |
| Reusability | Supports functions, function blocks, and libraries |
| Scalability | Ideal for modular and large-scale control applications |
| Integration Friendly | Easier integration with MES/ERP and OPC-UA systems |
Basic Syntax and Structure
Structured Text is block-structured and case-insensitive. Most platforms (Siemens TIA Portal, Allen-Bradley Studio 5000, Codesys) follow IEC standards.
Variable Declaration
VAR
StartButton : BOOL;
MotorOn : BOOL;
Counter : INT;
END_VAR
IF-THEN-ELSE
IF StartButton THEN
MotorOn := TRUE;
ELSE
MotorOn := FALSE;
END_IF;
CASE Statement
CASE Mode OF
1: StartMotor();
2: StopMotor();
3: ResetSystem();
ELSE
Fault := TRUE;
END_CASE;
FOR Loop
FOR i := 1 TO 10 DO
Total := Total + i;
END_FOR;
Common Programming Constructs
| Construct | Use Case |
| IF/THEN/ELSE | Decision-making logic |
| CASE | Mode control, state machines |
| FOR/WHILE | Loops, data array processing |
| FUNCTION | Return a single value |
| FUNCTION_BLOCK | Encapsulate code with memory (like an object) |
| ARRAYS | Store values like sensor data or batch steps |
Real-World Examples
Example 1: Motor Control with Interlock
IF StartBtn AND NOT Overload THEN
MotorRun := TRUE;
ELSE
MotorRun := FALSE;
END_IF;
Example 2: Level Control Logic
IF TankLevel > HighSetpoint THEN
InletValve := FALSE;
ELSIF TankLevel < LowSetpoint THEN
InletValve := TRUE;
END_IF;
Example 3: Batch Step Sequencing Using CASE
CASE Step OF
0: IF Start THEN Step := 10; END_IF;
10: IF ValveOpen THEN Step := 20; END_IF;
20: IF Timer.Q THEN Step := 30; END_IF;
30: StopProcess();
END_CASE;
Best Practices for ST Programming
| Practice | Benefit |
| Use meaningful variable names | Improves readability and team collaboration |
| Comment generously | Explains logic and avoids confusion later |
| Modularize with functions/FBs | Enhances reusability and structure |
| Handle all possible states | Prevents bugs due to unhandled logic paths |
| Initialize variables explicitly | Avoids unexpected behaviors at startup |
| Test each block independently | Easier debugging and maintenance |
Documentation Tip:
Include state diagrams and flowcharts alongside ST code to help non-programmers understand logic.
When to Use ST vs Ladder Logic
| Criteria | Use Ladder Logic | Use Structured Text |
| Simple ON/OFF control | ✅ | |
| Graphical visualization needed | ✅ | |
| Complex math or data handling | ✅ | |
| Sequences with many steps | ✅ | |
| Rapid prototyping and reuse | ✅ |
Many modern systems use both languages, selecting the right tool for the right task.
Conclusion
Structured Text empowers automation professionals to write clean, logical, and scalable control code for today’s advanced manufacturing environments. Its robust syntax, flexibility, and readability make it the perfect fit for complex projects where ladder logic might fall short.
If you’re aiming to take your PLC programming skills to the next level, Structured Text isn’t just a language—it’s a mindset for designing smarter, more maintainable systems.
