banner
Home News

A Weekly Programming Example: An Optimization Solution Based on a First-Order Lag Filter

A Weekly Programming Example: An Optimization Solution Based on a First-Order Lag Filter

Aug 18, 2025

 • In industrial automation, sensor signals are often affected by noise and interference, resulting in degraded system performance. Filtering algorithms are effective means of addressing these interferences, and the first-order lag filter is a commonly used signal smoothing technique. This article will explain how to implement and optimize a first-order lag filter in a PLC program to improve system signal stability, and will provide detailed explanations based on practical application scenarios. Program Code Below is the complete PLC program code (based on the ST language of the CodeSys platform):

1 FUNCTION_BLOCK PM_TimedFirstOrderLagFilter

2 VAR_INPUT

3 InputValue: REAL; // Input value for filtering

4 Alpha: REAL := 0.5; // Filter coefficient, value range 0-1

5 SampleInterval: INT; // Sampling interval, in milliseconds

6 END_VAR

7 VAR_OUTPUT

8 FilteredValue: REAL; // Filtered output value

9 END_VAR

10 VAR

11 previous_value: REAL; // Previous filtered value

12 time_interval : TIME;                  // Time interval in milliseconds

13 sampling_timer : TON;                   // Timer that controls the sampling interval

14 trigger_flag : BOOL;                  // Used to mark whether a new sample should be processed

15 END_VAR

16 // Limit the filter coefficient to the range between 0 and 1

17 Alpha := LIMIT(0, Alpha, 1);

18 // Convert the sampling interval from milliseconds to time format

19 time_interval := INT_TO_TIME(SampleInterval);

20 // Control the sampling timer

21 sampling_timer(IN := NOT trigger_flag, PT := time_interval, Q => trigger_flag);

22 IF trigger_flag THEN

23  // Apply the first-order lag filter formula

24 FilteredValue := (1 - Alpha) * previous_value + Alpha * InputValue;

25 previous_value := FilteredValue; // Update the previous filtered value

26 END_IF;

 

• Code Analysis: Input and Output Variables:

InputValue: The input signal to be filtered.

Alpha: The filter coefficient, which determines the weighting ratio between the new value and the previous value. A larger value increases the influence of the new value.

SampleInterval: The sampling interval, used to control the sampling frequency of the signal. Internal variable: previous_value: Saves the result of the previous filtering.

time_interval: Converts the sampling interval to a TIME type.

sampling_timer: A timer, used to control the sampling frequency.

trigger_flag: Indicates whether new input should be processed. Filtering Logic: Uses Alpha to perform a weighted average of the input values to smooth the signal. The filtered value is stored in previous_value for the next calculation.

 

• Optimization Key Points:

Limiting the Alpha Range: To prevent users from entering invalid values, use LIMIT(0, Alpha, 1) to limit the Alpha value range to between 0 and 1.

 

Sampling Interval Control: Use a timer to control the sampling interval, ensuring that the system samples within the specified time interval and avoiding the additional burden of excessive sampling.

 

Dynamic Adjustment of Filter Coefficients: Dynamically adjust the Alpha value based on actual system needs to adapt to different signal characteristics.

 

• Application Scenarios: First-order lag filters are widely used in situations where real-time signal smoothing is required, such as:

Temperature Control Systems: When the temperature data collected by a temperature sensor fluctuates significantly, a filter can be used to smooth it and obtain a more stable temperature value.

 

Pressure Monitoring Systems: Pressure data read by a pressure sensor often exhibits instantaneous fluctuations. Filters can effectively reduce this interference and maintain a stable pressure value.

 

Speed Control Systems: The motor speed may be affected by load fluctuations. Filters help smooth the speed data and ensure stable operation of the control system.

 

• Further Thinking: In addition to first-order lag filters, a variety of filtering techniques can be used in industrial control systems. For example, second-order lag filters can provide more precise signal smoothing, or Kalman filters can be used to handle complex dynamic systems. Different filtering algorithms can be selected for different application scenarios, or multiple algorithms can be combined for composite filtering to achieve optimal system performance.

 

• Summary: This article has demonstrated how to implement and optimize a first-order lag filter in a PLC program. This program effectively smooths input signals and reduces noise and interference, making it of great significance for industrial automation systems. We hope that readers will be able to flexibly adjust filtering parameters based on actual application needs to improve system stability and accuracy.

Leave A Message

Leave A Message
If you are interested in our products and want to know more details,please leave a message here,we will reply you as soon as we can.
Submit
Contact Us:justwellautomation@qq.com

Home

Products

whatsApp

contact