The short answer
Structured Text
Structured text is the high-level text language defined by IEC 61131-3. It has assignments, IF and CASE branches, FOR and WHILE loops, and calls to functions and function blocks, and it is the right language for calculations, data handling, and logic with many conditions. It is harder to troubleshoot online than ladder, so in a water or wastewater program it is best used inside well-named function blocks that ladder calls, not as a replacement for the ladder an electrician expects to see.
Key points
- Use it for arithmetic, data handling, state machines, and anything with a loop.
- Keep discrete motor and valve control in ladder where it is troubleshot online.
- One statement per line, named constants, and comments that say why, not what.
- Every IF that writes an output has an ELSE that writes it too, or the output holds its last value forever.
- A loop that can run long stalls the scan. Bound every loop.
What it looks like
Structured text reads like Pascal. Statements end in semicolons, assignment is a colon-equals, comparisons produce booleans, and blocks are delimited by keywords rather than braces. The example computes a scaled analog value with clamping and a validity flag, which in ladder would take a dozen rungs and in structured text takes a dozen lines that read top to bottom.
(* Scale a 4-20 mA input to engineering units, with range checks. *)
Raw_Span := Raw_Max - Raw_Min;
IF Raw_Span > 0 THEN
Level_ft := (Raw - Raw_Min) / Raw_Span * (EU_Max - EU_Min) + EU_Min;
ELSE
Level_ft := EU_Min; (* bad configuration: do not divide by zero *)
END_IF;
(* Under-range and over-range flags, with a small margin for calibration error. *)
Level_Under := Raw < (Raw_Min - Raw_Margin);
Level_Over := Raw > (Raw_Max + Raw_Margin);
Level_OK := NOT Level_Under AND NOT Level_Over AND Input_Card_OK;
(* Clamp the scaled value, but never let a clamped value look valid. *)
IF Level_ft < EU_Min THEN Level_ft := EU_Min; END_IF;
IF Level_ft > EU_Max THEN Level_ft := EU_Max; END_IF;Every element of the language is visible: comments in parentheses and asterisks, assignment, arithmetic, IF with ELSE, comparisons producing booleans, and named variables in place of addresses.
Control structures
| Structure | Use | Caution |
|---|---|---|
| IF ... ELSIF ... ELSE ... END_IF | Conditional assignment | Without an ELSE, an output assigned only in the IF branch keeps its last value when the condition is false. Sometimes that is intended; usually it is a bug. |
| CASE ... OF ... END_CASE | Selection on an integer or enumeration; the natural form of a state machine | Include an ELSE branch that handles an unexpected value, and write the state to a monitored tag. |
| FOR ... TO ... DO ... END_FOR | Iterate over an array: scan all pumps, all zones, all alarms | The loop runs to completion inside one scan. A large or nested loop extends the scan time; a FOR loop in a fast task is a watchdog risk. |
| WHILE ... DO ... END_WHILE and REPEAT ... UNTIL | Loop until a condition | A condition that never becomes true stalls the scan and trips the watchdog. Bound them with a counter, or avoid them in controller code. |
| Function and function block calls | Call a scaling function, a PID block, a pump control block | Function block instances must be declared; calling one changes its stored state. |
Where it belongs in a plant program
The question in a water or wastewater plant is not whether structured text is a good language; it is who will be standing in front of the panel at 2 a.m. An operator or an electrician troubleshooting online reads ladder: they see the contacts, the coils, and their states. Structured text shows them a page of code with values in the margin. That does not make structured text wrong; it makes it the wrong place for the logic they need to see.
- Discrete control of motors, valves, and interlocks in ladder, calling function blocks whose internals may be structured text.
- Analog scaling, validation, filtering, totalizing, and calculations in structured text inside function blocks.
- State machines and sequences in structured text with a CASE statement, with the current state and step description written to tags the HMI shows.
- Loops over arrays of equipment in structured text, never in ladder.
- Communication message handling, string building, and data packing in structured text.
Conventions that keep it maintainable
- 01One statement per line. A line with three assignments hides two of them.
- 02Named constants for every number that is not zero or one. A 0.85 in the middle of a calculation is a question nobody can answer in five years.
- 03Comments that explain intent and units. The code already says what it does.
- 04Consistent naming: prefix by equipment, suffix by type, no abbreviations that only the author understands.
- 05Every IF that writes an output has an ELSE that writes it too, unless the hold is deliberate and commented.
- 06No magic addresses. Every variable is declared with a type and a meaning; the I/O mapping is in one place.
- 07Short blocks. A block longer than a screen is two blocks.
- 08Unit tests where the platform allows simulation, and at minimum a documented set of input cases with expected outputs.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Output assigned only inside an IF | Output sticks at last value when the condition ends | Add the ELSE, or assign a default before the IF |
| Integer division | Fractions truncated silently; 7 / 2 = 3 | Convert to REAL before dividing |
| Comparing REALs for equality | Almost never true | Compare against a tolerance |
| Unbounded WHILE | Scan stalls; watchdog fault | Use FOR with a fixed bound, or a counter guard |
| State held in a local variable that is not retentive | State resets on power cycle | Declare it retentive, or reinitialize deliberately on first scan |
| Same output written from two places | Last write wins, and the first is invisible | One owner per output |
| Logic that the HMI also does | Two behaviors that drift apart | Logic in the controller, display in the HMI |
Frequently asked questions
- Is structured text faster than ladder?
- Execution speed is similar on most platforms; the compiler produces comparable code. Structured text is faster to write for calculations and slower to troubleshoot for discrete logic. Choose by readability for the person who will maintain it, not by execution speed.
- Can I write the whole program in structured text?
- You can, and some integrators do. In a utility where operators and electricians troubleshoot online, an all-text program is a maintenance problem they will resent for the life of the plant. Keep the discrete control they look at in ladder.
- How do I troubleshoot structured text online?
- Most platforms show live values beside each variable and let you set a breakpoint or a watch. The better answer is to write the block so it publishes its state, step, and hold reasons to tags, and to troubleshoot from those on the HMI or the tag monitor.
- Does structured text move between platforms?
- Better than any other language in the standard. The core syntax is the same; timers, function block declarations, and vendor extensions differ. Expect editing, not a rewrite.
Related topics
- IEC 61131-3The standard behind PLC programming languages: the five languages, the common elements of data types, variables, and program organization units, what conformance actually means, and how the standard shows up in real platforms.
- Ladder Logic FundamentalsRungs, contacts, coils, and the relay conventions the language inherited — plus the patterns you will meet in almost every industrial program.
- State Machines in PLC ProgramsWriting sequences as explicit states instead of tangled interlock chains: what a state is, how transitions are written, the pattern in structured text and in ladder, publishing the state to the HMI, and the failure handling every state machine needs.
- Analog Scaling: Raw Counts to Engineering UnitsThe arithmetic that turns an analog card reading into feet, psi, or gallons per minute — plus the range mismatches that silently corrupt it.
- The Watchdog TimerWhat the scan watchdog protects against, why it faults the controller instead of pausing it, what makes a scan run long, and why raising the limit is the wrong first response.
- Retentive MemoryWhat survives a power cycle in a controller and what does not, how retention is backed up by battery, capacitor, or nonvolatile storage, what should be retentive, and how retained data gets lost.
Direct contact
Have a controls question?
Reach Eric Sullivan directly about anything on this site, a controls or automation topic, or one of his personal projects.