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 / 
Constant Value Calculation (Typed and Non-Typed) in SCL
Advanced

Constant Value Calculation (Typed and Non-Typed) in SCL

PLC Programming

Introduction

Variables are the inseparable elements of PLC programming in every language defined in IEC 61131-3. Control engineers need to use different variables to proceed with their PLC programs. One of the practical variables that are widely used in mathematical operations are constants.

Most of the time, PLC programmers do not make a difference between the typed and non-typed constants, and they use a mixture of them in computation procedures. Unfortunately, doing so will deliver an undesirable result that is incorrect and unexpected.

In this tutorial, the different types of constants (typed and non-typed), the problem of using a mixture of different constant types in mathematical equations, and the two possible solutions that can resolve this issue will be discussed in detail.

Prerequisites

What you will need to follow along with this tutorial are:

Typed and non-typed constant interpretation

Data that have a fixed value and cannot change during the running of a program are called constants. Various program elements can read constants while the program is executing, but constants can't be overwritten. Depending on the type and format of data, standardized notations are used to represent the value of a constant. The typed and non-typed notations are distinguished from one another.

Mixing typed and non-typed constants in mathematical functions is not recommended as you would otherwise encounter undesired implicit conversions, and as a result, inaccurate values are obtained.

By exploring the following PLC programming example, a calculating process with a typed and a non-typed constant is explained.

After creating a new project and configuring an S7-1500 CPU for the PLC, in the TIA Portal software, double-click Add new block item on the left pant to start creating an SCL block.

In the pop-up window, select Function block as the desired block, name it FB_MathsFunctions, choose SCL from the language drop-down menu and press the OK button.

Figure 1.1: Creating an SCL function block

When the created function block opens, left-click the down arrow on the top of the window to open the Block interface. Scroll down to find Temp (Temporary) section and declare the Variable_DINT tag with the double integer data type.

Figure 1.2: Declaring a tag in the block interface

Type the following program code in the SCL editor: #Variable_DINT := INT#1 + 50000; In this mathematical calculation, the non-typed constant 50000 will be added to the typed constant INT#1.

Now, right-click over the SCL function block FB_MathsFunctions, select compile from the pop-up menu and then choose the item Software (only changes).

Figure 1.3: Typing program code and compiling it

The Inspector window appears with the Compile tab opened, indicating the block was successfully compiled, but it has one warning. The non-typed constant 50000 is underlined in yellow in the software to show that the constant value is beyond the data type INT's permitted range.

Figure 1.4: Constant value is outside the permitted range

For the SCL function block to be executed in each PLC cycle scan, the Main OB must call it. To do this, double-click Main OB on the left pane and then drag and drop the function block over Network 1. When the Call options window opens, it has only the Single instance data block option. Keep the name as it is and press the OK button.

Figure 1.5: Calling the function block by the Main OB

Now, the program is ready to be downloaded to the simulator. Left-click the Start simulation icon on the top toolbar to open the Load preview window. Press the Load button.

Figure 1.6: Downloading the program to the simulator - Load preview window

When the Load results window appears, press the Finish button and then left-click the Run button over the S7-PLCSIM Advanced software to bring the program into run mode.

Figure 1.7: Downloading the program to the simulator - Load results window

To see the result, double-click the SCL function block under the Program blocks folder in the Project tree and then press the eye-glasses icon in the SCL editor’s toolbar to make the program go online so you can monitor the block.

Figure 1.8: Bringing the program into online mode

The typed constant's data type determines the data type of the addition. The addition is done in the INT (integer) data type region.

Figure 1.9: Determination of the addition equation data type

In the initial stage, the non-typed constant 50000 is implicitly transformed into the integer data type. But the conversion yields a negative result (-15536).

Figure 1.10: Conversion of the non-typed constant into the integer data type

After that, this value is added to the typed constant (INT#1). The outcome is -15535. Given that the tag to which the addition result is to be written is defined with the data type DINT (double integer), the value -15535 is implicitly transformed to the data type DINT and stored in the tag Variable_DINT. However, the outcome is still negative.

We expected that the non-typed constant 50000 would be added to the typed constant one (INT#1) and deliver the result of 50001 (1 + 50000 = 50001). But it didn't happen. How to solve this problem?

Figure 1.11: Software result vs expected result

There are two ways to fix this issue. The first solution to avoid this undesirable outcome is to type both constants. When you type both constants, the computation operation is determined by the lengthier data type.

To apply this solution, left-click the Go offline button on the top toolbar to bring the program into offline mode. Then, modify your application code into: #Variable_DINT := INT#1 + DINT#50000; in the SCL function block FB_MathsFunctions. In this computation process, the typed constant DINT#50000 and the typed constant INT#1 will be added together.

Figure 1.12: Typing both constants in the program code

To compile the modified program, right-click over the Program blocks folder in the Project tree, select Compile from the pop-up menu and then choose Software (rebuild all).

Figure 1.13: Compiling the modified program - Typing both constants method

To download the modified program to the simulator, right-click over the Program blocks folder, select the Download to device from the pop-up menu and then choose Software (only changes). When the Load preview window opens, press the Load button.

Figure 1.14: Downloading the modified program to the simulator - Typing both constants method

To see the result, left-click the eye-glasses icon in the SCL editor’s toolbar to bring the program into online mode. At first, the constant INT#1 is transformed into the double integer (DINT) data type. Then, the addition of the two constants is performed in the DINT data type region: 1 + 50000 = 50001

Figure 1.15: Monitoring the modified program online - Typing both constants method

The second solution to resolve the mentioned issue is not to type both constants. If you do not type both constants, they are interpreted as the broadest available data type on the current CPU. It indicates the two constants are treated as LINT data types on an S7-1500 series CPU.

To apply this solution, once again, left-click the Go offline icon on the top toolbar and modify the application code in the SCL editor using the non-typed constants as follows: #Variable_DINT := 1 + 50000.

In this computation process, the non-typed constant 50000 and the non-typed constant 1 will be added together.

To compile the modified program, under the Program blocks folder, right-click over the SCL function block FB_MathsFunctions, select Compile from the pop-up menu and then choose Software (only changes).

Figure 1.16: Modifying and compiling the program code - Non-typed both constants method

This time to download the modified program to the simulator, left-click the Program blocks folder in the Project tree and then left-click the Download to device icon on the top toolbar to open the Load preview window. In the pop-up window, press the Load button.

Figure 1.17: Downloading the modified program to the simulator - Non-typed both constants method

To see the result, left-click the eye-glasses icon in the SCL editor’s toolbar to bring the program into the online mode to monitor it. The constant values 1 and 50000 are considered to be of the LINT data type, and the outcome of the addition is once more transformed into the DINT data type.

Figure 1.18: Monitoring the modified program online - Non-typed both constants method

Frequently Asked Questions (FAQ)

What is SCL programming in Siemens TIA Portal?

SCL (Structured Control Language) is a high-level, text-based programming language used inside Siemens TIA Portal. It is part of the IEC 61131-3 standard and is conceptually similar to languages like Pascal or C.

SCL is particularly powerful for:

  • Complex mathematical operations
  • Looping through arrays or structures
  • Handling conditional logic cleanly
  • Performing calculations that would be cumbersome in ladder logic (LAD) or function block diagram (FBD)

In industrial applications, SCL allows developers to write more compact, scalable, and readable code — especially in complex automation tasks like data manipulation, PID control, and motion logic.

What is the difference between a typed and non-typed constant in SCL?

In SCL:

  • Typed constant: A constant where you explicitly define the data type.
    Example:

CONSTANT
 pi : REAL := 3.14159;
END_CONSTANT

Here, pi is a REAL type constant.

  • Non-typed constant: A constant where the value is directly used without explicitly defining its type within a CONSTANT block.
    Example:

myValue := 10;

10 here is interpreted based on the variable type receiving it.

Key difference:
Typed constants improve code readability, enforce data integrity, and make maintenance easier. Non-typed constants are quicker to use but can lead to unexpected behavior if types don't match, especially when working with real numbers, integer truncation, or complex structures.

Why should I use typed constants in SCL programming?

Using typed constants offers several advantages:

  • Clarity: Future readers (or even yourself) will understand immediately what type of value the constant represents (e.g., INT, REAL, TIME).
  • Compile-Time Error Checking: Mistakes are caught earlier because the compiler enforces type compatibility.
  • Consistency: Typed constants prevent accidental type casting, truncation, or overflow.
  • Ease of Maintenance: If you need to adjust a value (like a speed threshold or PID parameter), you can find and update it easily, rather than searching through untyped literals in multiple places.

Best practice:
Always define important constants (especially those shared across multiple function blocks) as typed constants inside a dedicated CONSTANT block or data block (DB).

Can I declare constants inside a Function (FC) or Function Block (FB) in SCL?

Yes, you can declare constants locally inside an FC (Function) or FB (Function Block).

Typical structure:

CONSTANT
 MaxValue : INT := 1000;
END_CONSTANT

Placed inside the function body, this constant will only be available within that particular FC or FB, keeping your project modular and avoiding global namespace pollution.

However, if you want the constant to be shared across multiple blocks, it’s better to define it at the global level (e.g., inside a common data block or interface).

What happens if I don't specify the data type when assigning a value in SCL?

If you don't specify a type:

  • SCL tries to infer the type based on the target variable and the format of the constant.
  • This can sometimes lead to implicit type casting, which can cause rounding errors, truncation, or unexpected results, especially when dealing with real numbers or mixed calculations (like adding an INT and a REAL).

Example:

myReal := 5;

This assignment is legal if myReal is a REAL, but internally the integer 5 is cast to 5.0.

Why this matters:
When performing calculations, mixed-type operations can reduce code clarity and cause rounding issues. Explicit typing (or typed constants) avoids these problems.

How do typed constants impact memory usage and performance in Siemens PLCs?

Typed constants are usually compiled directly into the code at build time. This means:

  • They do not consume additional memory like a normal variable would.
  • They are embedded into the instruction set, making access extremely fast.
  • They improve CPU efficiency, especially when used in loops or calculations, because the compiler optimizes constant operations better than variable-based ones.

Important:
Constants are efficient for both memory and performance. However, overusing them without structure can still make code harder to maintain, so balance is key.

How should I organize constants for a large SCL program?

For larger projects, it’s a best practice to centralize your constants.
You can do this by:

  • Creating a global data block specifically for constants (read-only).
  • Grouping related constants (e.g., "Motor Settings", "Sensor Thresholds", "System Timers") together inside structured types (STRUCTS) inside the data block.
  • Giving constants meaningful names that describe their function clearly.

Example:

CONSTANT
 Motor_Speed_Max : REAL := 3000.0;
 Sensor_Temperature_Threshold : REAL := 120.5;
END_CONSTANT

This approach improves:

  • Readability
  • Traceability (for audits and troubleshooting)
  • Reusability across different program sections

When should I avoid using typed constants?

There are very few cases where typed constants should be avoided, but you might skip them:

  • In quick throwaway calculations where clarity is not critical.
  • When working inside a temporary project for testing simple logic.
  • When values are truly one-off and not reused elsewhere in the program.

However, for production-grade code — especially in manufacturing automation — using well-documented, typed constants is almost always recommended.

Still mastering SCL?
You're on the right track! Mastering constants, type handling, and clean code structures is key to writing professional-grade Siemens automation programs.

Conclusion

This tutorial discussed constant value calculation in Structured Control Language (SCL). You learned constant variables are divided into two categories: typed and non-typed, and you got familiar with the differences between them.

You learned not to combine typed and non-typed constants in mathematical equations as you would otherwise face undesired implicit conversions, resulting in inaccurate values.

You learned in case of encountering this issue during programming, use two possible options to fix it. The first solution is to type both constants. Whenever you type both constants, the lengthier data type determines how the calculation is performed.

And there is also a second solution to not typing both constants to avoid the undesired and wrong result. When you don't type both constants, the processor interprets them as the broadest possible data type. Consequently, on a Siemens S7-1500 processor, these two constants are interpreted as LINT values.