CODESYS Declaration Examples: Variables, Arrays, Function Blocks…


There are many ways to implement variable declaration in CODESYS. It can be easy to forget the syntax or specifics on some of them. This post covers them for reference. Also, a couple of programmer cautionary comics from XKCD at the bottom…    😀


CODESYS Variable declaration


Variable Declarations:

VarName : INT ;

Variable with located memory address

VarName AT % MW123: INT;


Variables with Initial Value:

VarName : INT := 23;

Variable initial values can be tied to another variable of the same data type:

VarName: INT:= VarName2;


VarName2:= INT;


Declaring STRING with defined length:

VarName2 :STRING(40);

With the above, a string of 40 characters is created. Each character is one byte.


Declaring STRING with initial assignment:



Declaring Arrays with Initial Value:

Array1: ARRAY[1..5] OF INT := [1,2,3,4,5];

Without initial value it would just be:

Array1: ARRAY[1..5] OF INT;

Some other notes about arrays:

The first element can be element zero, i.e.

Array1: ARRAY[0..5] OF INT;


Multi-dimensional Arrays:

Array1: ARRAY[0..5,0..2] OF INT;

Array1: ARRAY[0..5,0..2,0..4] OF INT;


Declaring Pointers:

PointerToStrArray : POINTER TO STRING ( 9 );

Declaring Pointers to Arrays:

PointerToStrArray : POINTER TO ARRAY[1..10] OF STRING ( 9 );

In both the pointer examples above, the pointer values can be loaded with the ADR function. More on that here.


Declaring Function Blocks with Inputs:

Declaring an OFF delay timer from the Standard library with an initialized preset time

OffDelayTimer: TOF:= (PT:=T#2S);

The above declaration method can also be used with the preset time tied to another variable of type time:

OffDelayTimer: TOF:= (PT: = PresetTime);

where the PresetTime is declared as follows:

PresetTime: TIME := T#2s;


One of the positive points of a textual variable declaration area is the option to copy and paste. In some programming environments, the alternate to a textual declaration is a tabular or table based variable declaration area. Some  table based ( or tabular) variable  areas might include a pull-down menu of options for each declaration. This may help someone who is beginning to learn in an environment.

Nevertheless, in the long term, textual declarations are more flexible. Another benefit is  opens up the option to generate the variable declaration externally- in Excel for example. The external declaration is useful for larger projects with multiple programmers. In Excel, a name can be created and multiple versions of a variable generated with prefixes or suffixes to that name. One example may be:

For a variable called SysRun:

The local variable could be SysRun_Local

The status to be fed to a SCADA system could be SysRun_SCADA

Instead of typing out: SysRun_Local: BOOL; and SysRun_SCADA: BOOL; , the entire declaration process can be automated in Excel and then copied and pasted into CODESYS.

To wrap up for now..

Comments in variable declaration field are important. It helps the next person who reads the code/program. It might even help you make sense of your work, in the future. Also, for function blocks, the comments on the same line as a variable become the description of input pins when the cursor hovers over a pin.


Include comments with variable declaration. Comments help refresh your memory or serve as a guide for the next user.
Include comments with variable declarations.Comments help refresh your memory or serve as a guide for the next user.


There are probably other useful points  and methods of variable declarations in CODESYS that are not included here. If one comes mind, please comment below.


Leave a Reply

Your email address will not be published. Required fields are marked *