Cookies are important for this site to function properly, to guarantee your safety, and to provide you with the best experience. By clicking OK, you accept all cookies. For more information, please access our Privacy Policy.
Table of Contents
Tutorials / 
A Comprehensive Guide to Constants for Siemens PLC Programming - TIA Portal
Intermediate

A Comprehensive Guide to Constants for Siemens PLC Programming - TIA Portal

Siemens
TIA Portal
PLC Programming
Ladder Logic

‚ÄćIntroduction

Understanding constants and their usage in PLC programming are vital for those working with S7 controllers. This comprehensive tutorial will equip you with the knowledge and skills to use constants in your PLC programs per your needs.

You should learn about types of constants to enhance program readability and code understanding. For instance: typed and non-typed constants, Boolean-type constants, and symbolic constants. Also, you will discover the significance of constant declaration and its permitted data types to become a master in this field.

Prerequisites

What you will need to follow along with this tutorial:

Constant Basics

During program runtime, constants assume unchangeable values, representing data that remains fixed. When the program is executed, constants can be accessed by different program elements. But, they retain their unmodifiable nature. The value of a constant is denoted using predefined notations determined by the data type and format. There is a recognized contrast between typed notation and non-typed notation.

Typed and Non-typed Constants

When using the non-typed notation, you just input the constant value without specifying its associated data type. The assignment of a data type to a non-typed constant occurs only when it is involved in its first logical or arithmetic operation. In Figure 1.1, you can observe illustrative examples that depict the utilization of the non-typed notation.

Figure 1.1 - Siemens TIA Portal Constants | Non-typed constants
Figure 1.1 - Siemens TIA Portal Constants | Non-typed constants

In the typed notation, you include the data type alongside the constant value for explicit specification. In Figure 1.2, you can observe an illustrative example that depicts the utilization of the typed notation.

Figure 1.2 - Siemens TIA Portal Constants | Typed constant
Figure 1.2 - Siemens TIA Portal Constants | Typed constant

Boolean-type Constants in FBD and LAD Programming

Consider CPUs of the S7-1500 series with firmware version 1.8 or higher and the S7-1200 series with firmware version 4.0 or higher. These are the only CPUs that allow the usage of Boolean constants as inputs for instructions in FBD and LAD.

Figure 2.1 - Siemens TIA Portal Constants | Implementing constants in older and newer firmware versions of S7-1200/1500 series
Figure 2.1 - Siemens TIA Portal Constants | Implementing constants in older and newer firmware versions of S7-1200/1500 series

But what about the earlier firmware version? Only internal function blocks (FBs) within the instructions allow the use of Boolean-type constants. The identification of these instructions is based on the occurrence of the "Call options" dialog. This dialog box opens when these instructions are inserted into a network. It is necessary to exclude using Boolean constants as inputs for instructions that are not part of the mentioned category.

Figure 2.2 - Siemens TIA Portal Constants | Overcoming the challenge of using a constant value in older versions of the S7-1200/1500 series
Figure 2.2 - Siemens TIA Portal Constants | Overcoming the challenge of using a constant value in older versions of the S7-1200/1500 series

The usage of Boolean constants is not allowed in S7-300 and 400 PLCs.

Figure 2.3 - Siemens TIA Portal Constants | Forbidding the usage of constants in the S7-300/400 series
Figure 2.3 - Siemens TIA Portal Constants | Forbidding the usage of constants in the S7-300/400 series

Symbolic Constants

You have the opportunity to assign symbolic names to constants. It grants you to reference constant values by a chosen name in the program. Adhering to this approach improves the program's readability and ease of maintenance. Its importance becomes clear when constant values need changes. It ensures efficient code understanding and seamless updates.

When constructing a symbolic constant, it is composed of the following elements:

  • Name
  • Data type: The presence of a data type is a constant feature for symbolic constants. Non-typed notation is not permitted for the declaration of symbolic constants.
  • Constant value: The constant value for symbolic constants can be chosen from the entire range of values allowed by the specified data type. For this purpose, you should be familiar with different data types in Siemens PLC programming.
Figure 3.1 - Siemens TIA Portal Constants | Different elements of a symbolic constant
Figure 3.1 - Siemens TIA Portal Constants | Different elements of a symbolic constant

Constant Declaration

From a point of view, you can consider two types of constants:

  • Global constants: These constants are universally applicable across all CPU sections.
  • Local constants: These constants are confined to a specific block, with no impact outside of it.

Global constants and local constants differ significantly, presenting the following distinctions between them:

  • Validity scope: The validity of global constants extends throughout the entire CPU. Also, their names are uniquely identified within the CPU. Local constants possess validity solely within the block of their declaration. Also, their names are unique exclusively within that block.
  • Valid character options: In the names of both global and local constants, digits, letters, and special characters are permitted and considered valid.
Figure 4.1 - Siemens TIA Portal Constants | Valid character options for constant declaration
Figure 4.1 - Siemens TIA Portal Constants | Valid character options for constant declaration
  • Place of declaration: Within the PLC tag table, global constants are defined exclusively within the designated "User constants" tab. The definition of local constants takes place in the block interface.
Figure 4.2 - Siemens TIA Portal Constants | Defining local and global constants
Figure 4.2 - Siemens TIA Portal Constants | Defining local and global constants
  • Visual depiction: The display of global constants includes the usage of quotation marks to represent them. For instance: "My_Constant2." Local constants representation involves using a number sign as a prefix. For example: #My_Constant1.
Figure 4.3 - Siemens TIA Portal Constants | Visual depiction of local and global constants
Figure 4.3 - Siemens TIA Portal Constants | Visual depiction of local and global constants

Imagine you have a local and global constant with identical symbolic names. Once you use this doubly specified name as the default value of a tag, the conflict can emerge. Within this situation, the local constant is employed automatically as the default choice.

Figure 4.4 - Siemens TIA Portal Constants | The conflict of using the same name for local and global constants
Figure 4.4 - Siemens TIA Portal Constants | The conflict of using the same name for local and global constants

The Challenge of Downloading Constant Declarations

When programs are downloaded to S7-300 and 400 CPUs, the constant declarations, both local and global, are not included in the transfer. When you transfer a program from a device, the declarations of constants may cease to be available for reference or use. What if you have defined and used constant values within your program, such as assigning a constant value to a variable? The actual constant declarations will not be available within the PLC after the download. The constant values will still be present and can be utilized within the program. But, the original constant declarations will not be accessible in the PLC's memory.

Figure 5.1 - Siemens TIA Portal Constants | Forbidding the download of constants to the CPU memory of the S7-300/400 series 
Figure 5.1 - Siemens TIA Portal Constants | Forbidding the download of constants to the CPU memory of the S7-300/400 series 

The Permitted data types assigned to constants

There are no limitations on the data types that can be used for constants, including both basic and derived types:

  • Numbers represented in binary form
  • Bit strings
  • Integers
  • Numbers represented in floating-point format
  • Timers
  • Data and time
  • Character strings

All general rules governing the conversion of types, both explicit and implicit, are applicable in this context.

STEP 7 consistently opts for the data type which offers the highest achievable precision in all cases. What will happen if it is not possible to explicitly define the data type of a constant within an expression? The CPU defaults to using the data type with the highest precision available. Consider the expression presented in Figure 6.1. It showcases the combination of an integer tag with a non-typed constant in the floating-point format. In the S7-300 and 400 series, the right-hand side of the assignment is evaluated using the Real format for computation.

Figure 6.1 - Siemens TIA Portal Constants | Evaluating the data type of an assignment in the S7-300/400 series
Figure 6.1 - Siemens TIA Portal Constants | Evaluating the data type of an assignment in the S7-300/400 series

In the S7-1200 and 1500 series, calculations are carried out using the LReal data type to achieve the most possible precision. Therefore, the assignment to a Real tag in this context would be considered invalid or may trigger the issuance of a warning.

Figure 6.2 - Siemens TIA Portal Constants | Evaluating the data type of an assignment in the S7-1200/1500 series
Figure 6.2 - Siemens TIA Portal Constants | Evaluating the data type of an assignment in the S7-1200/1500 series

It is advisable to employ the typed notation to provide an exact definition of the data type associated with a constant. To understand it better, look at the expression presented in Figure 6.3.

Figure 6.3 - Siemens TIA Portal Constants | Providing an exact definition of the data type associated with a constant
Figure 6.3 - Siemens TIA Portal Constants | Providing an exact definition of the data type associated with a constant

Instances where constants are employed

Use in assignments, instructions, and expressions: In the context of instructions or assignments, constants can be employed as viable substitutes for tags. In the realm of SCL, you have the added capability of utilizing constants within expressions. Nevertheless, since constants cannot be explicitly written, their usage is limited to input values. Figure 7.1 provides visual representations of examples that highlight the potential uses of constants.

Figure 7.1 - Siemens TIA Portal Constants | Using constants in assignments, instructions, and expressions
Figure 7.1 - Siemens TIA Portal Constants | Using constants in assignments, instructions, and expressions

Use as a default value: When assigning a default value to a tag, constants can be used for this purpose. To achieve this, enter either the specific value or the symbolic name of the constant. Where? Into the designated "Default value" column provided in the block interface. One way to ensure compatibility is that the constant's data type must match the tag's data type. Or the constant's data type should be convertible to the tag's data type through implicit conversion, as confirmed by the IEC check.

Figure 7.2 - Siemens TIA Portal Constants | Using constants as default values
Figure 7.2 - Siemens TIA Portal Constants | Using constants as default values

Use as maximum String length: When determining the maximum length of a String, you can utilize local or global constants with the following data types: INT, SINT, LINT, DINT, ULINT, UDINT, and UINT. Figure 7.3 presents an example that demonstrates the utilization of a constant as the maximum length for a STRING.

Figure 7.3 - Siemens TIA Portal Constants | Using constants as the maximum length for strings
Figure 7.3 - Siemens TIA Portal Constants | Using constants as the maximum length for strings

Use as the limit for an Array: When determining the Array limit, you can utilize local or global constants with the following data types: INT, SINT, LINT, DINT, ULINT, UDINT, and UINT. Figure 7.4 shows an example that demonstrates the usage of a constant as the limit for an Array.

Figure 7.4 - Siemens TIA Portal Constants | Using constants as the limit for arrays
Figure 7.4 - Siemens TIA Portal Constants | Using constants as the limit for arrays

Conclusion

In conclusion, you learned about the basics of constants. You got familiar with two predefined notations for the value of a constant: typed and non-typed. You understood the challenges of implementing Boolean-type constants in S7-PLCs with different firmware. You figured out what elements comprise symbolic constants and why you should use these constants. You grasped definitions of local and global constants and the main differences between these two types. And finally, you became acquainted with some instances where constants are being employed.

‚Äć