Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Function Blocks and Structured Text

User-Defined Function Blocks and Types

Write your own PLC function blocks, structures (UDTs), enumerations and arrays: a reusable Motor block with faults and feedback, design rules and testing, with C stand-ins.

Libraries of vendor blocks (timers, counters, PID) take you far, but the real power of a PLC project comes from your own blocks and data types. A well-made Motor block runs every motor in the plant identically; a MotorData structure keeps every motor's information together and makes the HMI easy to connect.

This lesson shows how to design a user-defined function block, define structures (also called UDTs, user-defined types) and enumerations, and test them. The code is Structured Text, read-only; a full runnable C model of the same block follows.

User-defined types

A structure groups variables of different types under one name.

TEXT
TYPE MotorCmd :
STRUCT
    Start   : BOOL;
    Stop    : BOOL;
    Reset   : BOOL;
END_STRUCT
END_TYPE

TYPE MotorSts :
STRUCT
    Running : BOOL;
    Fault   : BOOL;
    RunTime : DINT;      (* seconds *)
END_STRUCT
END_TYPE

An enumeration gives names to a set of values, much clearer than magic numbers:

TEXT
TYPE Mode : (Off, Manual, Auto); END_TYPE

An array repeats a type: Motors : ARRAY[1..8] OF MotorSts;. Together they let one loop service many devices.

Your own building blocks

Structure Group related data
Enumeration Named values
Array Many of one type
Function block Reusable behavior

Design once, instantiate many

1 / 2

Designing a Motor block

Design the interface first: what does the caller give, and what does it expect back?

Direction Name Meaning
Input Start, Stop Commands (stop priority)
Input Permissive All safe-to-run conditions
Input Feedback Auxiliary contact: motor is really running
Input Reset Acknowledge a fault
Output Run Contactor command
Output Fault Feedback did not match the command
TEXT
FUNCTION_BLOCK Motor
VAR_INPUT
    Start, Stop, Permissive, Feedback, Reset : BOOL;
    FeedbackTimeout : TIME := T#3s;
END_VAR
VAR_OUTPUT
    Run, Fault : BOOL;
END_VAR
VAR
    FbTimer : TON;
END_VAR

FbTimer(IN := Run AND NOT Feedback, PT := FeedbackTimeout);
IF FbTimer.Q THEN Fault := TRUE; END_IF;
IF Reset AND NOT FbTimer.Q THEN Fault := FALSE; END_IF;

Run := (Start OR Run) AND NOT Stop AND Permissive AND NOT Fault;
END_FUNCTION_BLOCK

Notes on the design:

  • Stop priority and permissives live inside the block, so every motor gets them.
  • The feedback timeout catches a contactor that did not pull in.
  • A fault latches until Reset, as in interlocks and motor control.

A runnable C model

C Stand-in: the Motor function block as a C struct (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The motor starts at scan 1, but its feedback does not arrive until scan 3 and vanishes at scan 5. The timeout then trips, the motor stops and the fault latches.

Many instances, one array

The big payoff: a loop over an array of instances.

C Stand-in: four motors, one block (C)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Motors 1 and 3 hold themselves on; motor 4 was stopped; motor 2 was never started.

Design rules for blocks

  1. No hidden dependencies. A block should not read global variables; pass everything through its interface.
  2. One job per block. A Motor runs motors; it does not also log data.
  3. Safe defaults. Initial values must leave the device off.
  4. Document the interface and give every input a unit or meaning.
  5. Version and test. Change a library block deliberately, because every instance changes with it.
  6. Do not call an instance twice per scan, or its internal timers and edges will run twice.

Test a new block in isolation: write a small test program that drives the inputs through a scripted sequence and check the outputs, just like the C stand-in above.

Passing data in and out

Large structures are passed with VAR_IN_OUT (by reference) so they are not copied. Use it for the command/status structures so the HMI and the block see the same data.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a structure (UDT)?

    A structure bundles several variables of different types under one name.
  2. 2. Why put stop priority and permissives inside the Motor block?

    Centralizing safety logic in the block guarantees consistency across all motors.
  3. 3. Why should an instance be called only once per scan?

    Internal state advances each call, so double calls distort timing and edges.
  4. 4. What is an enumeration good for?

    Named values make state and mode code readable.

Summary

  • Structures, enumerations and arrays organize data; function blocks package behavior.
  • Design the interface first and keep each block single-purpose with safe defaults.
  • Put shared safety logic inside the block, and call each instance once per scan.
  • Loops over arrays of instances service many identical devices with one block.

Next: PID Control Basics