Technical Article

PLC Programming With Function Block Diagrams

August 29, 2023 by Shawn Dietrich

Function block diagram (FBD) programming is a common language for PLCs following the IEC 61131 standard. What is FBD, and how does it differ from the familiar ladder logic programs?

What Are Function Block Diagrams?

An FBD (function block diagram) program is a graphical programming language that joins functions together to produce an output. Using lines and flags to represent variables in a more graphical format, the user can quickly build a program by dragging, dropping, and connecting different function blocks and variables. 

In the end, the program looks similar to logical block diagrams that might have been used to describe complex processes in school or during the design phase of equipment. By using a graphical programming language, the user can quickly produce programs using proven, debugged, code which reduces integration time and results in less wasted product. Users do not need an extensive programming or electrical background to develop FBD programs.


Figure 1. An example of a function block diagram program. I

Figure 1. An example of a function block diagram program. Image used courtesy of Schneider Electric
 

What are Function Blocks?

A function block can be written in any IEC-supported language, parameters (inputs) are passed into the function block, some logic is performed on the parameters, output variables are set or data is passed out of the block. Function blocks can contain small amounts of code or larger sections of code. The intent of a function block is to reduce the amount of repeated code. This is accomplished by using the input parameters writing the code to be dynamic then setting the output parameters. 

A simple FBD example would be triggering a camera. Many handshakes are required to ensure the picture has been taken and the results will need to be copied to a useful datatype. The function block may only need one input to start the acquisition and two outputs, one being an error flag, and the other being the results of the inspection. This same block can be used for each camera in the system. When debugging you would only need to debug one section of code. 

 

FBD Program Execution

FBD program execution is dependent on the functions that are being called. If for example, you are calling a custom function that has many internal processes and in parallel you are also calling a simple function the parallel process will execute faster to the second function. This can cause some confusion. It is important to note that functions can run in parallel but will execute at different rates. Program execution is typical from left to right, but can go up and down depending on the placement of function blocks. 


Figure 2. Function blocks, although not strictly ladder logic, look familiar to ladder users as they accomplish some ladder tasks.

Figure 2. Function blocks, although not strictly ladder logic, look familiar to ladder users as they accomplish some ladder tasks. Image courtesy of Wikipedia

 

When Should I Use FBDs?

Many die-hard PLC programmers will swear by ladder logic or structured text programs, but there are occasions when an FBD program could result in a faster more responsive system. 

Process automation works well with FBDs because block diagrams are used to design and explain their process when data is shared between events and other processes. One step can only advance once an input process has been completed, this is different from a sequence where boolean sensors or events are used to determine the end of a sequence. 

Think of baking cookies, you need to gather ingredients, prep ingredients, bake, package, and ship the cookies. The output of one function directly affects the next function and some functions like bake might have two inputs, cookies, and pre-heat oven for example. 

Safety systems are commonly using FBD programs because it is simple and quick to read. Complex safety programs leave room for errors, and mistakes within a safety program could cost human lives or injuries. 

Situations with repetitive code within a program can benefit from FBD programs by simply dragging, dropping, and assigning a user can build a program with proven functions very quickly. This can reduce training and the requirement for advanced-level programmers on the shop floor. Collaborative robots are making use of FBDs for this specific reason.

Multiple PID loops work well in FBDs because of their parallel processing abilities. While other programming languages also have parallel processing capabilities, it’s easier to follow multiple processes in an FBD program. 


Figure 3. Organizing ladder diagrams can significantly reduce confusion and workflow troubleshooting.

Figure 3. Organizing ladder diagrams can significantly reduce confusion and workflow troubleshooting.

 

How To Use FBDs

When building a program using the FBD language you want to make sure your program is fairly simple. Even though FBD programs are easy to read and debug they can be made very complex with many functions connected together. Try to spread out your functions as much as you can. When functions are bunched together it is hard to tell where the lines are coming from and where they are going. 

Reduce the number of branched outputs, some programming environments will only allow for so many outputs, if any, to be connected together. Keep the number of output connections to no more than three. This reduces complexity and will keep your diagram less cluttered. 


Figure 4. A practical example of function blocks that represent a process, not exactly a program.

Figure 4. A practical example of function blocks that represent a process, not exactly a program. Image used courtesy of Wikipedia

 

Function Block Diagrams in Practice

While FBDs might not be very popular in the automated assembly industry they do have a purpose in automation. The process automation and pharmaceutical industries both use FBDs because of their easy-to-use and easy-to-understand graphical user interface. National Instruments makes lab-style control systems that use the FBD programming language as its native language.