PLC variables, data types, programs and tasks

How IEC 61131-3 organizes data: variable scopes, direct addresses (%I, %Q, %M), elementary and user-defined types, and how programs are assigned to tasks.

Variable scopes

DeclarationVisible inTypical use
VAR_GLOBALWhole resource (all POUs)I/O mapping, shared data, HMI tags
VAR in a PROGRAM / FBThat POU; FB variables live in the instanceState, timers, internal memory
VAR_INPUT / VAR_OUTPUT / VAR_IN_OUTInterface of FB/FCParameters
VAR_TEMPOne executionIntermediate results, not kept
VAR CONSTANTRead-onlyLimits, scaling constants
VAR RETAIN / PERSISTENTKept after power loss / downloadCounters, settings

Direct addresses

%IX0.0 (input bit), %QW4 (output word), %MD10 (memory double word). Modern projects bind I/O to symbolic names (Motor1_RunFb AT %IX2.3 : BOOL;) and use the symbols in code — never raw addresses scattered through the program.

Data types

Elementary: BOOL, SINT…LINT, USINT…ULINT, REAL, LREAL, TIME, DATE, TIME_OF_DAY, DATE_AND_TIME, STRING, WSTRING, BYTE/WORD/DWORD/LWORD (bit strings).

User-defined (derived):

TYPE E_ValveState : (Closed, Opening, Open, Closing, Fault); END_TYPE

TYPE ST_Motor :
STRUCT
    RunCmd   : BOOL;
    Running  : BOOL;
    Speed    : REAL;      (* rpm *)
    FaultCode: UINT;
END_STRUCT
END_TYPE

VAR_GLOBAL
    Motors : ARRAY[1..12] OF ST_Motor;
END_VAR

Structures and arrays make HMI/SCADA mapping and code reuse simple; enumerations make state machines readable.

Programs and tasks

A task defines when code runs: cyclic (every 10 ms), freewheeling (as fast as possible) or event-triggered. Programs are assigned to tasks. Put fast control (motion, safety-relevant timing) in a short cyclic task and slow work (statistics, communication handling) in a slower one; data shared between tasks must be consistent — copy it once per cycle instead of reading it many times.

Loading the map…

Sources

  1. IEC 61131-3:2025 Programmable controllers — Part 3: Programming languages — IEC
  2. PLCopen — IEC 61131-3 — PLCopen
  3. TwinCAT 3 PLC Library Tc2_Standard — Beckhoff Automation

Updated 2026-10-02