IMDHMIControls LibraryShop main store

How-to

Write a PLC function block for gripper open and close control

PLC programming Published 6 min read

Short answer

Hold one of two digital outputs for the commanded direction and drop both for a timed neutral pause on every reversal: that is the whole function block. Declare two BOOL outputs and a TON, run a four-state CASE that exits through a neutral state on each command edge, then map the outputs to the gripper's open and close inputs on your output module.

On this page
  1. What the digital-I/O gripper expects
  2. Declare the function block
  3. Write the state machine
  4. Instantiate, call, and map the outputs
  5. Worked example: the neutral pause in a 4 ms task
  6. Commissioning checks on the machine
  7. Questions asked

An electric gripper with a digital-I/O interface gives you two command inputs, open and close, and expects one of them held at a time, with both removed for a fixed neutral interval on every reversal, a separation its internal logic requires. The function block below delivers that from one BOOL command in structured text: FALSE holds open, TRUE holds close, and a four-state CASE with one TON inserts the neutral pause. On the ctrlX CORE it was written for, the two outputs map onto an EtherCAT output module, an XI211208 in that cabinet. Build it in this order: confirm the interface, declare the block, write the states, map the outputs, then prove the pause at the terminals.

What the digital-I/O gripper expects #

The Schunk EGP line comes as a digital-I/O version and as an IO-Link version. This block is for the digital-I/O unit with separate open and close inputs; on the IO-Link unit the commands travel the master's telegrams and a two-output block does not apply.

The inputs are commanded with levels, not pulses. While open is wanted, the open input is held TRUE and the close input FALSE; the block never generates a command pulse.

On a reversal, both inputs must be FALSE for 15 ms before the new one rises. That number belongs to the gripper, not to the PLC: the EGP's control logic requires it. It is a per-device value — SMC's LEH gripper controller manual specifies the same interval between differing input signals — so when you port this block, take the interval from the manual of the gripper in front of you and write it into PT.

Set the electrical boundary before touching code. The gripper's connector documentation gives the supply, the signal reference and the input requirement; the output module's documentation gives its switching characteristic. No state machine repairs a level mismatch or a missing reference, and the code cannot tell you which channel lands on open — trace the two wires and write it down.

Declare the function block #

FUNCTION_BLOCK FB_GripperControl
VAR_INPUT
    bCloseGripper : BOOL;      // FALSE = open, TRUE = close
END_VAR
VAR_OUTPUT
    bOutOpen  : BOOL;          // hold gripper open
    bOutClose : BOOL;          // hold gripper closed
END_VAR
VAR
    istate     : INT := 0;     // 0 open, 1 neutral before open, 2 neutral before close, 3 closed
    tDelay     : TON;          // neutral interval timer
    rTrigClose : R_TRIG;       // rising edge of the command
    rTrigOpen  : R_TRIG;       // rising edge of the inverted command = falling edge of the command
END_VAR

istate starts at 0, so the block's first execution holds the gripper open — keep that in mind when you download to a machine; the commissioning section comes back to it.

Write the state machine #

rTrigClose(CLK := bCloseGripper);
rTrigOpen(CLK := NOT bCloseGripper);

CASE istate OF
0:  // hold open
    bOutOpen  := TRUE;
    bOutClose := FALSE;
    IF rTrigClose.Q THEN
        istate := 2;
    END_IF

1:  // neutral before opening
    bOutOpen  := FALSE;
    bOutClose := FALSE;
    tDelay(IN := TRUE, PT := T#15MS);
    IF tDelay.Q THEN
        tDelay(IN := FALSE);
        istate := 0;
    END_IF

2:  // neutral before closing
    bOutOpen  := FALSE;
    bOutClose := FALSE;
    tDelay(IN := TRUE, PT := T#15MS);
    IF tDelay.Q THEN
        tDelay(IN := FALSE);
        istate := 3;
    END_IF

3:  // hold closed
    bOutOpen  := FALSE;
    bOutClose := TRUE;
    IF rTrigOpen.Q THEN
        istate := 1;
    END_IF

ELSE
    istate    := 0;
    bOutOpen  := FALSE;
    bOutClose := FALSE;
END_CASE

Both edge detectors are called ahead of the CASE on every execution. rTrigOpen clocks the inverted command, so a rising edge there is a falling edge of bCloseGripper; the two detectors see opposite transitions of one command.

Each branch writes its outputs first and tests its exit condition after. That order carries the timing: on the reversal scan the old direction is written once more, and the new direction is written one execution after tDelay.Q rises — one task cycle late at each end of the pause, which is why the pause still lands (the worked example shows the arithmetic). PT is supplied at every TON call so the block never runs on a preset left over from an earlier call.

istate Meaning bOutOpen bOutClose Leaves when
0 Hold open TRUE FALSE bCloseGripper rises
1 Neutral before opening FALSE FALSE tDelay.Q is TRUE
2 Neutral before closing FALSE FALSE tDelay.Q is TRUE
3 Hold closed FALSE TRUE bCloseGripper falls
else Recover to open FALSE FALSE next execution

The design is the gripper counterpart of latching two level inputs for pump start and stop control — one command level, one held state — with a timed dead band the pump circuit does not need. An edge that arrives while the block sits in a neutral state is not acted on; the pause finishes first.

Instantiate, call, and map the outputs #

VAR
    bCloseCommand01    : BOOL := FALSE;   // your sequence's command (illustrative name)
    bOutOpenGripper01  : BOOL;            // mapped to the output module
    bOutCloseGripper01 : BOOL;            // mapped to the output module
    fbGripper01        : FB_GripperControl;
END_VAR

fbGripper01(
    bCloseGripper := bCloseCommand01,
    bOutOpen      => bOutOpenGripper01,
    bOutClose     => bOutCloseGripper01);
  1. Open DataLayer_Realtime in the device tree and select the realtime data selectively from the ctrlX CORE.
  2. Under Source: Control, pick the EtherCAT master entry, navigate to the digital output module, tick the two output parameters for this gripper and add them to the project.
  3. Open the output module in the device tree and assign, in its parameter table, bOutOpenGripper01 and bOutCloseGripper01 to the two channels you traced to the gripper's open and close inputs.
  4. Download and run: TRUE on bCloseCommand01 closes and holds, FALSE opens and holds.

In a plain CODESYS project the last step is the same job described in mapping physical I/O channels in CODESYS V3.5.

Worked example: the neutral pause in a 4 ms task #

Inputs:

  • PT = the separation interval the EGP's control logic requires — the requirement the block is judged against.
  • Task period T = 4 ms, an illustrative input; substitute the configured period of the task that calls the block and redo the two lines of arithmetic with it.
  • Both output changes travel the same EtherCAT output update, so the bus shifts each end of the pause by the same delay and cancels out of the difference.

Rule in words: the pause at the gripper terminals is the preset rounded up to whole task cycles, ceil(PT / T) × T. The hold branch writes the old direction once more on the reversal scan, so the pause starts one cycle late; the wait branch changes istate on the scan where tDelay.Q is TRUE and the new direction is written on the next execution, so the pause ends one cycle late. The two single-cycle delays cancel, leaving only the timer's rounding.

Arithmetic:

The preset ÷ 4 ms per cycle = 3.75, so tDelay.Q rises on the timer's 4th call in the neutral state.

4 cycles × 4 ms = 16 ms from the close input falling to the open input rising.

Result: 16 ms against the required interval — a pass with 1 ms of margin, at a cost of 16 ms of dead time per reversal.

Decision: keep PT := T#15MS and the state-exit structure at any task period — the rounded-up product never falls below the preset, and when the task period equals the preset the pause is exactly the preset, the smallest this structure can deliver. Do not write the new output inside the neutral branch when tDelay.Q rises: the new command then leads by one cycle and the pause becomes (4 − 1) × 4 ms = 12 ms, under the requirement. Accept the block by measuring both command inputs together: at least the required interval from the old input falling to the new input rising.

Commissioning checks on the machine #

  1. Confirm the unit is the digital-I/O version and note the connector assignment: supply, signal reference, open input, close input.
  2. Trace both output channels to the gripper and label them; compare the output module's switching characteristic with the gripper's input requirement, both from the data sheets.
  3. Bring the EtherCAT master to run and confirm the output module's process outputs take part in the cyclic exchange — a module visible in the project is not yet a module that writes outputs.
  4. Command one reversal and read three points: the function block's output variable, the module's channel indication, and the level at the gripper input. Block moves and channel does not: mapping or exchange. Channel moves and gripper does not: cable, connector or levels. Jaws travel the wrong way: channels swapped. Both command inputs active at once: a second writer on those variables.
  5. Scope both command inputs on one reversal and measure the pause; that waveform, not the timer preset, is what the gripper sees.

The first execution writes bOutOpen := TRUE: at power-up or download the jaws are commanded open. Decide that power-up state deliberately when the gripper can be holding a part, and make the first jaw motions under the machine's approved commissioning procedure with the workspace clear.

Questions asked

Does every electric gripper need the same 15 ms pause?

No. The interval coded as T#15MS for this EGP comes from the gripper's own control logic. SMC's LEH gripper controller manual specifies the same interval between differing input signals, but that is a per-device value. Take the interval from the manual of the gripper you actually have and set PT to that number.

What happens if the command changes during the neutral pause?

The edge detectors run every execution, but only the hold states act on them, so an edge that arrives during the pause is lost. The block finishes the pause, enters the hold state it was heading for, and waits for the next edge. If your sequence can reverse the command faster than the pause, compare the command level against the held state instead of relying on edges.

Why hold the outputs instead of sending short pulses?

The two gripper inputs are commanded with maintained levels in this scheme: while opening is wanted, the open input stays TRUE and the close input FALSE. A short pulse would leave both inputs FALSE outside the pulse, which is the neutral condition the pause exists to keep brief and deliberate.

Can one function block instance drive two grippers?

No. Create one instance per gripper, each with its own pair of output variables. The instance carries istate, tDelay and both edge detectors, so two grippers on one instance would share a single state machine and a single neutral timer and could not move independently.

Is the 15 ms pause a signal that the jaws have finished moving?

No. The pause only separates opposing commands so the gripper's internal logic never sees both at once. It says nothing about jaw position or grip force; confirmation that the jaws reached the workpiece has to come from the gripper's own status signals or separate sensing, which this block does not read.

Keep reading

  • How-to

    How to migrate derived function blocks to Machine Expert

    No tool converts Control Expert derived function blocks into Machine Expert. Migrate them as a manual code port: copy the Structured Text body, rebuild every declaration and dependency by hand, resolve the two environments' differences, and test each block; blocks written in other languages are recreated. IEC 61131-3 keeps the language portable, not the project or the libraries.

    PLC programming

  • How-to

    Port a PLC-5 filter routine to ControlLogix structured text

    The filter is a first-order lag whose only state is the previous filtered value. Give every filtered signal its own instance of one filter UDT that also holds the exponent and coefficient, translate the rungs to ordered assignments in a periodic task, and preload the state on the first pass. Prove the port with a step: 63.2% of the change after one time constant, about 95% after three.

    PLC programming

  • Explainer

    How Q series PLC addressing works for I/O and analog modules

    A Q series point's address comes from the I/O assignment: 16-point hex blocks numbered in mounting order through one sequence shared by X and Y, so two 32-point input cards then two output cards land at X0000, X0020, Y0040 and Y0060. Analog values never appear in X/Y space; they sit in the module's buffer memory, read with FROM or TO through a head number equal to the Start XY with its last hex digit dropped.

    PLC programming