06Data and variables
Declaring variables: VAR, VAR_INPUT and RETAIN
The declaration section says where a value comes from and where it goes, and RETAIN decides what survives a power cut.
Everything is declared first
ST is strongly typed: there is no such thing as an undeclared variable. The section it goes in defines its role:
VAR_INPUT
Start : BOOL;
SetPoint : REAL := 75.0; (* initial value *)
END_VARThe sections
Section | From / to | Writable inside? |
|---|---|---|
| comes from the caller | no, read-only |
| goes to the caller | yes |
| a reference to the caller's variable | yes, and the caller sees it |
| internal | yes |
| internal, cleared every scan | yes |
| shared across programs | yes |
| never changes | no |
The difference that catches people
VAR keeps its value between scans; VAR_TEMP does not: it starts from zero every scan. Put a counter in VAR_TEMP and it will sit at 1 forever.
RETAIN: what survives a power cut
VAR RETAIN
TotalBatches : DINT;
RunHours : REAL;
END_VARAn ordinary variable returns to its initial value on restart. A RETAIN variable is held in retentive memory and comes back as it was. Use it for totals and running hours, and not for machine state: after a power cut the machine must start from a known safe state, not from the middle of a fill.
Constants instead of magic numbers
VAR CONSTANT
HiAlarm : REAL := 85.0;
OverFill : REAL := 95.0;
END_VARNow IF Level > HiAlarm reads itself, and the limit changes in one place.
Naming
A name starts with a letter or _, takes no spaces or symbols, and is case-insensitive (Pump and PUMP are the same variable). Plant practice: name the variable after the tag on the P&ID, such as LT101_Level.
Try it in the lab
The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.
What to try
- Run the program and press START: the belt runs and the pusher rejects the tall boxes.
- After 3 rejects the belt stops and the lamp lights, because Target starts at 3.
- Change Target to 5 in its declaration line only and download again.
- Try adding Start := TRUE; at the end: the compiler refuses it because Start is a VAR_INPUT.
Terms in this lesson
People find this lesson by searching
VAR structured textVAR_INPUT VAR_OUTPUT VAR_IN_OUTVAR_GLOBAL plcretain variables plcplc variable declarationCONSTANT structured textVAR_TEMP
Course outline
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers