How to Write Structured Text Program for PLC - Simple Guide
Write a structured text program for PLC by using
VAR to declare variables, IF statements for logic, and END_IF to close conditions; for example: IF Start AND NOT Stop THEN Motor := TRUE; ELSE Motor := FALSE; END_IF;.Examples
InputStart = TRUE
OutputMotor = TRUE
InputStart = FALSE
OutputMotor = FALSE
InputStart = TRUE, Stop = TRUE
OutputMotor = FALSE
How to Think About It
To write a structured text program for a PLC, first decide what inputs and outputs you need. Use
VAR to declare these variables. Then write simple logic using IF and ELSE statements to control outputs based on inputs. End each block with END_IF to keep the program clear and structured.Algorithm
1
Declare input and output variables.2
Check input conditions using IF statements.3
Set output variables based on conditions.4
Close each IF block with END_IF.5
Repeat for all control logic needed.Code
plc_programming
VAR Start : BOOL; Stop : BOOL; Motor : BOOL; END_VAR IF Start AND NOT Stop THEN Motor := TRUE; ELSE Motor := FALSE; END_IF; // Output the Motor state // (In real PLC, Motor controls a device)
Output
Motor = TRUE if Start=TRUE and Stop=FALSE; otherwise Motor = FALSE
Dry Run
Let's trace Start=TRUE and Stop=FALSE through the code
1
Check condition
Start=TRUE, Stop=FALSE, condition Start AND NOT Stop is TRUE
2
Set Motor
Motor := TRUE
3
Output result
Motor = TRUE
| Start | Stop | Condition | Motor |
|---|---|---|---|
| TRUE | FALSE | TRUE | TRUE |
| FALSE | FALSE | FALSE | FALSE |
| TRUE | TRUE | FALSE | FALSE |
Why This Works
Step 1: Variable Declaration
Use VAR block to declare all inputs and outputs so PLC knows what data it handles.
Step 2: Logic Condition
The IF statement checks if Start is TRUE and Stop is FALSE to decide Motor state.
Step 3: Output Assignment
Based on the condition, Motor is set to TRUE or FALSE, controlling the device connected.
Alternative Approaches
Using CASE statement
plc_programming
VAR Command : INT; Motor : BOOL; END_VAR CASE Command OF 1: Motor := TRUE; 2: Motor := FALSE; ELSE Motor := FALSE; END_CASE;
CASE is useful for multiple discrete commands but less flexible for complex conditions.
Using Boolean expressions directly
plc_programming
VAR Start : BOOL; Stop : BOOL; Motor : BOOL; END_VAR Motor := Start AND NOT Stop;
Direct assignment is simpler and efficient for straightforward logic without IF blocks.
Complexity: O(1) time, O(1) space
Time Complexity
The program runs in constant time because it only evaluates fixed logical conditions without loops.
Space Complexity
Uses constant space for declared variables; no dynamic memory allocation.
Which Approach is Fastest?
Direct Boolean assignment is fastest and simplest; IF statements add clarity but slightly more code.
| Approach | Time | Space | Best For |
|---|---|---|---|
| IF statements | O(1) | O(1) | Clear logic with multiple conditions |
| CASE statement | O(1) | O(1) | Multiple discrete commands |
| Direct Boolean assignment | O(1) | O(1) | Simple true/false logic |
Always declare your variables first in the VAR block before using them in logic.
Forgetting to close IF statements with END_IF causes syntax errors in structured text.