What Is Structured Text?
Structured text, or ST, is the high-level, text-based PLC programming language defined by IEC 61131-3. If ladder logic looks like an electrical diagram, structured text looks like traditional software - with variables, IF statements, loops, and math. It is the language controls engineers reach for when logic gets too complex or too calculation-heavy to express cleanly in rungs or blocks.
Structured Text in one line: Structured text (ST) is a high-level, Pascal-like PLC programming language defined in IEC 61131-3, using constructs such as IF/THEN, FOR loops, and arithmetic expressions to implement complex logic and calculations that would be cumbersome in ladder logic.
How Structured Text Works
Structured text uses familiar programming constructs: assignment (:=), conditionals (IF/ELSIF/ELSE), loops (FOR, WHILE, REPEAT), CASE statements, and arithmetic and logical operators. Variables are declared with defined data types, and the code executes top to bottom each scan. To anyone who has written Pascal, C, or Python, it reads immediately - though it still runs inside the PLC's deterministic scan cycle like any other IEC 61131-3 language.
This textual form makes ST compact and expressive. A calculation that would need dozens of ladder rungs - iterating over an array, computing a totalized volume, or running a complex state machine - can be a few clear lines of structured text. It is also easy to comment, version, and review, which appeals to teams applying software-engineering discipline to control code.
When to Use Structured Text
ST is the right tool for math-intensive and algorithmic logic: flow calculations, unit conversions, data manipulation, recipe handling, and complex conditional logic. It is far more readable than ladder for anything involving loops or arrays. Many engineers use it selectively - writing the bulk of a program in ladder or FBD for readability, and dropping into ST for the parts that genuinely need it.
The trade-off is audience. Ladder was designed so electricians and technicians could read it at 2 a.m.; structured text assumes some programming fluency, which not every field team has. In oil and gas, ST often appears in flow computers and metering logic, where the calculations are inherently mathematical. As with any language, the PLC publishes its results as tags, and a SCADA platform like Merobix reads and trends those tags over the controller's native protocol regardless of how the logic was written.
A Worked Example: A Totalizer in Four Lines
Here is the kind of job ST makes trivial. To accumulate a volume from a flow rate, with the rate in units per hour and the scan interval in seconds:
IF Running THEN
Total := Total + FlowRate * ScanSeconds / 3600.0;
END_IF;
Four lines, readable by anyone. The division by 3600.0 converts the hourly rate to a per-second contribution; multiplying by the scan interval integrates it over time. The same logic in rungs takes a multiply block, a divide block, an add block, and a move instruction, wired together and read in the wrong order forever after.
The example also carries a classic trap. Total is a REAL, and single-precision floating point loses resolution as the accumulated value grows - eventually each small addition rounds away entirely and the totalizer silently flatlines. The standard fixes are periodic rollover into a separate register, double-precision types where the platform offers them, or integer accumulation in the smallest meaningful unit. And when the total is for custody or fiscal purposes, this logic does not belong in the PLC at all: that is what a dedicated flow computer exists for.
ST Pitfalls That Surface at the Worst Time
Most ST bugs are ordinary programming bugs given industrial consequences. An unbounded WHILE loop does not hang a desktop app; it blows the PLC's watchdog and faults the processor. Integer division silently truncates - 3 / 2 is 1 - which corrupts scaling math until someone notices the units are wrong. Division by a process value faults the scan the first time that value is zero, which is usually during the abnormal condition the logic most needed to survive. The defensive habits are the same ones any language teaches: bound every loop, guard every divisor, and treat compiler warnings as errors.
Two more deserve respect. Edge detection: ST evaluates conditions every scan, so a naive 'when the button is pressed, increment the counter' increments every scan the button is held - rising-edge detection with R_TRIG or an explicit last-state variable is mandatory. And retentivity: variables declared retentive keep their value across a power cycle while others restart at initial values; a state machine that mixes the two can wake up in a state that never existed. Deciding what must survive a restart is design work, not a declaration detail.
Keeping ST Reviewable by the Whole Team
ST's power is also its risk: it lets one clever engineer write something nobody else on the team can maintain. A few habits keep it honest:
- Name states and constants - a CASE statement over named values reads like a procedure; magic numbers read like a puzzle.
- Comment intent, not mechanics: say why the deadband exists, not that the line compares two numbers.
- Keep each routine short enough to read on one screen, with one job per routine.
- Keep interlocks and permissives in ladder logic where technicians expect to find them, and use ST for the math and sequencing underneath.
- Export, version, and peer-review the source like any other code, and test changes in simulation before they reach a running unit.
None of this is bureaucracy for its own sake. The ST that causes trouble is rarely wrong when first written; it is wrong two years later, after three undocumented edits by three different people. Process is what lets the fourth person edit it safely, and what lets an auditor or a new hire trust what they are reading.
Frequently Asked Questions
What is the difference between structured text and ladder logic?
Ladder logic is graphical and relay-style, ideal for discrete interlocks and easy for technicians to read; structured text is a high-level text language suited to complex calculations, loops, and algorithms. Both are IEC 61131-3 languages and can coexist in one program.
Is structured text hard to learn?
If you have programmed in any language like Pascal, C, or Python, structured text is quick to pick up because it shares the same constructs. For teams with only electrical or ladder backgrounds, there is a steeper learning curve, which is why many projects use it only where its power is needed.
When should I use structured text over ladder?
Use structured text for math-heavy or algorithmic tasks - flow totalization, unit conversions, array processing, complex state machines - where ladder would become sprawling and hard to follow. Ladder remains a better fit for simple discrete interlocks and permissives.
Does structured text run faster than ladder logic?
Not in any way you should design around. Both compile down to the same controller runtime, and any differences are compiler- and platform-specific. Choose the language per task for readability and maintainability - the team's ability to safely modify the code in the middle of the night is worth more than a marginal scan-time difference.
How do you debug structured text on a live PLC?
Online variable watch and forcing, used sparingly and per site procedures - forcing a value on a running process is an operational decision, not just a programming one. Breakpoints and single-stepping belong in simulation only, since pausing a controller that runs real equipment is not acceptable. Good ST is written to be debuggable: meaningful intermediate variables you can watch beat one dense expression.
Sources and verification
This page references the vendor products and their official documentation published by the organizations below. Editions, product capabilities, and documentation change over time - confirm current requirements and specifications directly with the source.
- Rockwell Automation Literature Library (Allen-Bradley, Studio 5000) - Rockwell Automation
- Siemens SIMATIC and TIA Portal documentation - Siemens
Merobix is not affiliated with, endorsed by, or sponsored by these organizations; their names are used only to identify the standards and products discussed.
Automation services
Need help turning this into a working system?
Merobix integrates SCADA, programs Allen-Bradley and Siemens PLCs, and designs and fabricates industrial control panels.
Meeting requests are reviewed before confirmation.