09/19/2025
How-to

PLC Function Block for Schunk EGP gripper via digital I/O

In this short HowTo article a function block is provided to control a Schunk EGP electric gripper from your PLC program.

Requirements

  • Electric Gripper with digital IO

  • ctrlX CORE

  • Digital Output Module (for example: XI211208)

  • Apps:

    • Ethercat Master

    • PLC

Connection to the gripper

The electric gripper, that is used here, has digital inputs for the close and open commands. Another version of the EGP gripper uses IO-Link, which would result in a different workflow than described here.

Connection Plug

Once you have the EtherCAT configuration of your digital output module running (won't be described here), connect the gripper cable to two physical outputs.

PLC function block

Add a funktion block to your PLC project and name it "FB_GripperControl". Inside the variable section add:

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;    // state machine for FB
    tDelay          : TON;         // Timer for 15 ms delay
    rTrigClose      : R_TRIG;      // rising edge detector for bCloseGripper
    rTrigOpen       : R_TRIG;      // falling edge detector for bCloseGripper
END_VAR

Then insert the following code to your FB:

// 1) rising/falling edge detector
rTrigClose(CLK := bCloseGripper);
rTrigOpen (CLK := NOT bCloseGripper);

// 2) state machine
CASE istate OF
	
	0:  // hold open (default)
		bOutOpen  := TRUE;
		bOutClose := FALSE;
		IF rTrigClose.Q THEN
			// command: open→close → wait before close
			istate := 2; 
			tDelay(IN := TRUE, PT := T#15MS);
		END_IF
	
	1:  // wait open – 15 ms pause before open
		bOutOpen  := FALSE;
		bOutClose := FALSE;
		tDelay(IN := TRUE);
		IF tDelay.Q THEN
			tDelay(IN := FALSE);
			istate    := 0;   // go to open state
		END_IF
	
	2:  // wait close – 15 ms pause before close
		bOutOpen  := FALSE;
		bOutClose := FALSE;
		tDelay(IN := TRUE);
		IF tDelay.Q THEN
			tDelay(IN := FALSE);
			istate    := 3;   // in Geschlossen-Status
		END_IF
	
	3:  // hold close
		bOutOpen  := FALSE;
		bOutClose := TRUE;
		IF rTrigOpen.Q THEN
			// Command: close→open → wait before close
			istate := 1;
			tDelay(IN := TRUE, PT := T#15MS);
		END_IF

	ELSE
		// undefined state → gripper open
		istate    := 0;
		bOutOpen  := FALSE;
		bOutClose := FALSE;

END_CASE
Flowchart of FB state machine

The state machine inside the FB starts in "Hold open", so that the gripper is open per default. In this state the gripper will receive a signal on the open-input and no signal on the close-input.

Then, when a rising edge on the input variable "bCloseGripper" is detected, the FB will enter a 15 ms pause, which is required by the internal logic of the gripper. In this state both digital inputs on the gripper wont get a signal.

Digital input signals

After the pause the FB goes to state "Hold close", where a signal is sent to the close-input and no signal to the open-input.

Then the next falling edge on the input variable "bCloseGripper" will open the gripper again and keep it open.

After the FB is defined, go to your program and create an instance of it and two boolean output variables "bOutCloseGripper01" and "bOutOpenGripper01". If you have several electric grippers connected, create more instances of the FB and more output variables.

VAR
	// Gripper Outputs (mapped to digital output module) 
	bOutCloseGripper01	: BOOL;
	bOutOpenGripper01	: BOOL;
	
	// Gripper FB
	fbGripper01			: FB_GripperControl;
END_VAR

Then call the FB inside your program like this:

fbGripper01(bCloseGripper	:= 	,
			bOutClose 		=> 	bOutCloseGripper01, 
			bOutOpen		=> 	bOutOpenGripper01);

Finally map the two boolean output variables in your program to the physical outputs on your digital output module. Therefore go down to DataLayer_Realtime in the device tree on the left and select realtime data selectively from ctrlX CORE.

EtherCAT realtime data

In the pop up window under "Source: Control" on the right side, select "ethercat_master..." (1). Then navigate to the digital output module and select the parameters (2) according to the image below. If you have several grippers, select the parameters for all of them. Then add the parameters to your project with the button in the middle (3) and close the window (4).

Select realtime parameters

Then map the EtherCAT parameters to the variables in your program. Therefore open the output module in the device tree and for each of the parameters double-click on the empty field in the variable column and then click the tree dots, that will appear. There you can select the variables, that should be mapped to the physical ports on your output module. Repeat this for the other grippers, if you have more than one.

Parameter mapping

Now you can download the program to your control and run it. If you set the input variable "bCloseGripper" of your FB to TRUE, the gripper will close and stay closed. If you set the input FALSE again, the gripper will open and stay open.

Good luck ;)

Types
How-to
Products
Controls
PLC
Markets
Assembly Lines
Logistic
Packaging
Robotics
Semicon & Electronics

Latest published/updated articles