Skip to content

Global variables

Global variables are variables that are available throughout the entire project. They are defined in the project tree under Globals. Global variables are not mandatory, but can be useful if you have variables that are used in multiple POUs.

Global variables are declared inside a VAR_GLOBAL ... END_VAR block in the Globals editor. You can group several declarations in a single block and assign initial values:

VAR_GLOBAL
gMachineRunning : BOOL := FALSE; // shared run state
gCycleCounter : UDINT := 0; // counts completed cycles
gSetpoint : REAL := 21.5; // shared temperature setpoint
END_VAR

Once declared, a global variable can be read and written from any POU without re-declaring it locally. Reference it directly by its name:

PROGRAM MainProgram
VAR
localTemp : REAL;
END_VAR
// Read a global value
localTemp := gSetpoint;
// Write a global value from this POU
IF localTemp > 30.0 THEN
gMachineRunning := FALSE;
END_IF;
// Increment a shared counter every cycle
gCycleCounter := gCycleCounter + 1;
END_PROGRAM

Because the same instance is shared everywhere, a change written from one POU is immediately visible to every other POU that reads the variable in the same scan.