FBD Practical Examples

This section provides practical examples of Function Block Diagram programming patterns. These examples show how to combine standard and custom blocks to solve real-world control problems using the data-flow approach.

Temperature Alarm System

This example uses comparison blocks and Boolean logic to generate alarms from a temperature sensor.

Variables

NameTypeClassDescription
sensor_tempREALLocalCurrent temperature reading
high_limitREALLocalHigh temperature threshold (e.g., 80.0)
low_limitREALLocalLow temperature threshold (e.g., 5.0)
high_alarmBOOLLocalHigh temperature alarm
low_alarmBOOLLocalLow temperature alarm
any_alarmBOOLLocalCombined alarm output

FBD Structure

How It Works

  1. The GT (Greater Than) block compares sensor_temp against high_limit. If the temperature exceeds the limit, high_alarm goes TRUE.
  2. The LT (Less Than) block compares sensor_temp against low_limit. If the temperature drops below the limit, low_alarm goes TRUE.
  3. The OR block combines both alarms. If either alarm is active, any_alarm is TRUE.

This pattern scales easily. Add more comparison blocks for additional thresholds (warning levels, critical levels) and chain them with OR/AND blocks.

Analog Signal Scaling

Industrial sensors often output raw values (e.g., 0–4095 from a 12-bit ADC) that need to be scaled to engineering units.

Variables

NameTypeClassDescription
raw_inputINTLocalRaw ADC reading (0–4095)
raw_realREALLocalRaw value converted to REAL
scaled_valueREALLocalScaled output in engineering units
raw_minREALLocalMinimum raw value (0.0)
raw_maxREALLocalMaximum raw value (4095.0)
eng_minREALLocalMinimum engineering value (0.0)
eng_maxREALLocalMaximum engineering value (100.0)
span_rawREALLocalRaw range
span_engREALLocalEngineering range
normalizedREALLocalNormalized 0.0–1.0 value

FBD Structure

The scaling formula is: scaled = eng_min + ((raw - raw_min) / (raw_max - raw_min)) * (eng_max - eng_min)

How It Works

This example demonstrates FBD's strength: a multi-step mathematical calculation expressed as a clear left-to-right data flow. Each block performs one arithmetic operation, and the chain transforms a raw integer into a scaled real-number value.

Timer-Based Pump Control

This example uses TON timers to implement a pump that must run for a minimum duration once started, with a cooldown period before it can restart.

Variables

NameTypeClassDescription
start_cmdBOOLLocalOperator start command
stop_cmdBOOLLocalOperator stop command
pump_runningBOOLLocalCurrent pump state
min_run_okBOOLLocalMinimum run time has elapsed
cooldown_okBOOLLocalCooldown period has elapsed
can_startBOOLLocalStarting is permitted
can_stopBOOLLocalStopping is permitted
run_timerTONLocalMinimum run timer
cooldown_timerTONLocalCooldown timer

FBD Structure

How It Works

  1. The run timer counts while the pump is running. After 30 seconds, min_run_ok allows stopping.
  2. The cooldown timer counts while the pump is stopped. After 60 seconds, cooldown_ok allows restarting.
  3. The start AND block ensures starting only when commanded, cooldown has elapsed, and pump is not already running.
  4. The stop AND block ensures stopping only when commanded and minimum run time has elapsed.
  5. The SR bistable holds the pump state between scan cycles.

Counter with Reset and Display

Count items passing a sensor and reset when a batch is complete.

Variables

NameTypeClassDescription
item_sensorBOOLLocalItem detected (pulsed)
batch_resetBOOLLocalReset counter for new batch
batch_sizeINTLocalTarget batch count (e.g., 100)
item_countINTLocalCurrent count
batch_completeBOOLLocalBatch target reached

FBD Structure

How It Works

The CTU (Counter Up) block increments on each rising edge of item_sensor. When item_count reaches batch_size, batch_complete goes TRUE. The batch_reset input clears the counter for a new batch.

Selector and Multiplexer

Choose between automatic and manual control modes.

Variables

NameTypeClassDescription
auto_modeBOOLLocalAutomatic mode selector
auto_speedREALLocalSpeed from automatic controller
manual_speedREALLocalSpeed from operator
output_speedREALLocalActual speed command sent to drive

FBD Structure

How It Works

The SEL (Selector) block chooses between two inputs based on a Boolean selector:

  • When auto_mode is FALSE → output_speed = manual_speed
  • When auto_mode is TRUE → output_speed = auto_speed

This is the FBD equivalent of an IF/ELSE statement in ST:

code
IF auto_mode THEN output_speed := auto_speed; ELSE output_speed := manual_speed; END_IF;

Best Practices for FBD

  1. Flow left to right: Keep data flow consistent. Inputs on the left, outputs on the right.
  2. Name all variables: Give every input and output variable a meaningful name in the Variables Table.
  3. Break complex logic into stages: Use intermediate variables to split long processing chains into understandable steps.
  4. Use comments: Add comment nodes to explain non-obvious logic.
  5. Group related blocks: Position blocks that work together close to each other on the canvas.
  6. Keep it flat: If your FBD diagram gets too complex, consider refactoring parts into a custom function block.

What's Next?

Explore other programming languages: Instruction List Basics for a low-level text-based approach, or return to Ladder Diagram for relay-style logic.