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:
- L





























