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

  1. What Is Structured Text?
  2. Why Use Structured Text for PLCs?
  3. Basic Syntax and Structure
  4. Common Programming Constructs
  5. Real-World Examples
  6. Best Practices for ST Programming
  7. When to Use ST vs Ladder Logic
  8. 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?

BenefitDescription
Powerful LogicEasily handles nested IF/ELSE, FOR loops, CASE statements
Compact CodeFewer lines than equivalent ladder logic
ReusabilitySupports functions, function blocks, and libraries
ScalabilityIdeal for modular and large-scale control applications
Integration FriendlyEasier 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

ConstructUse Case
IF/THEN/ELSEDecision-making logic
CASEMode control, state machines
FOR/WHILELoops, data array processing
FUNCTIONReturn a single value
FUNCTION_BLOCKEncapsulate code with memory (like an object)
ARRAYSStore 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

PracticeBenefit
Use meaningful variable namesImproves readability and team collaboration
Comment generouslyExplains logic and avoids confusion later
Modularize with functions/FBsEnhances reusability and structure
Handle all possible statesPrevents bugs due to unhandled logic paths
Initialize variables explicitlyAvoids unexpected behaviors at startup
Test each block independentlyEasier 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

CriteriaUse Ladder LogicUse 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.

Share The Post :

Leave a Reply