Hi friends, today we are going to explore mathematical computations in ladder logic. Like in any programing language you should find logic and mathematic computations, here in PLC programming you often need to process the input data that is collected from reading analog devices like temperature, level, flow et cetera. Then you need to run some calculations on this data to derive some other variables for deciding to run or stop some device or even to determine analog output to output to analog device i.e. valve or actuators. In the following sections, we are going to explore the mathematical functions and their input operators and outputs as well. Then we will show how to utilize such functions in ladder logic with simple examples and as usual enjoy practicing them thanks to the PLC simulator.

## What are mathematical operations we have

You may find some minor changes in the set of mathematical functions from brand to brand of PLC controllers. However, you will find in most of them are common in every controller. For example, you will find the basic mathematical functions are available like addition, subtraction, multiplication, division, negation by applying two’s complement, modulus, increment and decrement, absolute, maximum, and minimum. In addition, trigonometric functions like sine, cosine, tangent are included. Let us go over these functions and explore their operators and output and how they can be utilized in PLC ladder logic programming.

## Functions operators and outputs

For all these functions, they have input operators and one or more returned outputs. Most of them have two inputs like addition, subtraction, multiplication, and division. While some have only one input parameter like negating function, maximum, and minimum. And they all have only one input. Table 1 lists all these functions and their input operators and output.

Table 1: the functions’ input parameters and outputs

## Operator and Output values data types

It is very important to be familiar with the type of data you are trying to process using these mathematical functions. So, simply you just need to know that the smallest size data type is a bool data type which is true or false or “1” or “0” and it uses only one single bit. Then character “char” or byte data type which is 8 bits while word data type is formed of 2 bytes or 16 bits and that is the word size in PLC. Also, there are doubleword data types that occupy 4 bytes or 32 bits. Moving to mathematical data types there are integer data types that can be stored in one word and the double integer “DINT” which can be stored in two words or 32 bit for holding numbers up to = 4294967296 for unsigned integers and half of that value for signed integers. Also, there is a real data type for holding numbers with floating-point. It needs 2 words or 32 bits and for a long real LREAL data type extends to 4 words or 64 bits.

## How to use the mathematical function in ladder logic

In the TIA portal for siemens, there are two ways to add mathematical functions in a rung of ladder logic program. Figure 1 shows the first method which uses an empty box in the block interface and then you can select the function and its inputs and outputs parameters.

Fig. 1: Adding an empty box for including math functions

Figure 2 shows a list of functions from which you can choose the mathematical function you want to use.

Fig. 2: A list of math functions to select

Figure 3 shows another way to add a mathematical function by going to the basic instructions and then going over the math functions on the most right part of the window as shown and selecting the function you want to use.

Fig. 3: The second way to add a mathematical function

Figure 4 shows how to define the input and output parameters of the used mathematical function. For example, in figure two input parameters are defined as the function’s input operators which are literal number five and variable “x” which is predefined as an integer in the variable declaration section in the middle top section. Also, the Variable sum is defined as an integer. So the add function will add X to 5 and assign the result to variable Sum.

Fig. 4: Defining function parameters and outputs

In this example, we are going to show how mathematical functions can be used in a ladder logic program and also show the simulation results. As in fig. 5, we design a simple calculator that uses the mathematical functions and each function can be triggered by a switch on a contact that is connected in series to the enable line of the function box.

Figure 5 shows an additional example in ladder logic. It shows two operators A and B of integer data types. The variable A is located at address MW2 and variable B is located at address MW4 while the result is stored in variable RES which is located at address MW6. All of these variables are of type integer and size one word or 32 bits. The figure show simulation results on the PLC simulator and shows the RES variable holds summation of A and B.

## Subtraction math function example

Figure 6 shows a subtraction example in ladder logic. It shows two operators A and B of integer data types. The variable A is located at address MW2 and variable B is located at address MW4 while the result is stored in variable RES which is located at address MW6. All of these variables are of type integer and size one word or 32 bits. The figure shows simulation results before triggering the subtraction function on the PLC simulator. So, the RES variable holds zero before enabling the function.

Fig. 6: Subtraction function example not triggered

Figure 7 shows the simulation results after enabling the subtraction by switch or the “SUB” command contact. The Res variable now reflects the results of subtraction.

Fig. 7: Subtraction function example in ladder logic programming

## Multiplication math example

Similarly, Fig. 8 shows an example for performing the multiplication process. First, the “MUL” is selected and two input operators of multiplications are given and output. The simulation result shows the RES variable holds the result of multiplication of the input operators.

Fig. 8: Multiplication function example in ladder logic programming

## Division function example

Similarly, Fig. 9 shows an example for performing the Division process. The “DIV” is selected as an instruction to be executed and two input operators of division are provided and output. The simulation result shows the RES variable holds the result of the division of the input operators.

Fig. 9: Division function example after triggering

## MOD function example

Similarly, Fig. 10 shows an example for performing the MOD function process. The MOD function determines the remainder of the division process, in this example we are dividing 25 by 10 which gives 2, and the remainder of 5. “MOD” function firstly is selected as an instruction to be executed and two input operators of MOD function are provided and output. The simulation result shows the RES variable holds the remainder of the division of the input operators.

Fig. 10: MOD function example in ladder logic programming

## General calculation

Now, one may ask how I need to do the calculation in form of an equation that has many processes? This is a very smart question! Many programming tools support this, fortunately. For example, in Siemens, there is a function called “calculate” which can take two inputs and perform mathematical equations on these two variables and return the result in an output variable as shown in Fig. 11. It shows the function which is called “CALCULATE” and it has two inputs “IN1” and “IN2” and one output “OUT”. I triggered the help to show you an example that states OUT can be determined from the equation in inputs and also shows all functions that can be used including logic and mathematical functions. So we can use this function, in general, to act like any mathematical or logical function separately or by combining two or more functions in one equation.

Fig. 11: Using equations in ladder logic

## Example of performing equation in Ladder logic

As you can see in the given example in fig. 12, the output variable can be determined based on multiplying the summation and subtraction of the input parameters as an equation. The result of the program is validated with a calculator.

Fig. 12: Using equations in ladder logic

## Combining mathematical function

There is another way to perform combine many mathematical functions as shown in fig. 13. As you can notice, the output variable “out” can be determined by multiplying the summation of input variables “in1” and “in2” by “in1”.

Fig. 13: Combination mathematical function in ladder logic

## Negate function

Negate function is one example of a single operator function in ladder logic programming. it reverses the sign of the input variable. For instance, fig. 14 shows an example of converting the sign of input variables “in1” to show the negative value reflected in the result in “res”.

Fig. 14: Single operator mathematical function in ladder logic programming

## Absolute function

Figure 15 shows an example of getting the absolute of variable “RES” and saving it in variable “b”.

Fig. 15: Absolute function in ladder logic

## Minimum and maximum in ladder logic

Getting minimum and maximum is one of the most frequently used in the mathematical operation. Figure 16 shows an example for getting a minimum of two input variables “in1” and “in2” while fig. 17 shows an instance of getting the maximum of two input variables as well.

Fig. 16: Getting a minimum of two input variables in ladder logic programming

Fig. 17: Getting a maximum of two input variables in ladder logic programming

## Decrement and increment in ladder logic

One of the most commonly used functions is incrementing and decrementing one variable. For example, the counter variable all the time gets incremented and decremented through the logic of the program. Figure 18 shows an example for decrementing and incrementing an input variable. First, the initial value of the variable was 15, and then after incrementing it. The variable became 6 and then by applying decrementing operation it return to 5. It is very crucial to notice that, the user variable in increment and decrement operation works as input and output at the same time. Therefore, you can see in the increment and decrement blocks, it is defined as input-output “input”.

Fig. 18: Increment and decrement operation in ladder logic

## Limiting variable in ladder logic

Now, let us use one of the very useful functions to secure a variable at specific limits. Figure 19 shows an example of limiting the value of one variable “in” to be sited between the specified minimum and maximum values. In that very example, the input variable has had a value of 5 and because it exceeds the set limit of maximum it is set to 4 which is the maximum allowed value to the variable thanks to the limit function.

Fig. 19: limiting variable in ladder logic programming

## Trigonometric functions in ladder logic

Also, trigonometric functions like sine and cosine and other related functions can be executed in ladder logic. Figure 20 shows one example of how to use a sine function in ladder logic. The first rung in the example shows how to convert the degree into rad thanks to mathematical functions multiply and division. Ultimately, the sin function block is used to determine the sine of the given angle in rad.