Resources for Codesys

 

Codesys home page

 

Videos

Video tutorials on Youtube (in Norwegian).

 

Function blocks

The following function blocks (*.export files) can be imported into a Codesys project with the meny Project -> Import (in Codesys).

The function blocks are generated in CODESYS V3.5 SP21 Patch 4.


Measurement signal scaling

MEAS_SIGNAL_SCALING.export


   

Inputs:

  • m is measurement signal value (e.g. volt or milliampere).
  • (m_min, p_min) and (m_max, p_max) are two given measurement points.

Output:

  • p is process value (e.g. celsius).

Function:

p is calculated with the following linear formula:

p := (a * m) + b;

where

a := (p_max - p_min) / (m_max - m_min);

b := p_min - (a * m_min);

 


Control signal scaling

CONTROL_SIGNAL_SCALING.export


   

Inputs:

  • u_con is the controller output (in unit e.g. %).
  • u_con_min is the minimum value of u_con (e.g. 0 %).
  • u_con_max is the maximum value of u_con (e.g. 100 %).
  • u_act_min is the minimum value of control signal applied to the actuator (e.g. 0 mA), corresponding to u_con_min.
  • u_act_max is the maximum value of control signal applied to the actuator (e.g. 20 mA), corresponding to u_con_max.

Output:

  • u_act_min is the minimum value of control signal applied to the actuator (in unit e.g. mA).

Function:

u_act is calculated with the following linear formula:

u_act := (a * u_con) + b;

where

a := (u_act_max - u_act_max) / (u_con_max - u_con_min);

b := u_act_min - (a * u_con_min);

 


EWMA filter

(EWMA = Exponentially Weighed Moving Average)

EWMA_FILTER.export

  

Inputs:

  • y_nonfilt is nonfiltered (raw; noisy) value.
  • tf [s] is filter time constant.
  • ts [s] is time step, or sampling interval.

Output:

  • y_filt is filtered value.

Function:

y_filt is calculated with

y_filt := (1 - a) * y_filt_prev + a * y_nonfilt;

where a is the filter constant:

a := 1/nf;

where nf is the filter length:

nf := (tf/ts) + 1;

At the first iteration, y_filt_prev is set equal to y_nonfilt, which makes the initial value of y_filt become equal to y_nonfilt.

 


Analog input: From WORD to REAL

AI_FROM_WORD_TO_REAL.export

Inputs:

  • ai_word is the analog input signal of data type WORD from the analog-digital converter on the PLC.
  • ai_word_as_real_min is the minimum value of ai_word expressed as a REAL type (e.g. 0.0 (REAL) as on Wago CC100 PLCs).
  • ai_word_as_real_max is the maximum value of ai_word expressed as a REAL type (e.g. 32760.0 (REAL) as on Wago CC100 PLCs).
  • ai_ real_min is the minimum value of the converted value of data type REAL (e.g. 0.0 mA or 0.0 V).
  • ai_ real_max is the maximum value of the converted value of data type REAL (e.g. 20.0 mA or 10 V).

Output:

  • ai_real is the converted value of data type REAL (e.g. in unit mA or V).

Function:

This function block implements the linear conversion of an analog input value of data type WORD to a corresponding value of data type REAL (e.g. from 16380 WORD to 5.0 REAL (Volt) as on Wago CC100 PLCs).

The mathematical function of the conversion is the following linear function:

ai_real := (a * ai_word_as_real) + b;

where

a := (ai_real_max - ai_real_min) / (ai_word_as_real_max - ai_word_as_real_min);

b := ai_real_min - (a * ai_word_as_real_min);

This function uses the built-in function WORD_TO_REAL to convert a WORD value into a REAL value (of the same numerical value). This is necessary since the digital-analog converter generates a WORD value (not a REAL value), and the pertinent calculations works on REAL values (not WORD values).

Resources:

 


Analog output: From REAL to WORD

AO_FROM_REAL_TO_WORD.export

Inputs:

  • ao_real is the REAL value (e.g. 5.0 in unit V) to be converted to a WORD value to be applied to the digital-analog converter of the PLC.
  • ao_real_min is the minimum value of ao_real (e.g. 0.0 (REAL) V).
  • ao_real_max is the maximum value of ao_real (e.g. 10.0 (REAL) V).
  • ao_word_as_real_min is the minimum value of ao_word_as_real (e.g. 0 (WORD) on Wago CC100 PLCs).
  • ao_word_as_real_max is the maximum value of ao_word_as_real (e.g. 32767 (WORD) on Wago CC100 PLCs).

Output:

  • ao_real_as_word is the value of data type WORD to be applied to the digital-analog converter of the PLC.

Function:

This function block implements the linear conversion of a REAL value in unit e.g. V or mA to the corresponding value of data type WORD (e.g. from 5.0 V to 16384 WORD as on Wago CC100 PLCs).

The mathematical function of the conversion is following linear function:

ao_word_as_real := (a * ao_real) + b;

where

a := (ao_word_as_real_max - ao_word_as_real_min) / (ao_real_max - ao_real_min);

b := ao_word_as_real_min - (a * ao_real_min);

This function uses the built-in function REAL_TO_WORD to convert a REAL value into a WORD value (of the same numerical value). This is necessary since the digital-analog converter operates on basis of a WORD value (not on a REAL value).

Resources:

 


Time constant system

This function block implements a time constant system, which can be used in e.g. a process simulator. The output is updated continuously.

TIMECONSTANT_SYSTEM.export

Inputs:

  • y_init is the initial value of the output y.
  • u is the input value.
  • K is the gain.
  • tc [s] is the time constant.
  • ts [s] is time step, or sampling interval.

Output:

  • y is output value.

Function:

The function block implements a numerical calculation of output y based on Euler Forward algorithm for solving the differential equation comprising the model of the system.

The model (differential equation) is

y' = (K*u - y) / tc

The Euler Forward simulation algorithm implemented in the function block (initialization code not shown here):

y := y_next;

dy_dt := (K*u - y)/tc;

y_next := y + ts * dy_dt;


Time delay

This function block implements a time delay, which can be used in e.g. a process simulator. The output is updated continuously.

TIME_DELAY.export

Inputs:

  • y_nondelayed is the signal to be time delayed.
  • delay_array_init is the initial value of y_delayed, i.e. its value before the time delay has elapsed.
  • t_delay [s] is the time delay.
  • ts [s] is time step, or sampling interval.

Output:

  • y_delayed is the time delayed signal.

Function:

The delay is implemented with an array. At each point of time:

1.     y_delayed is the value of the last element of the array.

2.     Each of the elements of the array are one step to the right.

3.     y_nondelayed is entered into the first element of the array.


Updated 9 Nov 2026 by Finn Aakre Haugen (finn@techteach.no).