Declaring variables in LD and assigning a variable to contact/coil

Use the following possibility to declare one or more →variables within a →POU in →LD.

In this article:

After the variable has been declared, you are able to assign a variable to a contact or a coil.

Good to know

(grey lightbulb) The application navigator provides the possibility to declare external variables and to have the declarations based on the already existing global variables. This is done when you drag the global variables from a global-object onto a →POU. Details: See "Declaring external variables by dragging and dropping global variables into POUs".

Creating variables by using a dialog in the list of the declared variables

  1. Make the list of declared variables visible. 
    Alternative for an →input variable, →output variable or →in-/out variableOpen the interface editor for the object (from within the project explorer). 

  2. From the context menu, select New Variable... Alternative: Press the Ctrl-key and the +-key.

  3. In the dialog, enter the following properties for the variable and click OK.

    1. name – The name must be an →IEC-identifier
      (info)  If you want to declare several variables in one go, enter the name with , separating the names. Example: UpErr, QuitErr, MotorUp, MotorDown 

    2. section (= the variable section), e.g. LOCAL (for →local variable, also known as →internal variable), INPUT (for →input variable) etc.

      Restrictions

      →Temporary variables are not possible in FBD. →In-out variables cannot be declared in a →program, →global variables cannot be declared in a →function block or →function

  4. →data type or →function block type
    (warning) Multi-dimensional array data types are not supported for connecting to a block in-/output with a →generic data type (e.g. the in-/output of the MOVE block). 
    (info) Enter one or several letters so that a selection list with the corresponding data types and function blocks is provided.
    Subsequent changes to the selection are possible, e.g. by completing an →ARRAY declaration. For the STRING data type, STRING[80] is automatically entered. Overwrite 80, if you need a different length for the STRING variable.  

  5. →initial value (only optional required) – This initial value may be a →constant expression (in compliance with the →data type).
    It is not possible to assign an initial value to an →external variable 

Creating a Boolean local variable fast

  1. Make the list of declared variables visible. 

  2. From the context menu, select New Variable (LOCAL, BOOL). Alternative: Press Ctrl+Shift and the +-key.
    (info) If necessary, you are able to change the name as well as the data type later on. See under "Modifying variables".

Creating variables when assigning one to contact or coil (by using the content assist)

This possibility assigns one variable to a contact or a coil. All specified variables are automatically declared. 

  1. Position the mouse pointer over the input field for the variable that is displayed above the contact or the coil.
    (info) By default, the ENO output is already entered.

  2. Double-click.
    Result: An input field is opened.

  3. Declare the variables according to this syntax: X, Y : name_1, name_2, ..., name_n : BOOL := initial-value (with the parts X, Y : and :=initial-value being optional).
    As short information: X = abbreviation for the required variable section, Y = abbreviation for the required keyword. This syntax is analogous to the one in the FBD-editor (a detailed explanation of this syntax can be found under "Explanation of the syntax...").

    Examples
    • var1, var2 : BOOL
      2 internal variables (due to the missing prefix for X) are declared. The 1st variable named var1 is assigned to the contact or coil. Both variables are of →data type BOOL. There is no →initial value and no keyword (due to the missing prefix for Y). You are able to assign the 2nd declared variables named var1 to a different contact or coil later on.

    • I : var3 : BOOL := 0
      The →input variable (due to I) named var3 of data type BOOL and the initial value 0 is declared. This variable is assigned to the contact or coil. There is no keyword (due to the missing prefix for Y).

    • L, C : var4 : BOOL := 1
      The internal variable (due to L) named var4 of data type BOOL and the initial value 1 is declared. This variable is assigned to the contact or coil. Due to C, the variable is declared with the keyword CONSTANT, so it is a →constant internal variable.

  1. Press the Enter-key.