Blog

IEC 61131-3 Function Blocks - Unleash the Power!

IEC 61131-3 Function Blocks - Unleash the Power!

If you are a programmer, an OEM, or end user that utilizes IEC 61131-3 compliant PLCs, you should MUST read this article.

In the 3rd part of my series on the IEC61131-3 programming standard (you can check out the previous part 1 and part 2), I will explain the huge benefits of structured programming and why Function Blocks are the greatest thing ever....seriously they are awesome and worth using in every PLC program.

So what is a Function Block anyway?

A Function Block is an encapsulated piece of reusable code that consists of Input variables, Output variables, Through variables, Internal variables, and Internal logic.

Function Block - Input Variables Output Variables Through Variables Internal Variables

For anyone who is familiar with Object Oriented Programming (OOP), a Function Block is just like a class or an object. In order to actually use a Function Block in your program, you create an instance of the Function Block. An Instance is the term used to describe the allocation of the memory for a particular Function Block. Each unique instance runs the same logic as the others but has its own internal memory.

If I just lost you, don't worry, it's actually not that complicated... and I guarantee it is worth the investment of your time!

Believe it or not, anyone who has programmed a PLC has probably used Function Blocks without knowing it. The workhorse of the PLC, the ubiquitous Timer Block is, in fact, a function block.

Timer Block

It has inputs, outputs, internal variables, and internal logic....and you can create as many as you like (within the memory limits of the PLC).

Typically, you would never think of writing your own Timer Block. You just expect every PLC to have them and you really never need to look inside the block because its functionality is well understood. Since it is already done for you, you can focus on the higher level logic and not on the creation of a low-level timer block. This same concept can be applied to user defined Function Blocks as well. The idea is to encapsulate your low-level code into a Function Block so that you can focus your energy on the higher level control.

Here are the top 5 reasons why you should use Function Blocks:

  1. Encapsulation - Reusable Function Blocks can be used to consolidate duplicate code.

    If you frequently find yourself copying and pasting code, you will benefit from encapsulating this code into a reusable function block.

    Ladder logic with duplicated code - encapsulated function blocks

    Encapsulated function block - IEC 61131-3

    After isolating the duplicated code and encapsulating it into a reusable Function Block, you can replace the original code with instances of your new block.

  2. Maintenance - Code is defined and managed in one spot.

    Normally you will create a reusable function block because you plan to use many instances of the block throughout your program. When you need to make changes or enhancements to the block (and you will), these changes are managed in one place and will propagate to all of the instances. This is a HUGE time saver, especially when you have hundreds of instances throughout your program.

  3. Standardization - Create a code library of standard function blocks

    Custom Function Blocks - IEC 61131-3

    You should create modular, reusable Function Blocks for all of your commonly used devices (Valves, Cylinders, Motors, Robots, Cameras, etc.). Standardized Function Blocks will allow you to maintain consistency among all systems in your organization. As long as you put enough thought into the interface and internal logic design of each block, you should be able to modularize the functionality such that the internal code is rarely viewed. This reduces the need for future programmers to fully understand all of the intricacies and internal workings of the block, which reduces development time and ultimately saves money.

  4. Collaboration - multiple programmers seamlessly working on the same project

    Function Blocks allow you to structure and modularize your program in such a way that easily lends itself to collaboration. Each programmer can be working on a separate function block. Since the blocks communicate to outside logic via the interface (inputs, outputs, etc.), you just need to know the interface structure of the block in order to use it. Someone can be working on the logic for a low-level function block while another person assembles the low-level blocks into a higher level program. We find this type of collaboration to be crucial for large projects requiring multiple programmers.

  5. Mix & Match Languages - Use the right language for the task at hand

    When you organize your code into separate reusable function blocks, you can more easily incorporate different IEC61131-3 languages into your project. We have many customers that prefer to write everything in ladder (which is perfectly fine). However, once in a while there is a need for a function that requires using another language like Structured Text. It may be a complex math routine, a communication block, or may have some complex array indexing, which means ST is the only good option. The simple solution is to encapsulate the Structured Text code into a Function Block that can be called in Ladder. Problem solved!...and the customer still only sees ladder in the main program.

    Structured Text - IEC 61131-3 Ladder Logic

So is there any downside to using Function Blocks?

In my (admittedly biased) opinion, the answer is NO, but feel free to comment below if you have some insight on the matter. I've mentioned before in previous posts that we can typically program an IEC 61131-3 compliant PLC in half the time it takes to program on a noncompliant PLC. Function Blocks are one of the major reasons for this enhanced efficiency. We write code once and then call as many instances of each block as needed. Copying and pasting code is old-school!

So get ready to unleash the power of your PLC!

If you are an OEM, a library of modular, reusable function blocks will speed your development time and allow you to better manage your code base. Or, if you are an end-user, you can provide your standard libraries to your OEMs when purchasing equipment. This will ensure that your standards are properly followed. And if you are an integrator, then you probably already know about the tremendous benefits of using Function Blocks and they are part of every project you deliver.

Learn more about DMC's PLC programming expertise.

Comments

pradeepan
# pradeepan
am doing a project on baggage handling systems in airports using ab plc (micro logix 1200 series c) but i don't know where to start.. so i need your help.. please guide me...
dzh
# dzh
downside is lack of version control software for FB based program files.
I can version control or compare or diff text based ST program files but can't when it comes to based program files.

Post a comment

Name (required)

Email (required)

CAPTCHA image
Enter the code shown above: