Siemens PLC Programming Guide

Siemens PLC Blocks Explained: OB, FB, FC and DB

Understand how a Siemens PLC program is organised, where logic should execute, which block should remember data, and how to build reusable machine code in TIA Portal.

Beginner to Intermediate10 min readS7-1200 / S7-1500LAD, FBD or SCL

What’s covered

  1. OB, FB, FC and DB at a glance
  2. Organization blocks (OB)
  3. Functions (FC)
  4. Function blocks (FB)
  5. Data blocks (DB)
  6. How to select the correct block
  7. Recommended program structure
  8. TIA Portal practical lab

OB, FB, FC and DB at a glance

Siemens separates a PLC program into code blocks and data blocks. An OB is the entry point triggered by the CPU operating system. An FC or FB contains reusable logic. A DB stores values. The most important selection question is whether the logic needs its own persistent internal state.

BlockMain purposeOwn memory?Typical use
OBRespond to a CPU event and organise executionNot an instance memory blockMain cycle, startup, cyclic interrupt, hardware interrupt, diagnostics
FCPerform reusable, usually stateless logicNo instance DB; TEMP data is not retainedCalculations, conversions, interlock evaluation, utility logic
FBPerform reusable, stateful logicYes, through an instance DBMotors, valves, pumps, sequences, equipment modules
DBStore program dataIt is a data areaRecipes, setpoints, HMI data, production values, FB instance data

Quick rule: use an FC when outputs can be calculated entirely from the current inputs. Use an FB when the block must remember a state, timer, edge, operating mode or previous value between calls.

Organization Block (OB): when should you use it?

An Organization Block is the interface between the CPU operating system and your user program. The CPU starts an OB when its assigned event occurs. You do not normally use an OB as a reusable equipment module; you use it to define when other program logic runs.

OB1

Program cycle

Runs repeatedly while the CPU is in RUN. Keep it readable by calling FCs and FBs in a clear sequence.

Use for: normal cyclic machine control
OB

Startup

Runs when the CPU changes to RUN. Initialise states only when startup behaviour is genuinely required.

Use for: safe initialisation and startup checks
OB

Cyclic interrupt

Runs at a configured interval and priority, independently of the ordinary OB1 scan.

Use for: deterministic sampling or control calculations
OB

Hardware / diagnostic event

Runs when a configured interrupt or diagnostic event occurs, subject to CPU capability.

Use for: fast event reaction and fault handling

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.

Function (FC): reusable logic without instance memory

An FC is a code block for a recurring task. It has an interface—typically Input, Output, InOut and Temp—but it does not have an associated instance DB. Temporary values exist only while the FC executes, so they should not be used as memory for the next scan.

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;

Avoid this mistake: an FC can still write to a global DB or other external memory, but doing so does not give the FC private instance memory. Excessive direct global access also reduces portability and testability.

Function Block (FB): reusable logic that remembers

An FB is a code block that stores its parameters and Static values in an instance DB. Each instance therefore has independent memory. The same Motor FB can be called for Motor 1, Motor 2 and Motor 3, with a separate instance for each motor.

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.
OB1 calls Motor FBMotor_1 instance DB+Motor_2 instance DB

The FB code is shared, but the two instance DBs keep their data separate. In an FB, another FB may also be declared as a Static variable to create a multi-instance. This groups nested instance data inside the parent instance DB and is useful for modular machine sections.

Data Block (DB): where program data lives

A DB contains data rather than executable networks. Siemens programs commonly use two DB categories:

DB typeOwnershipBest useImportant note
Global DBAccessible by permitted program blocksRecipes, setpoints, HMI exchange, plant data and configurationDesign a clear structure and avoid turning one DB into an uncontrolled data dump
Instance DBAssigned to an FB callInput, output, InOut and Static data for that FB instanceNormally manage the values through the FB interface rather than unrelated direct writes

For S7-1200 and S7-1500 projects, optimised data access and symbolic tags are usually the natural choice. Use non-optimised or absolute access only when a communication interface or legacy requirement makes it necessary, and document that decision.

How to select the correct Siemens block

  1. Is the CPU event starting this logic?
    Choose an OB for a program cycle, startup, timed interrupt, hardware event or supported diagnostic event.
  2. Does the logic only process current input values?
    Choose an FC for a stateless calculation or reusable operation.
  3. Must the logic remember anything between calls?
    Choose an FB with an instance DB for modes, states, timers, counters, edges or equipment history.
  4. Do several blocks or an HMI need shared structured data?
    Create a global DB with meaningful nested structures or PLC data types.
  5. Will the same equipment logic be reused?
    Build one FB and create a separate instance for each real device.

Recommended program structure

A scalable project keeps OBs short and uses descriptive symbolic names. The OB decides execution order; equipment FBs own their behaviour and state; utility FCs perform stateless operations; global DBs hold shared configuration and process data.

OB1 Main CycleFC PermissivesFB EquipmentGlobal DB / HMI
  • Pass values through block interfaces where practical.
  • Use consistent names such as FB_Motor, FC_Scale, DB_Recipe and DB_Motor01.
  • Keep safety logic in the appropriate certified safety environment; this tutorial covers standard logic only.
  • Document each block’s purpose, interface and calling conditions.

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.

  1. Create a project and add the correct CPU.
    Open Program blocks beneath the PLC in the project tree.
  2. Create FC_ScaleCurrent.
    Add raw input and engineering-range parameters, then return the scaled current. Use LAD, FBD or SCL.
  3. Create FB_Motor.
    Add Start, Stop, Permissive and Feedback inputs; RunCmd and Fault outputs; use Static data for remembered state or fault logic.
  4. Create an FB instance.
    Call FB_Motor in OB1 and let TIA Portal create DB_Motor01, or select an existing compatible instance DB.
  5. Create DB_ProcessData.
    Add structured tags for setpoints, measured values and HMI-visible status.
  6. Arrange OB1.
    Call FC_ScaleCurrent first, write the result to DB_ProcessData, then call FB_Motor with clearly mapped tags.
  7. 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

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.

Learn Siemens PLC programming with practical examples

Build structured S7-1200 and S7-1500 projects in TIA Portal with guided industrial exercises.

Siemens PLC & TIA Portal Learning Path

Continue with the related Siemens PLC tutorials in this practical learning series.

  1. SCL vs Ladder Logic
  2. Upload PLC Program
  3. TIA Selection Tool
  4. Analog Input Scaling
  5. PLC Counters
  6. PLC Timers
  7. Addressing & Data Types
  8. Hardware & PLC Tags
  9. OB, FB, FC & DB