Function Block Diagram (FBD) PLC Programming Tutorial for Beginners

The Function Block Diagram (FBD) is an official, widely used PLC programming language. It gives you a simple graphical way to connect functions in a PLC program. Function Block Diagram is easy to learn and supports many possibilities.
FBD is officially recognised as a PLC programming language in IEC 61131-3. You can use it to combine logic with timers and PID controllers, and even integrate a SCADA system into your projects.
Most engineers like FBD because its graphical layout is a familiar way to describe a system. Engineers often describe things in boxes, which is the idea behind function block diagrams. FBD is particularly useful when batch control concepts from ISA-88 are applied.
In this tutorial, I will introduce you to some of the basic principles of programming in FBD and explain the function blocks it uses.
Content of FBD Tutorial
What is Function Block Diagram?
If you know systems engineering, you may already be familiar with function block diagrams. PLC function block diagram works in much the same way. FBD puts functions containing many lines of code into boxes.
We can then connect these function boxes to each other, making it easier to build up a larger PLC program.
FBD, just like ladder logic and structured text, follows the IEC 61131-3 standard from PLCOpen. FBD is common in PLC programming and is often used alongside structured text. It gives you a straightforward way to connect functions within your program.
Function Blocks
FBD puts every function into a function block with one or more inputs and outputs. The relationship between its input and output states defines what the block does.
The following illustration shows what a simple function block can look like in FBD:
The box in this illustration is the function block. In the middle, you will often see either a symbol or some text. That symbol tells you what function the block performs.
Depending on its function, a block can have any number of inputs and outputs. You can take the output from one function block and connect it to the input of another. Joining the blocks together in this way creates a Function Block Diagram.
FBD has many predefined function blocks available for your program. You can also create your own blocks to suit the function you need. This is particularly useful when you want to reuse code, such as the code for controlling a motor or a valve. You can use that custom block multiple times instead of writing the same function again.
Start with the standard function blocks described in the IEC standard for PLC programming languages. Their functions range from basic operations to advanced ones.
Standard Function Blocks
The IEC standard describes a large number of function blocks. The following sections give an overview of the main blocks you will encounter in the official description of FBD.
Bit Logic Function Blocks
Logic is the most basic function of a PLC program. Combining it produces combinatorial logic. As the simplest form of algorithm, logic sets outputs according to input states. FBD has two basic bit logic functions, or operations. These two operations can be combined to produce many other logic functions.
To see how these bit logic operations work, start with the first one:
OR Logic Operation
The OR function block works like an OR gate. It has 2 inputs and 1 output.
If one of the inputs is true the output will also become true.
The illustration below shows the typical appearance of this block in an FBD program:
The OR operation uses the symbol >=1 to show the output condition. The output becomes true when the sum of the two inputs is greater than or equal to 1.
As in other bit logic operations, 0 means false and 1 means true. The OR truth table shows this relationship:
| >=1 |
IN1 | ||
| 0 | 1 | ||
| IN2 | 0 | 0 | 1 |
| 1 | 1 | 1 | |
An OR block works like two contacts connected in parallel in ladder logic. Closing either contact sets the output.
Here, output means the function block’s output. Its output pin can connect only to another function block. To use it to set a physical output or a bit, we need another block.
This is where the next function block comes in. It handles the assignment we need.
Assignment Operation
Usually, a function block’s output connects to another block’s input. Sometimes you need that output to control one or more bits, for example to set an output or assign a variable’s value.
Use an assignment function block for this. Assignment is itself a function block, so you cannot simply put a memory address at your existing block’s output.
The way an assignment block works is to take the value at its input and assign it to a location in the PLC’s memory.
The assignment function block also has its own output, which you can connect to other function blocks. This lets you assign a value at any point in the function block diagram where you need one. An assignment can therefore happen before the output of the last block in the diagram.
An assignment function works like a coil in ladder logic.
AND Logic Operation
The next function block to learn is AND. Like OR, the AND function block is one of the most fundamental blocks you will use when programming a PLC.
It has two inputs and one output. Although similar to OR, it requires both inputs to be true to set the output. OR needs only one true input.
If both the inputs are true the output will also become true.
We can illustrate the operation of the AND function block using the following truth table:
| & |
IN1 | ||
| 0 | 1 | ||
| IN2 | 0 | 0 | 0 |
| 1 | 0 | 1 | |
In ladder logic terms, an AND function block works like two contacts connected in series. Both contacts must be true for the block’s output to be true.
Negation Operation
Use negation when you need to invert a block’s input or output. How it appears depends on your programming software.
In some programming software, negation has a whole function block of its own. In other software, you can put a small circle on a pin of a function block to negate the signal at that pin.
Whether the operation is called NOT, invert RLO or negate assignment, its practical effect is to change the signal to its opposite.
When a signal is negated, or inverted, a true value becomes false. A false value becomes true in the same way. The small circle on the function block’s pin indicates exactly this change to the signal.
You can invert inputs as well as outputs by placing the circle on an input pin. Later, we will see how inverting all the inputs has the same effect on a block’s function as inverting its output.
Exclusive OR Operation
Exclusive OR, or XOR, is a special case of OR. The OR block requires input values greater than or equal to 1. As shown below, XOR requires the two inputs to equal 1.
For example, if both inputs are true, their sum is greater than 1 and the XOR output is false.
You can create the XOR function yourself using two AND function blocks and one OR function block. However, it is also available as a function block of its own. You will find this dedicated XOR block in Siemens TIA Portal, Codesys and many other systems.
One common reason to use this block is to check whether exactly one of two inputs has a true value.
NAND, NOR etc.
The next two function blocks also combine basic blocks. Called negated blocks, they have negated outputs.
The NAND block below illustrates this arrangement, using an AND block with a negated output. NAND means NOT AND, or Negated AND.
Compare this truth table with the one for AND. Each result is the exact opposite of the corresponding result in the AND table.
| NAND |
IN1 | ||
| 0 | 1 | ||
| IN2 | 0 | 1 | 1 |
| 1 | 1 | 0 | |
Negating the inputs also produces the NAND function described earlier. This has the same effect as negating the output, so a NAND block can also look like this:
Bistable Function Blocks
Next are bistable function blocks, which I think of as a simple form of memory. You can set or reset their output. The output (Q) remembers the last state of the set input (S1).
A pulse at S1 sets Q1. If S1 returns to false, the output stays true. In that sense, Q1 remembers an event at S1, which is why I describe bistable functions, or flip-flops, as memory.
Set/Reset
This function block is called SR, short for set/reset. The following illustration shows how it looks:
To understand this function better, look at what happens inside the function block. The body of the SR block shows how it is constructed from 2 basic bit logic blocks. These are an AND function and an OR function working together.
The output from the AND function connects to one input of the OR function. One of the inputs to the AND function is Q1. That same value is also used as the output for the whole function block.
This arrangement is often called a flip-flop function. Q1 remembers that S1 was true until R, the reset input, becomes true and resets Q1. Because the output (Q1) remembers its state, I think of this as a simple memory block.
Blocks of this type always have a priority. That priority determines what will happen to the output if both inputs become true at the same time.
SR blocks give priority to set. If both inputs are true, the output is therefore set.
Set priority will not always give you the behaviour you want. The next block provides the other option.
Reset/Set
Use the RS block when you want the output to reset if both inputs (S1 and R) are true.
The RS block operates in the same way as the SR block. The difference is that reset takes priority over set.
You can also construct this function from two basic logic blocks. However, SR/RS flip-flops are so fundamental to PLC programming that they have their own function blocks. You can use those blocks directly in your program.
There are several ways to remember a state. Flip-flops are simple, but the following blocks may be an even simpler form of memory.
Edge Detection
Detecting signal edges is useful in both PLC programming and electronics. Here, an edge means the change between a signal’s states.
Suppose a push button is connected to an input and you want to count how often you press it.
Normally, you would connect the input to a counter function block, covered later. However, a PLC has a scan time or a cycle time.
This cycle time is usually very short, around 20-50 ms. When you press a push button, even if you release it quickly, the input stays on for much longer than that. A typical press lasts 100-200 ms, so the PLC sees the input on during several successive scan cycles.
With the input true, the counter increments each time the PLC reaches its block. A single button press therefore adds 2, 3, 5 or even more counts instead of one.
To avoid counting the same button press repeatedly, we will use edge detection blocks.
R_TRIG Function Block
One edge in a digital signal is the rising edge, also called the positive edge.
A rising edge occurs as the signal changes from false (0) to true (1), or from 0 to 5V in digital electronics. In the button example, it occurs when we press the button.
When the block detects a rising edge at input CLK, it sets the output. This lasts for only a brief moment because the rising edge itself happens quickly. The input may still be true during the following scan cycles, but that does not cause the output to be set again. Detecting a positive, or rising, edge generates a single pulse at the output.
The following diagram shows the operation of the function block and helps explain its behaviour:
Another output pulse requires a new rising edge. The block remembers that it has seen a positive edge, so the input must return to false and then become true again before the output can be set. With the push button, you must release it and press it again.
F_TRIG Function Block
As well as rising edges, you can detect falling edges in a signal. A falling edge occurs when the signal changes from true (1) to false (0).
The falling edge is sometimes called the negative edge. This block generates an output pulse when it detects that edge at the input.
A typical reason to use this block is when you want one thing to happen at the moment another thing stops.
Suppose a yellow lamp must turn on when a motor stops, provided the motor has already started. The lamp must come on at the moment the motor stops.
Use F_TRIG to detect the falling edge at the motor output. Its pulse can then set the yellow lamp’s output.
Timer Function Blocks
The previous blocks limit a signal to the scan time. Sometimes you need to control a signal’s duration or when it occurs.
Timer function blocks provide this control. Timers are among the most used PLC functions and fall into three types.
For more about timers and a PLC simulation showing how they work, read the article on PLC timers. It includes video tutorials for the on delay timer, off delay timer and pulse timer.
Some argue that one timer is enough because it can be used to derive all timer functions. However, IEC 61131-3 describes all three, and most software provides them, so we will cover each one.
Pulse Timer (TP)
The pulse timer comes first. Its name describes its function: generating a pulse of a specified length.
The TP block has two inputs and two outputs. In the blocks covered so far, all the inputs and outputs used boolean data. The IN input and Q output of this timer still use boolean values. PT and ET work differently, with both using variables of the data type TIME.
PT, or Preset Time, is the input that sets the duration of the pulse at Q. When IN becomes true, Q is set for the time specified at PT.
ET means Elapsed Time and shows how long Q has been active.
To run a ventilation fan for 10 minutes, enter 10m at PT and connect the fan to output Q. ET lets you see how long the fan has been running, which is often useful.
You can see the behaviour of the pulse timer in more detail by following the signals in the timing diagram above.
On Delay Timer (TON)
The next timer type is the On Delay Timer, usually shortened to TON. The pulse timer controls how long a pulse lasts. With TON, you instead set the delay before the pulse occurs.
Turning on the input starts the timer. Once PT has elapsed, it sets output Q, which explains its name.
It turns on the output after a delay
The on delay timer uses the same input and output pins as the pulse timer. ET again shows how much time has elapsed.
The timing diagram below illustrates the operation of the on delay timer, showing its inputs and outputs:
This is the timer some people claim is the only one needed. Before explaining that claim, we will look at the third and final timer type.
Off Delay Timer (TOF)
The operation of TOF is similar to that of TON. However, one main difference determines how this timer controls its output.
It turns off the output after a delay
Setting the input true immediately sets the output. While the input remains true, the output stays on until PT has elapsed. The output then turns off.
At first, the off delay timer looks similar to the pulse timer (PT). One difference separates their behaviour.
The pulse timer keeps its output on for PT even if the input becomes false during timing, because it detects a rising edge at its input. The off delay timer instead stops counting if the input comes back.
IEC 61131-3 describes these three official timer types. With some ladder logic, you can technically build all of them using only an on-delay timer.
Counter Function Blocks
The next function blocks add an input and an output, along with another data type. These are the PLC counters.
Counting is fundamental to PLC programming. A counter can track how many products a machine has produced or the tool’s current sequence step. These are among the many reasons to use counters in a PLC program.
IEC provides three standard counter blocks. One counts up and another counts down. The third can count in either direction.
Start with the first of these counter blocks, which lets us count upwards.
Up Counter (CTU)
The up counter block has three inputs and two outputs, all needed for counting. Before examining each connection, consider how the block works.
Each pulse on CU will count CV up by 1. When CV >= PV then Q is set.
CU detects a rising edge and increases CV by 1. For this to work, output CV uses a different data type: integer.
The integer is a 16-bit number, with the first bit used for signed/unsigned, allowing a count up to 32767. Input PV and output CV both use the integer data type. PV sets the limit at which boolean output Q becomes true.
Reset input R sets CV to 0. Counting usually starts at 0 so that CV always shows the number of pulses received at CU.
For readers familiar with textual programming languages, this is the function block body in structured text:
IF R THEN
CV := 0;
ELSIF CU AND (CV < PVmax) THEN
CV := CV + 1;
END_IF;
Q := (CV >= PV);
Down Counter (CTD)
Counting down is useful too. If your PLC program needs to repeat an operation a specified number of times, a down counter helps track the repetitions.
Each pulse on CD will count CV down by 1. When CV <= 0 then Q is set.
It works like the up counter but subtracts 1 instead of adding one. This creates another difference: a down counter needs a value to count from.
Counting upwards usually starts at 0. To count down, we need to load a starting value using input LD. Setting LD true copies PV into output CV.
The following structured text shows what the function block body looks like:
IF LD THEN
CV := PV;
ELSIF CD AND (CV > PVmin) THEN
CV := CV - 1;
END_IF;
Q := (CV <= 0);
Now that you have seen how to count upwards and downwards separately, we can look at the final counter block.
Up Down Counters (CTUD)
Some applications need to count in both directions. IEC therefore provides an official block that counts both up and down.
This counter combines the two previous blocks, with 5 inputs and 3 outputs. Their names and functions match the inputs and outputs of those blocks.
To understand how the combined counter works, look at the following function block body:
IF R THEN
CV := 0;
ELSIF LD THEN
CV := PV;
ELSE
IF NOT (CU AND CD) THEN
IF CU AND (CV < PVmax) THEN
CV := CV + 1;
ELSIF CD AND (CV > PVmin) THEN
CV := CV - 1;
END_IF;
END_IF;
END_IF;
QU := (CV >= PV);
QD := (CV <= 0);
Although the code may initially look complicated, it combines the up and down counters. CU increases CV by 1, while CD decreases CV by 1. QU and QD use the same conditions as the outputs of the two previous counter blocks.
Comparison Function Blocks
The next function blocks compare numbers. They answer questions such as whether A equals B or CV is greater than PV.
Several earlier blocks already used comparisons. The OR block, for example, compared two boolean inputs and set its output if their total equalled 1.
FBD comparisons support most data types and all real numbers, as well as booleans. Imaginary numbers are not included yet. These comparisons are useful and something you will use frequently.
All comparison blocks share the same basic arrangement: two or more inputs and a single output. Each input can accept any elementary data type, while the output always uses a boolean data type. You need to take care with the data types you compare, because comparisons between them can be tricky.
To see how comparison works, begin with the first basic comparison block:
Equality (=)
An equality function block sets its output when two variables are equal. I think of it as asking:
Is IN1 equal to IN2?
Although simple, this block needs care when choosing data types. You will probably use it most often with integers. For example, to set an output when a machine reaches step 12, use the step variable at IN1 and set IN2 to 12.
The three dots between the inputs show that the block accepts more than two inputs. All must be equal to set the output. Structured text describes this function as:
OUT := IN1 = IN2 = ... = INn
Equality comparisons can cause problems with other data types. An analog value, using the real data type, is very unlikely to be exactly equal to another real value.
Both inputs must match exactly before this block sets its output. Real data types use floating-point numbers. To trigger an action at 80 degrees, an equality comparison with reals would require exactly 80.0 degrees. A combination of comparison blocks is better for this situation because it checks a range of values instead of a single number.
The equality block remains useful for integers and, sometimes, words.
Inequality (<>)
To check whether numbers differ, use the inequality block. Negating the equality block’s output would also work, but it would be more confusing than using the dedicated block.
This block works like equality but checks whether the values differ. To remember its function, ask:
Is IN1 NOT equal to IN2?
Unequal inputs set the output. It remains set while they differ and turns off as soon as they become equal.
Readers familiar with structured text will recognise the inequality symbol. Many programming languages write it as !=, while PLC programming uses <>.
When using this block, remember that it accepts only two inputs.
Less Than (<)
The next two blocks may solve the earlier problem with equality comparisons of real values. They let us check whether a variable falls within a range.
The official name is increasing sequence, but less than is easier to remember. It asks:
Is IN1 less than IN2?
It checks whether the first input’s value is smaller than the second’s. This works well with real numbers, for example when checking whether a temperature is below 80.5 degrees.
The name increasing sequence makes more sense when the block has multiple inputs. Like equality, it can be extended beyond two inputs.
With multiple inputs, the values must form an increasing sequence. In1 must be less than IN2, which must be less than IN3, and so on. In text, the function is:
OUT := (IN1 < IN2) AND (IN2 < IN3) AND ... (INn-1 < INn)
Greater Than (>)
You can check a decreasing sequence as well as an increasing one. In simpler terms, this block checks whether one value is greater than another. It asks:
Is IN1 greater than IN2?
Like the previous block, it accepts more than two inputs. Those inputs must form a decreasing sequence to set the output.
Each successive input must be smaller than the previous one, forming a decreasing sequence. In a textual language such as structured text, the greater than function is:
OUT := (IN1 > IN2) AND (IN2 > IN3) AND ... (INn-1 > INn)
Combining greater than and less than blocks is useful for checking ranges of analog values. To test whether a temperature is between 90 and 100 degrees, for example, first check that it is greater than 90, then check that it is less than 100.
Comparison blocks are often combined, particularly equality with less than or greater than. These combinations often have dedicated function blocks:
- Less than OR equal to
- Greater than OR equal to
Selection Function Blocks
Comparing two or more values is useful in a PLC program. Sometimes, however, you need to choose one of those values instead of comparing them. Selection function blocks let you make that choice and use the selected value.
What these blocks share is the ability to select a value. That selection can happen with or without a condition. At their simplest, these blocks carry out an assignment: they select one of the input values, then assign that value to the block’s output.
Move
The simplest selection block is move. You have already seen the assignment block, which has the same basic function.
The assignment block copies its input value to its output, and move does the same. FBD provides both because, despite this similarity, they have one main difference.
Move supports any data type, whereas assignment supports only boolean data. With move, you can transfer a value from any data type to any other.
For readers who prefer structured text, the same function has a simple textual form:
OUT := IN
The illustration below shows how the move block appears in function block diagram:
The block shown has only one input and one output. Since there is just one value available, the idea of selecting between values does not really apply here. However, many implementations let you add more outputs to the move block. You can then use a single block to move the value to several different places.
The move block accepts any data type at its input and can transfer that value to any data type at the output. You do not have to choose the same type for both ends of the block. For example, you can move an integer to a real, or a TIME value to a double word.
Moving between different data types makes this block tricky to use. You need to be careful whenever the type at the output differs from the type at the input. For beginners, I strongly recommend moving values only to the same data type as the input. Even for professional programmers, I rarely recommend mixing the types. It makes the PLC program more complicated.
Binary Selection (SEL)
Binary selection makes an actual choice. It selects between two values and assigns the chosen value to an output.
As its name suggests, a binary (boolean) input chooses between two input values. The block has three inputs: condition G and the values IN0, IN1. G uses a boolean data type to select one of those values.
The input names differ from those on other blocks to make this block easier to understand. Condition G has two possible values, 0 and 1, which correspond to the names IN0 and IN1.
G selects IN0 when it is 0 and IN1 when it is 1. The selected input’s value is then assigned to output OUT.
We can also describe the same selection in structured text, as shown here:
IF G = 0 THEN
OUT := IN0
ELSIF G = 1 THEN
OUT := IN1
END_IF;
Extensible Multiplexer
Sometimes you need to choose between more than two values. The extensible multiplexer handles that situation. If you have worked with digital electronics, you may already know the word multiplexer. This block performs the same function: it chooses one input value from several available inputs.
As with binary selection, a condition (selector) accompanies the input values. The selector’s value determines which of the N inputs is selected.
The condition input is called K on this block. Unlike the condition on the binary selector, it accepts any elementary data type instead of being limited to boolean. However, although the block allows other types, an integer is the only data type that really makes sense for selecting an input here.
Choosing between IN0 and INn requires a whole number at K. Only numbers between 0 and n are valid. The function can be expressed as:
OUT := INK
Here, K must fall within 0 – n, with n being the number of inputs.
Extensible Minimum Function (MIN)
So far, selection has used a condition input, either binary or integer depending on the number of available inputs.
Sometimes the values themselves determine the selection. You might need the highest or lowest value among several variables.
The next two blocks make that selection.
You will need to choose the smallest variable at some point. The block for this is officially called extensible minimum, often shortened to minimum or MIN. Extensible means you can add any number of inputs. Each accepts any elementary data type, and a single block can have inputs of different types.
OUT := MIN (IN1, IN2)
The value assigned to this block’s output is always the smallest of its input values. In other words, the block selects the input with the minimum value.
Extensible Maximum Function (MAX)
Alongside the block for finding the minimum, FBD provides a standard function block for finding the maximum value.
Its operation is the same as the minimum function, except that it selects the variable with the highest value. That value is then assigned to the output.
As with minimum, this block accepts any elementary data type at the inputs and output.
For both blocks, check the output’s data type. If you find the maximum of real values 80.46 and 206.95 but use an integer output, the result will be 206.
OUT := MAX (IN1, IN2)
Both of these blocks can also compare values with time and date data types. For example, you may have several date stamps in your PLC program and want to know which is the latest. Applying the maximum function to those date stamps finds the most recent date.
Limiter (LIMIT)
Combining minimum and maximum creates a new function for limiting values. Called a limiter, this block sets limits on a range of values, something you will often need in a PLC program.
The limiter is made from the minimum and maximum functions working together. Looking at its function block body shows how they combine:
OUT := MIN ( MAX (IN, MN), MX)
This sets minimum and maximum limits for the variable at input IN. Inputs MIN and MAX define those limits. All three inputs accept any elementary data type.
Make your own function blocks
The blocks above cover most of those described in IEC 61131-3, the official PLC programming language standard. FBD provides many more blocks, with one for almost every PLC programming operation. Examples include:
- Arithmetic Function Blocks
- Bit Shift Function Blocks
- Character String Function Blocks
- Conversion Function Blocks
- Communication Function Blocks
These are some examples, and many other function blocks are available.
I recommend experimenting in your Automation IDE, such as TIA Portal or Codesys. Create simple PLC programs in function block diagram. In my experience, this is the best way to learn new function blocks.
Every function block contains a function. Its body describes that function using structured text or ladder logic, or another PLC programming language. Standard blocks cover many functions, but they cannot always provide everything you need.
When the available standard blocks cannot provide the function you need, you will need to build your own function blocks. Creating your own blocks is one of the basic parts of structured PLC programming. PLC Academy will explain more about how to do this.










































Pls check.
IEC 31161-3 => IEC 61131-3
Thanks! I corrected it 🙂
very very helpful, thank you very much for your great job. Is it possible to get a pdf version link as the structured text tuto as well?
Please send me the ladder logic of analogue output functional block with logic that i can understand easily….
Very in depth article on functional block diagrams. FBD can make ones life so much easier when the complexity of ladder and instruction list just becomes so messy. Thanks for the article.
Heart ♥ touching
Thanks a lot! 🙂
AND with negated inputs in NOR…
A’.B’=(A+B)’
(A.B)’ = A’+B’
??
Good morning,
About the NAND section: when you write “Therefore a NAND block could also look like this;”, it is wrong.
/(A.B) is not equivalent to /A./B
Thanks for the article
HELLO, HOW CAN I GOT THE FBD APPLICATION SOFTWARE?
Hey! You can use Codesys (free to download on http://www.codesys.com). It is a free open source PLC programming software, where you can use all the PLC programming languages.
Your tutorials are very educative. Do you have a PDF for all the tutorial parts on PLC programming and SCADA systems?
I would really appreciate.
Hey Fred! Thank you very much! Yes I’m making PDF’s of all my articles. They will soon be available for all of them. Sign up for my newsletter or write me an e-mail. Then I will notify you whenever I write more articles and PDF’s.
is there any separate languages to program the DCS…?