Skip to content

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.

Lesson 6 of 3411 minIEC 61131-3

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_VAR

The sections

Section

From / to

Writable inside?

VAR_INPUT

comes from the caller

no, read-only

VAR_OUTPUT

goes to the caller

yes

VAR_IN_OUT

a reference to the caller's variable

yes, and the caller sees it

VAR

internal

yes

VAR_TEMP

internal, cleared every scan

yes

VAR_GLOBAL

shared across programs

yes

VAR CONSTANT

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_VAR

An 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_VAR

Now 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

  1. Run the program and press START: the belt runs and the pusher rejects the tall boxes.
  2. After 3 rejects the belt stops and the lamp lights, because Target starts at 3.
  3. Change Target to 5 in its declaration line only and download again.
  4. 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

  1. 01What is Structured Text, and when to use it instead of ladder
  2. 02How a PLC runs your code: the scan cycle and the process image
  3. 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
  4. 04Your first program: starting a pump with seal-in logic
  5. 05Data types: BOOL, INT, REAL, TIME and the rest
  6. 06Declaring variables: VAR, VAR_INPUT and RETAIN
  7. 07Literals and constants: 16#FF, 2#1010 and T#5s
  8. 08Type conversion and scaling a 4-20 mA signal
  9. 09Assignment and arithmetic in ST: := and operator precedence
  10. 10Boolean logic, comparisons and bit operations
  11. 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
  12. 12IF, ELSIF and ELSE: the rules and when to avoid them
  13. 13The CASE statement and your first state machine
  14. 14The FOR loop and the watchdog
  15. 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
  16. 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
  17. 17Timers: TON, TOF and TP
  18. 18Counters: CTU, CTD and CTUD
  19. 19SR and RS flip-flops: which one wins
  20. 20Writing your own function
  21. 21Writing a function block
  22. 22Arrays in Structured Text
  23. 23Structs and enumerations
  24. 24References and pointers
  25. 25The state machine pattern: a batch sequence that cannot get stuck
  26. 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
  27. 27Analogue signal processing: counts to engineering units, NE43 and filtering
  28. 28Closed-loop control in ST: hysteresis first, then a practical PID
  29. 29Motors and valves in practice: permissives, feedback and travel time
  30. 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
  31. 31Testing and debugging ST: the watch table, forcing and simulation
  32. 32An ST style guide: naming, comments, constants and version control
  33. 33Final project: a complete tank station with a state machine and alarms
  34. 3420 interview questions on ST and PLC programming, with answers