Function Blocks in PLC (FBs) – What You Need To Know?

In a previous article, we discussed functions or FCs in PLC programming, how to use them, and what their advantages are. In this article, we will talk about function blocks or FBs and how to use them in your PLC logic, and how FBs are different from FCs.

Contents:

  • Process example
  • What if the process gets more complicated?
  • What is a function block FB?
  • Program simulation.
  • Conclusions.

Simple Process Example

Let’s consider the same simple process we used in the FCs article, to make it easier to distinguish the differences between FBs and FCs.

In this example, we have a conveyor belt M1 that runs to transfer a product P1 to the end of the line. The conveyor will run when a start S1 signal is available after a presence sensor B1 detects the product on the Belt.

The conveyor will start running and continue to run until the product is transferred to the end of the line and a detection sensor B2 detects the product, then the belt will stop. See picture 1.

PLC industrial Conveyor process
Picture 1 – Simple conveyor system

As you can see in picture 1, the process needed to be controlled is simple. Doesn’t require a lot of coding, however, the importance of FBs or FCs will rise when your process has many conveyors that need to be controlled in the same manner. In the previous article, we showed how a function FC can achieve that, in this article, we will use FBs.

As we did before, coding this process will be done by easy and with a few logic code steps, see picture 2. In this example, we will use the same Siemens PLC we used before (in the previous article), the CPU 1512C-1 PN.

logic of the conveyor system
Picture 2 – the logic of the conveyor system

What if the process gets more complicated?

As we discussed before, if you have 50 different conveyors in your process that need to be controlled in the same manner, it was best to use FCs to make your PLC coding structure easier to read and understand and take advantage of function reusability to reduce the size of your code.

We also mentioned that when needing to change something in your logic, it will be easier with an FC as you would only need to make that change in the function code, and it will be automatically changed in every function call.

However, not every change I need to make will be suitable for a function FC, as some changes will require the use of a function block FB and not an FC.

Why?

The most crucial difference between a function and a function block is that an FB will have an associated memory or Data Block DB with each different call to store the function values inside so that they can remain available after the function block is executed. See picture 3.

So, if you need to know the value of your parameters from previous calls to be used in future calls, then using a function block FB will be more suitable than an FC.

Function Blocks in PLC
Pictures 3 – Using a function block require an associated data block.

What is a Function Block? (FB)

The function block is a block of code that performs certain functionality. FBs will store their input, output, and in-out parameters permanently in a data block so that they remain available even after the block has been executed.

A function block can also be called several times at different points in a PLC program. As a result, they simplify the programming of frequently recurring functions.

A call of a function block is referred to as an instance. An instance data block is required for each call of a function block; it contains call-specific values for the formal parameters declared in the function block.

You have been using functions all along, maybe you haven’t noticed, but some of the basic instructions you usually use in your codes are actually function blocks FBs, and each time you use these instructions you just call the FB and assign your parameters to it. Examples of these FBs are Timers and counters instruction. Did you notice before when trying to use a timer or a counter in your code, you have to assign a data block instance.

That is because you are using a function block. See picture 4.

PLC Timer is basically a function block
Picture 4 – A timer is basically a function block that needs a memory block

Up until this point, our process doesn’t really need a function block, and a simple function can be enough. Even when the process has 50 different conveyors because the PLC logic of the conveyor doesn’t really need any parameters to be stored permanently.

Let’s consider some extra functionality that will raise the need for function blocks.

Let’s say that you want to assign a value for the state of the conveyor belt to know at which state the conveyor is currently in. that means you need to remember that value whenever the logic is being executed. Needing to remember a value means you need to use a function block.

 So to sum up what we need to do is to assign a state of the conveyor and it will be as follow:

  • If there is a product at the start but no start signal then the state will be 1 or waiting for the start signal.
  • If the product is at the start and the start signal is true then the conveyor will be in state 2 or transfer state.
  • When the product is at end of the conveyor and the motor stopped, then will be in state 3 or the product arrived in the state.

How to Create and Use the Function Block?

We now have two different problems, we need to create a function block to operate the 50 conveyors in the same manner and also have a status value regarding the current state of each conveyor belt.

This can be done as follows:

Create a function block, by pressing the “add new block” icon, see picture 5.

How to Create and Use the Function Block
Picture 5 – Adding new Function Block

Create the conveyor belt logic as you did before, but don’t assign specific IOs for that logic, instead define IOs inside the function block interface, so it can be reused again. See picture 6.

Create your FB logic in PLC Programming
Picture 6 – Create your FB logic

Now, add the new logic for the conveyor belt status, don’t forget to define the status tag as static to ensure the value will be saved in the associated block. See picture 7.

conveyor status logic in Siemens PLC
Picture 7 – Conveyor status logic

You now finished PLC coding your function; you can call this function anytime you want to execute that logic.

We will assume calling the FB twice, one time for conveyor 1 and another time for conveyor 15. See picture 8.

call FB Block and assign data block
Picture 8 – calling an FB will require assigning a data block

Each time you call an FB, you will be expected to provide the IOs parameters associated with that call see pictures 9 and 10.

Siemens Tia Portal Function Block
Picture 9 – Calling FB many times
Introduction to Function Block PLC Programming
Picture 10 – Each FB call will have different associated parameters

PLC Program Simulation

We will now, simulate our PLC program to see how a function works. Follow the next steps:

  1. Compile your code and start a new simulation.
  2. At first, you see the Function is called twice, for conveyors 1 and 15.
  3. FB calls are independent of each other, which are why you will see the different status for them depending on the condition of each one. See picture 11.
FB programming in Siemens PLC
Picture 11 – FB calls

Check the following animation for the PLC program simulation and see if you can notice how the logic behaves.

Function Block in PLC Program

You can call this FB as many times as you want, but each time you call an FB you will have to assign a data instance. And also you will need to provide the related IOs for this call.

Conclusion

A function block is a piece of code that has specific functionality, in our PLC example, it was controlling the run and stop of a conveyor belt.

The main difference between an FB and an FC when programming a PLC is that with an FB you have the ability to store the values of your parameters in an associated data block. Whereas in the FC you don’t need a memory block.

Using functions or function blocks will help organize your code and make it more readable. And it will also give you the advantage of reusing your logic many times at different parts of your program.

If you liked this article, then please subscribe to our YouTube Channel for PLC and SCADA video tutorials.

You can also follow us on Facebook and Twitter to receive daily updates.

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