Skip to main content

Structured Text Programming in Studio 5000 — Syntax and Examples

By NFM Consulting 2 min read

Key Takeaway

How to write Structured Text routines in Studio 5000 for Allen-Bradley PLCs — syntax, data types, control structures, and practical examples for analog scaling and sequencing.

Quick Answer

Structured Text (ST) is one of four IEC 61131-3 programming languages supported by Studio 5000. It uses high-level text syntax similar to Pascal for math-intensive calculations, data manipulation, and complex algorithms that would be cumbersome in Ladder Diagram.

What Is Structured Text in IEC 61131-3?

Structured Text is a high-level programming language defined in the IEC 61131-3 standard for programmable controllers. Unlike Ladder Diagram (graphical, relay-based) or Function Block Diagram (graphical, block-based), ST uses text-based statements, expressions, and control structures familiar to software developers.

When to Use Structured Text vs Ladder Logic

  • Use Structured Text for: Complex math, string manipulation, data table operations, state machines, and algorithms that require nested conditionals or loops.
  • Use Ladder Logic for: Discrete I/O control, motor circuits, interlocks, and logic that maintenance electricians need to troubleshoot in the field.
  • Mix both: Each routine in a Studio 5000 project can use a different language. Use ST for calculations and Ladder for I/O control in the same program.

Data Types

  • BOOL — True/False (1/0)
  • SINT — 8-bit signed integer (-128 to 127)
  • INT — 16-bit signed integer (-32768 to 32767)
  • DINT — 32-bit signed integer
  • REAL — 32-bit floating point
  • STRING — Text string (up to 82 characters default)

Assignment Statements and Expressions

// Assignment uses :=
TotalFlow := GrossFlow - WaterCut;
Efficiency := (OutputPower / InputPower) * 100.0;
PumpRunning := StartCommand AND NOT FaultActive;

IF / THEN / ELSIF / ELSE / END_IF

IF Pressure > HighHighLimit THEN
    AlarmLevel := 4;
    ShutdownValve := 1;
ELSIF Pressure > HighLimit THEN
    AlarmLevel := 3;
ELSIF Pressure < LowLimit THEN
    AlarmLevel := 2;
ELSE
    AlarmLevel := 0;
END_IF;

FOR Loops and WHILE Loops

// Sum 10 tank levels
TotalVolume := 0.0;
FOR i := 0 TO 9 DO
    TotalVolume := TotalVolume + TankLevel[i];
END_FOR;

// Wait loop (use cautiously — blocks scan)
WHILE NOT SensorReady DO
    // Check sensor status
END_WHILE;

CASE Statements

CASE SequenceStep OF
    0: // Idle
        IF StartCommand THEN SequenceStep := 10; END_IF;
    10: // Open inlet valve
        InletValve := 1;
        IF InletValveOpen THEN SequenceStep := 20; END_IF;
    20: // Start pump
        PumpStart := 1;
        IF PumpRunning THEN SequenceStep := 30; END_IF;
    30: // Running
        IF StopCommand THEN SequenceStep := 40; END_IF;
    40: // Shutdown
        PumpStart := 0;
        InletValve := 0;
        SequenceStep := 0;
END_CASE;

Calling Add-On Instructions from Structured Text

// Call an AOI named PumpControl
PumpControl_Instance(
    IN_Start := StartPB,
    IN_Stop := StopPB,
    IN_Fault := MotorFault
);
MotorContactor := PumpControl_Instance.OUT_Run;

Example: Analog Value Scaling

// Scale 4-20mA raw count (0-32767) to 0-500 PSIG
ScaledPressure := (RawInput - 6554.0) / (26214.0 - 6554.0) * 500.0;
IF ScaledPressure < 0.0 THEN ScaledPressure := 0.0; END_IF;
IF ScaledPressure > 500.0 THEN ScaledPressure := 500.0; END_IF;

For SCADA-side scaling, see how Geo SCADA handles calculated values using Basic scripting.

Frequently Asked Questions

Ready to Get Started?

Our engineers are ready to help with your automation project.