Function Blocks (FB) in Siemens TIA Portal are reusable code structures that permanently store their input, output, and in-out parameters in associated instance data blocks (DBs). This memory-retaining behavior allows Function Blocks to preserve values across scan cycles, making them ideal for controlling motors, monitoring sensors, handling timers, or managing process states.
Function Blocks can also use temporary variables, which exist only during a single program cycle and are not saved in the instance DB. These are useful for intermediate calculations and logic that doesn't require persistent storage.
Function Blocks are different from Functions (FC), as Functions do not retain state between calls. This makes FBs more suitable for logic where historical data or ongoing states need to be tracked.
In Siemens TIA Portal, you can create your own Function Blocks using any supported PLC programming language, such as Ladder Diagram (LAD), Function Block Diagram (FBD), or Structured Control Language (SCL). While the internal logic of the FB can be written in your preferred language, the block itself serves as a reusable container of logic that can be instantiated multiple times with different data.
In this article, we focus on how to create a Function Block in Siemens TIA Portal and implement its internal logic using Structured Control Language (SCL). SCL is a high-level text-based language, ideal for writing efficient, readable, and scalable PLC logic, especially when dealing with complex conditions, loops, or mathematical operations.
Function Blocks (FBs) are essential for implementing reusable and modular code in Siemens TIA Portal. They contain subroutines and logic that are executed every time the block is called by another code block, such as an organization block (OB) or another FB. One of the key advantages of Function Blocks is that they can be called multiple times at different locations within a program, each time using a unique instance data block. This allows the same logic to be applied across multiple devices or processes — such as motors, sensors, or valves — without duplicating code. FBs simplify the development and maintenance of large automation systems by encapsulating frequently recurring logic into structured and testable modules. They are especially useful in applications involving:
By using Function Blocks with Structured Control Language (SCL), you gain even more control and flexibility in expressing complex algorithms, loops, and conditional logic, while maintaining code clarity and reuse. This makes FBs a powerful tool for scalable, efficient, and professional-grade PLC programming in industrial automation projects.
A Function Block (FB) is a reusable block of code that stores its data using an instance data block (DB). Unlike Functions (FC), Function Blocks can remember values from one cycle to the next, making them ideal for tasks like motor control, timers, and counters.
There are three main types of Function Block usage in TIA Portal:
Each type helps structure your program based on memory needs, reusability, and system complexity.
To create a Function Block (FB) using Structured Control Language (SCL) in Siemens TIA Portal, follow these steps:
This will create a new SCL-based Function Block where you can write your control logic using simple, readable high-level code.
In this example, we create a simple Function Block (FB) using SCL, which includes both input and output parameters. Defining parameters is an essential part of creating reusable logic blocks. Inputs are used to receive data from outside the FB, and outputs are used to send results back to the main program.
Input parameters are used to provide values or control signals to the function block. In this example, we define the following input tags:
Output parameters are used to provide the results or status back to the main program. In this function block, the following outputs are defined:
These parameters make the function block modular and reusable, so you can use it in multiple places without writing the same logic again.
This is a basic example of using a Function Block (FB) in Structured Control Language (SCL) to control a motor using a timer and a level condition.
In this logic, when the high level value becomes greater than 33.45 and the emergency stop (em_stop) is not activated, the motor is started for a fixed time using a Pulse Timer (TP).
The TP timer keeps the output Q active for the duration specified in PT (preset time). The elapsed time is captured in ET. This elapsed time is converted to a numeric value and stored in current_lvl to monitor how long the motor has run.
Once the level goes below the limit or the emergency stop is pressed, the motor will stop on the next scan cycle.
Once you create your Function Block (FB), compile it, and open the main program block — usually called OB1. From here, you can call the FB and use it in your main logic.
1. Single Instance: The function block gets its own separate data block to store values. This is best for simple and reusable tasks.
2. Multi-Instance: The FB is used inside another FB and shares the same instance data block. It’s useful when you want to organize logic and save memory.
This makes the FB fully active in your main program, ready to run its logic based on the inputs you provide.
After writing your Function Block and main logic in SCL, the next important step is to check your program for errors and download it to the PLC. This ensures that your logic is working correctly and ready for execution in the real environment.
First, click on the "Compile" button in TIA Portal to check for syntax or configuration errors. Make sure all blocks, tags, and instances are correctly assigned and there are no red error messages.
Before downloading the program to the actual PLC, it's highly recommended to test it using the built-in PLC simulator (PLCSIM). The simulator allows you to run the code in a virtual environment, monitor values, and verify your logic without needing physical hardware.
Once your logic works as expected in simulation, connect to the PLC device, set it to online mode, and download your program. Confirm all prompts, and ensure that your Function Blocks and OB1 are selected for transfer.
After downloading, switch the PLC to RUN mode. You can now monitor your Function Block execution in real-time and ensure the inputs and outputs respond as expected.