Six animated steps
OB, FB, FC and DB at a glance
Code blocks decide when and what runs; data blocks decide where values live. The bar under each card shows what happens to its memory.
Quick rule: outputs depend only on current inputs → FC. Must remember a state, timer, edge or mode → FB.
Reference: comparison table
| Block | Main purpose | Own memory? | Typical use |
|---|---|---|---|
| OB | Respond to a CPU event and organise execution | Not an instance memory block | Main cycle, startup, cyclic interrupt, hardware interrupt, diagnostics |
| FC | Perform reusable, usually stateless logic | No instance DB; TEMP data is not retained | Calculations, conversions, interlock evaluation, utility logic |
| FB | Perform reusable, stateful logic | Yes, through an instance DB | Motors, valves, pumps, sequences, equipment modules |
| DB | Store program data | It is a data area | Recipes, setpoints, HMI data, production values, FB instance data |
Organization Block (OB): When the CPU Runs Your Code
The operating system starts an OB when its event happens. Power up the CPU, then fire a hardware or diagnostic event and watch OB1 get interrupted.
Demo timing: OB30 set to 100 ms, time slowed down 4×. Which OBs exist, and their priorities, depend on the CPU and firmware.
Reference: the main OB types
Program cycle
Runs repeatedly while the CPU is in RUN. Keep it readable by calling FCs and FBs in a clear sequence.
Startup
Runs when the CPU changes to RUN. Initialise states only when startup behaviour is genuinely required.
Cyclic interrupt
Runs at a configured interval and priority, independently of the ordinary OB1 scan.
Hardware / diagnostic event
Runs when a configured interrupt or diagnostic event occurs, subject to CPU capability.
Available OB types and priorities depend on the selected CPU. Check the CPU properties and Siemens device documentation before assuming that a particular event OB is supported.
FC vs FB: Which Block Remembers?
The same start/stop latch, written once as an FC with a Temp variable and once as an FB with a Static variable. Press Start and watch each scan.
#Latch := #Start OR (#Latch AND NOT #Stop); // same line in both blocks
#Run := #Latch;Good FC applications
Good FC applications
- Scale an analogue input from raw counts to engineering units.
- Convert temperature, pressure, speed or length units.
- Calculate an alarm limit or efficiency value.
- Evaluate a stateless permissive or interlock condition.
- Move, format or validate a group of values.
// SCL-style example: stateless scaling function
#SpanIn := #RawMax - #RawMin;
IF #SpanIn <> 0.0 THEN
#ScaledValue := ((#RawValue - #RawMin) / #SpanIn)
* (#EngMax - #EngMin) + #EngMin;
END_IF;
Good FB applications
Good FB applications
- Motor, pump, fan, conveyor and valve control.
- Sequences, state machines and operating-mode control.
- Logic using timers, counters, edge detection or stored faults.
- Equipment modules that need status and command data.
- Reusable objects connected to HMI faceplates.
Function Block (FB): One FB, Many Instance DBs
The FB code exists once. Every call gets its own instance DB, so four roller beds keep four separate sets of values. Or group them as multi-instances inside one parent FB.
Data Block (DB): Global DB vs Instance DB
Instance DBs belong to one FB call. Global DBs hold shared data such as recipes and HMI values, ideally built from the UDTs in Part 3.
| DB type | Ownership | Best use | Important note |
|---|---|---|---|
| Global DB | Accessible by permitted program blocks | Recipes, setpoints, HMI exchange, plant data and configuration | Design a clear structure and avoid turning one DB into an uncontrolled data dump |
| Instance DB | Assigned to an FB call | Input, output, InOut and Static data for that FB instance | Normally manage the values through the FB interface rather than unrelated direct writes |
Block Picker: Choose the Right Siemens Block
Eight real jobs from the conveyor line. Pick the block for each one.
Reference: the 5-question selection guide
- Is the CPU event starting this logic?
Choose an OB for a program cycle, startup, timed interrupt, hardware event or supported diagnostic event. - Does the logic only process current input values?
Choose an FC for a stateless calculation or reusable operation. - Must the logic remember anything between calls?
Choose an FB with an instance DB for modes, states, timers, counters, edges or equipment history. - Do several blocks or an HMI need shared structured data?
Create a global DB with meaningful nested structures or PLC data types. - Will the same equipment logic be reused?
Build one FB and create a separate instance for each real device.
Recommended Program Structure: the Conveyor Call Tree
Short OB1, clear call order, one FB per equipment type. Press play to follow one cycle.
- Use consistent names: FB_PRB, FC_Scale_Level, DB_Recipe.
- Pass values through block interfaces; keep global writes in a few clear places.
- Safety logic belongs in the certified safety program; this tutorial covers standard logic only.
Step-by-step lab: OB, FB, FC and DB in TIA Portal
This small exercise demonstrates all four block types with a motor and an analogue current value.
- Create a project and add the correct CPU.
Open Program blocks beneath the PLC in the project tree. - Create FC_ScaleCurrent.
Add raw input and engineering-range parameters, then return the scaled current. Use LAD, FBD or SCL. - Create FB_Motor.
Add Start, Stop, Permissive and Feedback inputs; RunCmd and Fault outputs; use Static data for remembered state or fault logic. - Create an FB instance.
Call FB_Motor in OB1 and let TIA Portal create DB_Motor01, or select an existing compatible instance DB. - Create DB_ProcessData.
Add structured tags for setpoints, measured values and HMI-visible status. - Arrange OB1.
Call FC_ScaleCurrent first, write the result to DB_ProcessData, then call FB_Motor with clearly mapped tags. - Compile and test.
Download to a test PLC or PLCSIM where suitable. Monitor the blocks and confirm that each FB instance retains independent state.
Expected result: the FC returns a calculation without private retained state, the FB controls the motor and remembers its instance values, the instance DB belongs to that motor call, and the global DB shares process data.
Frequently asked questions
Why does an FC forget its value after the scan?
FC Temp variables only exist while the FC runs. At the next call they start again, so a latch or timer in Temp is lost. Use an FB with a Static variable, which is stored in the instance DB, when the value must survive between scans.
What is a multi-instance in TIA Portal?
A multi-instance is an FB declared as a Static variable inside another FB. Its data is stored inside the parent FB's instance DB, so 16 FB_PRB motors can live inside one DB_Conveyor instead of 16 separate DBs.
What is the main difference between FB and FC in Siemens PLC?
An FB has an associated instance DB and can retain Static values between calls. An FC has no instance DB and its Temp values are not retained after execution.
Can an FC use a data block?
Yes. An FC can read or write permitted global data, but that DB is not private instance memory belonging to the FC.
Can the same FB control several motors?
Yes. Call the same FB once per motor and assign a separate instance DB—or separate multi-instance—for every motor.
Should all code be written directly in OB1?
No. OB1 should normally coordinate calls. Placing reusable equipment and utility logic in FBs and FCs makes the project easier to test, maintain and expand.
What is the difference between a global DB and an instance DB?
A global DB is a shared program data area. An instance DB is tied to an FB instance and stores that call’s interface and Static data.
Does every FB require a separate DB?
Every FB instance needs instance storage. It may use a single-instance DB or be declared as a multi-instance inside a parent FB’s instance DB.
Official Siemens references
Technical definitions were checked against Siemens STEP 7 documentation: Overview of the block types and Function block (FB). CPU features and supported OBs can vary by device and firmware.