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
| Declaration | Visible in | Typical use |
|---|---|---|
VAR_GLOBAL | Whole resource (all POUs) | I/O mapping, shared data, HMI tags |
VAR in a PROGRAM / FB | That POU; FB variables live in the instance | State, timers, internal memory |
VAR_INPUT / VAR_OUTPUT / VAR_IN_OUT | Interface of FB/FC | Parameters |
VAR_TEMP | One execution | Intermediate results, not kept |
VAR CONSTANT | Read-only | Limits, scaling constants |
VAR RETAIN / PERSISTENT | Kept after power loss / download | Counters, 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.
Connections
Open in Knowledge Graph →Loading the map…
Sources
- IEC 61131-3:2025 Programmable controllers — Part 3: Programming languages — IEC
- PLCopen — IEC 61131-3 — PLCopen
- TwinCAT 3 PLC Library Tc2_Standard — Beckhoff Automation
Updated 2026-10-02