How to Use Function Blocks in Siemens PLC Programming
In Siemens PLC programming, use
Function Blocks (FB) to create reusable code modules with internal memory. You declare an FB, instantiate it as an instance data block, and call it in your program to execute its logic with stored states.Syntax
A Function Block (FB) in Siemens PLC is declared with a name and contains code and internal variables. To use it, you create an instance data block (DB) that stores its data, then call the FB in your program with the instance DB as a parameter.
Key parts:
FUNCTION_BLOCK FB_Name: Declares the function block.VAR_INPUT: Input variables to the FB.VAR_OUTPUT: Output variables from the FB.VAR: Internal variables that keep state.END_FUNCTION_BLOCK: Ends the FB declaration.- In the main program, instantiate the FB with a data block, e.g.,
MyFB_Instance : FB_Name; - Call the FB with
MyFB_Instance();to execute it.
structured_text
FUNCTION_BLOCK FB_Timer
VAR_INPUT
Start : BOOL;
PT : TIME;
END_VAR
VAR_OUTPUT
Q : BOOL;
ET : TIME;
END_VAR
VAR
StartTime : TIME;
Running : BOOL;
END_VAR
IF Start AND NOT Running THEN
StartTime := TIME();
Running := TRUE;
END_IF
IF Running THEN
ET := TIME() - StartTime;
IF ET >= PT THEN
Q := TRUE;
Running := FALSE;
ELSE
Q := FALSE;
END_IF
ELSE
ET := T#0s;
Q := FALSE;
END_IF
END_FUNCTION_BLOCKExample
This example shows how to declare a simple timer function block and use it in a main program. The FB counts elapsed time when started and sets an output when the preset time is reached.
structured_text
FUNCTION_BLOCK FB_Timer
VAR_INPUT
Start : BOOL;
PT : TIME;
END_VAR
VAR_OUTPUT
Q : BOOL;
ET : TIME;
END_VAR
VAR
StartTime : TIME;
Running : BOOL;
END_VAR
IF Start AND NOT Running THEN
StartTime := TIME();
Running := TRUE;
END_IF
IF Running THEN
ET := TIME() - StartTime;
IF ET >= PT THEN
Q := TRUE;
Running := FALSE;
ELSE
Q := FALSE;
END_IF
ELSE
ET := T#0s;
Q := FALSE;
END_IF
END_FUNCTION_BLOCK
PROGRAM Main
VAR
Timer1 : FB_Timer;
StartSignal : BOOL := TRUE;
PresetTime : TIME := T#5s;
END_VAR
// Call the function block
Timer1(Start := StartSignal, PT := PresetTime);
// Outputs
// Timer1.Q will be TRUE after 5 seconds
// Timer1.ET shows elapsed time
END_PROGRAMOutput
After 5 seconds, Timer1.Q = TRUE and Timer1.ET >= T#5s
Common Pitfalls
Common mistakes when using function blocks in Siemens PLC include:
- Not creating an instance data block for the FB, which causes loss of internal state.
- Calling the FB without passing the instance, leading to errors.
- Confusing
FUNCTION(stateless) withFUNCTION_BLOCK(stateful). - Not initializing internal variables properly, causing unexpected behavior.
Always remember that each FB instance keeps its own data, so you must declare separate instances for multiple uses.
structured_text
(* Wrong: Calling FB without instance *) PROGRAM Main VAR Timer1 : FB_Timer; END_VAR // Incorrect call - missing instance FB_Timer(Start := TRUE, PT := T#5s); END_PROGRAM (* Right: Call with instance *) PROGRAM Main VAR Timer1 : FB_Timer; END_VAR Timer1(Start := TRUE, PT := T#5s); END_PROGRAM
Quick Reference
| Concept | Description |
|---|---|
| FUNCTION_BLOCK | Declares a reusable block with memory |
| VAR_INPUT | Inputs to the function block |
| VAR_OUTPUT | Outputs from the function block |
| VAR | Internal variables to keep state |
| Instance Data Block | Stores data for each FB instance |
| Calling FB | Use instance name with parameters to run FB |
Key Takeaways
Function blocks in Siemens PLC store internal state and require instance data blocks.
Declare FBs with inputs, outputs, and internal variables using structured text syntax.
Always create and use an instance of the FB to preserve data between calls.
Call the FB in your program by passing inputs and reading outputs from the instance.
Avoid calling FBs without instances and confusing FBs with stateless functions.