Create a User-Defined Function Block in Codesys

When a PLC programmer wants to create a logic where some part will be repeated many times in the program, then instead of writing it every time, he can create a library which will be the reference. In PLC programming, it is called a function block.

Function block allows the programmer to write the logic only once and then use multiple instances of it to reduce the coding time and memory to a great extent. There is one software tool in industrial automation that is used as a base for many software manufacturers, whose name is Codesys.

In this post, we will see how to create a user-defined function block in Codesys.

User-Defined Function Block in Codesys

First, in an application, select a new POU as shown in the first image. After that, you will be asked to choose to create a program or a function block.

Create a User-Defined Function Block in Codesys

There, you need to choose the function block as shown in the second image. Choose the corresponding implementation language you require for designing.

Codesys user-defined function block

Now, once the function block named POU has been created, you can write the logic inside it. Now, when you mapping a new variable, you will be first asked to create one as shown in the below image.

As a function block requires an input and output, these two types must be used; otherwise, there is no use in creating a function block. This popup comes automatically. Alternatively, you can write and create variables quickly by seeing the second image.

Follow the syntax as shown in a black box and write variables under var_input and var_output. You can also create local variables that can be used just inside the function box, by clicking the var option.

Accordingly, you can design your variable types. You can also make a variable as retentive, by clicking the retain option in the popup shown below.

function block named POU

The logic as of now that has been created is as shown in the second image – the value of the input is being moved to the output.

How to create a function block in Codesys

Once the logic inside the function block has been created, you can create a new program and create multiple instances of this function block. As shown in the below image, two instances of the function block have been created.

You can define the variables and blocks locally or globally as per your requirements. We have mapped the input and output variables as shown in the figure. These function blocks work in the same way as a system function block; working independently in instances.

Just remember that the function block instances must not be used more than once, otherwise, the PLC will not realize which function block to execute. And the logic will not work due to this. That is why, as you can see, we have used different names for the two instances we have used.

Function block development in Codesys

Now, if you make the following changes in the function block – adding or removing some variables, then you update these function block instances by right-clicking the block and using the option reset pin as shown in the below image.

As you can see, you can also remove unused pins (which means input and output), add an input pin, or add an output pin. If you are adding any pin, then you need to make the corresponding changes inside the function block too.

Codesys function block example

In this way, we saw how to create a user-defined function block in the Codesys platform.

Read Next:

Don't Miss Our Updates
Be the first to get exclusive content straight to your email.
We promise not to spam you. You can unsubscribe at any time.
Invalid email address

Leave a Comment

Share via
Follow us and never miss an update!