What are Functions in PLC Programming? (FCs)

Programming a certain industrial process using a PLC can be very simple and easy to achieve provided that you are able to use the many tools available within the PLC, these tools are there to help you reach your programming goals easily and fast and also help you write an easy to read, follow and debug code.

One of these many tools in a PLC is the Function blocks or FCs. In this article we will get to know the basics of function blocks, what are they, when to use them, and how to use them.

Contents:

  • Simple process.
  • More complicated process.
  • What is a Function (FC)?
  • How to create and use an FC?
  • Program simulation.
  • Conclusions.

Simple Process

Let’s consider a simple industrial process where you 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

Coding this process in your PLC will be fairly simple and easy, considering you have few inputs and outputs in this logic.

When coding this logic in Siemens TIA Portal, it will be as follows:

  1. Create your project.
  2. Configure your PLC (in our example we used CPU 1512C-1 PN).
  3. Define your input/output tags: see picture 2.
  • Start push button S1  %I10.0
  • Detection sensor B1   %I10.1
  • Detection sensor B2   %I10.2
  • Conveyor Motor M1   %Q4.0
defining your tags in PLC
Picture 2 – defining your tags

4. Open your main OB1 and write your code. See picture 3.

5. Compile and run your logic.

Functions in PLC Programming
Picture 3 – the logic of the conveyor belt

As you can see from picture 3, the logic of this process is very simple and it works as follows:

When a product is at the start of the belt and the start Push button is pressed the conveyor will run and it will keep running until the product is detected at the end of the line.

More Complicated Process

Now, imagine your production line has 50 conveyor belts all of them need to be controlled in the same manner as conveyor M1. One approach to achieve this will be by repeating steps 3 through 5 fifty times. This is not wrong, but now you have to write the same logic 50 times, and even if you just copied network 1 in picture 3 and paste it fifty times, you still have to redefine and reassign the new tags to the new networks.

Another hustle you might face is when you need to add a new parameter for your control, for example, you need to add a stop push button in your logic to stop the conveyor belt at any time the button is pressed. That means you will now define the new input signal and you will have to write it fifty times for your fifty networks, See picture 4.

More Complicated Process in Siemens Tia
Picture 4 – Re-editing your code

You see, you will have to make this change in all fifty networks of your logic. And if you forget one, your code will not work as desired and most likely it will not be something that your compiler can detect. So programming your logic this way has a lot of disadvantages.

The Best approach is to use Function Blocks or FCs.

What is a Function Block (FC)?

A function block or FC is simply a block that contains a specific logic made by a programmer, this logic or FC will be executed whenever it is called.

Functions can be used, for example, for the following purposes:

  • To return function values to the calling block, e.g. for mathematical functions.
  • To execute technological functions, e.g. individual controls using bit logic operations.

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, and each time you use this instruction you just call the FC and assign your parameters to it.

Examples of these functions are mathematical instructions like ADD & MOVE instruction. See picture 5.

What is a Function Block (FC)
Picture 5 – ADD&MOVE are examples of FCs

How to Create and Use an FC?

Now, back to our fifty conveyor belt process, how can we apply the FC concept to our logic?

The simple concept is that you will create a function block that has the logic of operation of the conveyor belt created inside, and each time to need to run a certain conveyor belt you will just call the FC and assign the parameter of this conveyor.

For example, if I want to run the conveyor belt 20 in my process, I will call this function and give it the parameters of the conveyor belt 20. Same if I want to run belt 46 or any other conveyor.

That means I will create the logic only one time. And reuse it whenever I need to. It also means, if at any point I decided to add something to my logic, I will only make that edit in the main FC block, that change will automatically be included each time you call the function.

Functions in PLC Programming

To create an FC Block, we do the following Steps.

In Siemens TIA Portal, Create a function block, by pressing the add new block icon, see picture 6.

How to Create and Use an FC Block
Picture 6 – create 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 7.

create your FC logic
Picture 7 – Create your FC logic

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

call your FC Block
Picture 8 – calling your FCs

Notice, that each time you call the function, it will ask you to assign the inputs/outputs parameters of the function. That way you can use the same logic for different conveyors.

If for example, I need to operate conveyors 1 and 15 I will call the function twice and give each call the parameters of a certain conveyor. See picture 9.

assigns different PLC parameters to different function calls
Picture 9 – assigns different parameters to different function calls.

Now, imagine you need to edit your logic and add some extra functionality, for example, you want to add an output that will be ON whenever the conveyor is running, and you will use this output for an indication lamp to notify the operators that this conveyor is now running.

Using the concept of FCs, you will just have to edit your logic inside the function and it will be implied whenever you use your function. See picture 10.

editing function block
Picture 10 – Editing your function block

Now, each time you call the function it will ask for the indication lamp output tag for each call. See picture 11.

Function Block Example Code in PLC
Picture 11 – Editing the FC will be implied in each function call

Program Simulation

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

Compile your PLC code and start a new simulation.

At first, you see the Function is called twice, for conveyors 1 and 15 see picture 12.

Function Block Program Simulation
Picture 12 – Simulation of program.

Now, try to run conveyor 1 by activating S1 and B1, see picture 13.

Learn How to Use FC for Programing Siemens PLC
Picture 13 – only conveyor 1 is running

Notice, how only belt 1 is running, even though we have two calls for the same function, but conveyor 15 is not running as its conditions are not met yet.

Now, try to run conveyor 15, and notice that its operation is completely separate from the conveyor 1 process, even though it is the same function. See picture 14.

What are PLC function blocks
Picture 14 – Different function calls are independent of each other.

Did you notice, how after a while conveyor 1 stopped as the B2 signal is now true? While conveyor 15 still running. That is how a function block in a plc can be created and used.

Downloads:

Conclusion

Using function blocks or FCs in your logic will optimize your code and make it clean and easy to understand. It will also give you the advantage of reusing your code as many times as you want in one program which will reduce a lot of coding and editing time and also will reduce errors in 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

1 thought on “What are Functions in PLC Programming? (FCs)”

Leave a Comment