Attention
IronPLC can only run very simple programs. The steps described are accurate but many language features are not yet supported.
Variables and I/O¶
This page explains how IEC 61131-3 programs declare variables and connect them to physical inputs and outputs. For hands-on practice, see the Quick start tutorial.
Variable Declarations¶
Variables are declared inside VAR … END_VAR blocks at the
top of a program, function, or function block:
VAR
Counter : INT := 0;
Temperature : REAL;
MotorOn : BOOL := FALSE;
END_VAR
Each declaration has a name, a type, and an optional initial value. If you omit the initial value, the variable starts at the type’s default (0 for numbers, FALSE for BOOL, empty for STRING).
Variables declared inside a PROGRAM or FUNCTION_BLOCK
retain their values between scans. This is how you maintain state in a
cyclic program — you do not need global variables or external storage.
Variable Sections¶
IEC 61131-3 uses different keywords to indicate how a variable is used:
Section |
Meaning |
|---|---|
|
Local variable, private to this POU. |
|
An input parameter passed into this POU by the caller. |
|
An output parameter passed back to the caller. |
|
A parameter passed by reference — the POU can read and modify it. |
|
A global variable visible across the configuration. |
These sections make the data flow between program organization units explicit, which is important for understanding and maintaining complex control systems.
Directly Represented Variables¶
The key concept that distinguishes PLC programming from general-purpose programming is the ability to map variables directly to hardware I/O addresses. These are called directly represented variables.
A directly represented variable uses the AT keyword followed by
an address:
VAR
Button AT %IX1 : BOOL;
Buzzer AT %QX1 : BOOL;
END_VAR
The address follows a specific format:
%<direction><size><address>
Where:
Direction indicates whether this is an input, output, or memory location:
Prefix
Meaning
IInput — read from a sensor or external device
QOutput — write to an actuator or external device
MMemory — internal storage, not connected to hardware
Size indicates the width of the data:
Suffix
Size
XSingle bit (BOOL)
BByte (8 bits)
WWord (16 bits)
DDouble word (32 bits)
Address is a numeric identifier for the specific I/O point.
For example:
%IX1— read a single bit from input address 1%QX1— write a single bit to output address 1%IW3— read a 16-bit word from input address 3%MD10— a 32-bit memory location at address 10
How I/O Works in the Scan Cycle¶
During each scan cycle (see What is IEC 61131-3?):
The runtime reads all physical inputs and copies them into the input variables (
%I).Your program executes using those values.
The runtime copies output variables (
%Q) to the physical outputs.
This means your program never reads a sensor or writes an actuator directly. Instead, it works with a snapshot of the I/O state that is refreshed on each scan. This approach (sometimes called process image) ensures that all input values are consistent within a single scan — a sensor value does not change halfway through your logic.
Next Steps¶
To see these concepts in action, work through the Quick start tutorial which builds up a program step by step. For details on all supported data types and variable sections, see the Compiler reference.