S7-300 Analog Input Filtering in STEP 7 V5.x: A Complete Guide

David Krause17 min read
S7-300SiemensTutorial / How-to
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

1. Problem Definition: Erratic Analog Readings on a Siemens S7-300

A displacement transducer wired into a Siemens SIMATIC S7-300 SM331 analog input module frequently reports a value that is theoretically stationary but wanders ±0.5 LSB to ±3 LSB. A typical field report is a constant target returning 20.45 to 20.67 in the engineering units register, while the physical object has not moved. On a 16-bit SM331 channel configured for ±10 V the least significant bit is 305.18 µV, and on a 4-20 mA input it is 0.305 µA - both small enough that electrical interference, sensor thermal noise, and quantization all contribute to a visible jitter at the operator panel.

The raw peripheral value (PEW) feeds scaling (commonly FC105 "SCALE"), which in turn feeds a closed-loop controller, an HMI tag, or a data archive. Without filtering, every LSB jump propagates downstream. The correct fix is to filter the analog value - either in the module's hardware by extending its integration time, in firmware by activating the channel smoothing parameter, or in user software with a low-pass filter implemented in SCL, STL, or LAD. This guide covers all three methods, the trade-offs, and the diagnostics needed to confirm the filter is actually reducing noise rather than masking a real signal problem.

Engineering rule of thumb: A noise band wider than 2× the mechanical resolution of the measurement is filtered - a band narrower than that is a sensor or wiring problem that software filtering cannot fix. Confirm the wiring, shield grounding, and sensor supply stability before adding filters.

2. Noise Sources in Industrial Analog Measurements

Before choosing a filter, identify the dominant noise source. Different filters solve different problems.

Noise Source Frequency / Characteristic Best Mitigation
50 Hz / 60 Hz mains pickup Sinusoidal, line-locked Integration time matched to 1/50 s or 1/60 s
Common-mode voltage DC to several kHz Shielded twisted pair, equipotential bonding
VFD / inverter switching 2 kHz to 20 kHz PWM Ferrite on cable, physical separation from VFD cables
Sensor thermal (Johnson) noise White, broadband Lower bandwidth, PT1 filter
Quantization (LSB) noise ±½ LSB uniform Dithering, averaging
Mechanical vibration 1 Hz to 500 Hz Mechanical isolation, low-pass filter
Ground-loop offset DC and line frequency Single-point ground, isolator

The integration time parameter on the SM331 module is a hardware-side moving average performed by the analog front end and is the first, cheapest, and most effective filter for line-frequency noise. Software filters are added on top when the integration time alone is insufficient or when the application requires a variable time constant.

3. Prerequisites

Before you configure filtering, the following must be in place:

  • STEP 7 V5.5 or later with Service Pack installed. The procedure below is valid for STEP 7 V5.4 SP5 and STEP 7 V5.5. The S7-300 product line is described on the official Siemens SIMATIC S7-300 product page (SIMATIC S7-300) and the SM331 module data is in the S7-300 Module Data manual on the Siemens Industry Online Support portal.
  • An S7-300 PLC with an SM331 analog input module. Order numbers relevant to filtering are 6ES7331-1KF02-0AB0, 6ES7331-7KF02-0AB0, 6ES7331-7NF00-0AB0, 6ES7331-7NF10-0AB0, 6ES7331-7PF01-0AB0, 6ES7331-7PF11-0AB0, and 6ES7331-7KB02-0AB0. Each exposes a different combination of integration times and channel smoothing.
  • Sensor wired in three-wire (for active 4-wire sensors) or four-wire (for passive two-wire sensors) configuration with shielded cable and shield grounded at one end only.
  • HW Config open in STEP 7 with the station already compiled and downloaded once successfully.
  • For SCL-based filters: SCL compiler installed (standard with STEP 7 V5.x Professional).
Important: Hardware filter parameter changes require a STOP → download → RUN cycle on the S7-300. The new parameters do not take effect online without reinitializing the module. For non-disruptive changes, change the integration time on a planned maintenance window or use SFC55 "WR_PARM" to re-parameterize the module from the user program at runtime.

4. Configuring the Hardware Filter in HW Config (STEP 7 V5.x)

The SM331 exposes two related filter parameters: integration time (sometimes called the A/D conversion time) and smoothing (a software-style running average applied inside the module firmware). Integration time is the first-line hardware filter and is always present; smoothing is available on specific module variants and on specific channels only.

  1. Open SIMATIC Manager and double-click the S7-300 station to launch HW Config.
  2. Locate the SM331 in the rack. Right-click the module and select Object Properties.
  3. Switch to the Inputs tab. Each of the 2 or 8 channels is listed.
  4. For each channel, set:
    • Measurement type (e.g. Voltage, Current 4-wire, RTD) - must match the wiring.
    • Measurement range (e.g. 0-10 V, 4-20 mA, Pt100 standard).
    • Integration time - selectable values are 2.5 ms, 16.67 ms, 20 ms, and 100 ms. Select 20 ms at 50 Hz mains or 16.67 ms at 60 Hz mains to reject one full mains cycle per conversion. Use 100 ms for the lowest noise (also the slowest update). The 2.5 ms setting is the default and is rarely correct for noisy industrial environments.
    • Noise suppression - on modules that expose this separately, set to 50 Hz or 60 Hz. This is sometimes linked to the integration time selection.
    • Smoothing (when available) - a separate parameter that applies a finite-impulse-response average on the channel. Typical values are None, Weak, Medium, Strong. Strong corresponds to approximately 32-sample averaging in firmware.
  5. Click OK to close the Properties dialog. The module is marked as "changed" in HW Config.
  6. Select Station > Save and Compile.
  7. Download the hardware configuration to the PLC. If the PLC is in RUN, the download will prompt for STOP. The new parameters are written to the module's parameter assignment at the next STOP→RUN transition.

The peripheral word (PEW) read from the module will now reflect the filtered value. To verify, place the PLC in RUN, open a VAT or watch table on the relevant PEW, and read several hundred values with the target stationary. The standard deviation should drop by a factor of 2 to 5 when moving from 2.5 ms to 20 ms integration time.

5. SM331 Parameter Reference

The integration time and smoothing capability differ by module variant. Use the table below to confirm the order number on the front of the module against the available filter settings before configuring.

Order Number Channels Resolution Integration Times Smoothing Use Case
6ES7331-7KB02-0AB0 2 AI 9/12/13/14-bit 2.5 / 16.67 / 20 / 100 ms No Cost-sensitive, low-resolution
6ES7331-1KF02-0AB0 8 AI 13-bit 2.5 / 16.67 / 20 / 100 ms No Standard voltage/current
6ES7331-7KF02-0AB0 8 AI 14/15-bit 2.5 / 16.67 / 20 / 100 ms Yes (per channel) Higher precision + smoothing
6ES7331-7NF00-0AB0 8 AI 16-bit 2.5 / 16.67 / 20 / 100 ms Yes (per channel) High precision, generic
6ES7331-7NF10-0AB0 8 AI 16-bit 2.5 / 16.67 / 20 / 100 ms Yes (per channel) Replacement for 7NF00
6ES7331-7PF01-0AB0 8 AI 16-bit RTD/TC 2.5 / 16.67 / 20 / 100 ms Yes (per channel) Thermocouple / RTD
6ES7331-7PF11-0AB0 8 AI 16-bit RTD/TC 2.5 / 16.67 / 20 / 100 ms Yes (per channel) Replacement for 7PF01

Cross-reference these order numbers against the actual MLFB printed on the front of the module. The 6ES7331-7xxx variants share the same mechanical footprint and pinout, but the firmware inside the module determines whether the smoothing parameter is honored. If the smoothing field is greyed out in HW Config, the order number in the rack does not match the physical module - close HW Config, re-read the station, and verify.

6. Software Low-Pass Filter - PT1 (Exponential) in SCL

A first-order lag filter - also called a PT1 or exponential moving average - approximates an RC low-pass filter in software. It is the cheapest software filter in CPU cycles, has no internal buffer, and is ideal when the input is a slowly varying process variable such as a displacement, level, or temperature.

Transfer function: y[n] = y[n-1] + α × (x[n] - y[n-1]), where α = TA / (TA + T1), TA is the sampling period in seconds and T1 is the filter time constant in seconds. A larger T1 means more smoothing but more phase lag.

Create an SCL source file in STEP 7, add the following block, and compile it into your S7 program:

FUNCTION_BLOCK FB_PT1_Filter
// PT1 (exponential) low-pass filter
VAR_INPUT
    X_IN  : REAL;    // Raw input (engineering units or raw counts)
    TA    : REAL;    // Sampling time in seconds, e.g. 0.1
    T1    : REAL;    // Filter time constant in seconds, e.g. 1.0
    RESET : BOOL;    // TRUE forces X_OUT := X_IN
END_VAR
VAR_OUTPUT
    X_OUT : REAL;    // Filtered output
END_VAR
VAR
    ALPHA : REAL;
END_VAR
BEGIN
    IF RESET THEN
        X_OUT := X_IN;
    ELSE
        IF T1 < 0.0001 THEN
            X_OUT := X_IN;            // avoid divide-by-zero
        ELSE
            ALPHA := TA / (TA + T1);  // recursive coefficient
            X_OUT := X_OUT + ALPHA * (X_IN - X_OUT);
        END_IF;
    END_IF;
END_FUNCTION_BLOCK

Call the block in OB35 (the cyclic interrupt OB, default 100 ms) so TA is fixed. Pass PEW converted to REAL through FC105 "SCALE" to engineering units before feeding it to the filter, and store the result in a DB for HMI and control consumption.

For a displacement sensor updating at 100 ms with a desired 1.0 s time constant: TA = 0.1, T1 = 1.0, ALPHA = 0.0909. A step input from 20.45 to 20.67 will reach 63% of the step (i.e. 20.59) in 1.0 s and 95% in 3.0 s.

7. Software Low-Pass Filter - Moving Average in SCL

A moving average (also called a boxcar or FIR filter) provides a flatter passband than PT1 at the cost of N samples of memory and one division per output. It is the right filter when the dominant noise is broadband white noise rather than line-frequency interference, and when the engineer wants a strictly defined noise bandwidth of f_s / N.

FUNCTION_BLOCK FB_MovingAverage
// Sliding-window moving average, window size N
VAR_INPUT
    X_IN  : REAL;
    N     : INT;     // Window size, 1..50
    RESET : BOOL;
END_VAR
VAR_OUTPUT
    X_OUT : REAL;
END_VAR
VAR
    Buffer: ARRAY[1..50] OF REAL;
    Sum   : REAL;
    Head  : INT;
    i     : INT;
END_VAR
BEGIN
    IF RESET OR (N < 1) OR (N > 50) THEN
        FOR i := 1 TO 50 DO Buffer[i] := 0.0; END_FOR;
        Sum := 0.0;
        Head := 1;
        X_OUT := X_IN;
    ELSE
        Sum := Sum - Buffer[Head] + X_IN;
        Buffer[Head] := X_IN;
        Head := Head MOD N + 1;
        X_OUT := Sum / INT_TO_REAL(N);
    END_IF;
END_FUNCTION_BLOCK

For a 10-sample window at 100 ms update (TA = 0.1 s), the equivalent noise bandwidth is 0.1 / 10 = 0.01 Hz for white noise. The step response settles in 10 × TA = 1.0 s with no overshoot. Group delay is fixed at (N-1)/2 × TA = 0.45 s for N=10.

8. Software Low-Pass Filter - Median Filter in SCL

A median filter replaces each sample with the middle value of a sorted window. It is the most effective filter for rejecting single-sample spikes (spark-over from a relay coil, ESD, intermittent open circuit) while preserving sharp edges. The cost is the sort operation, which scales with N log N but is acceptable for window sizes of 3, 5, 7, or 9 on an S7-300.

FUNCTION_BLOCK FB_MedianFilter
// Median filter, window size N (odd, 3..9 recommended)
VAR_INPUT
    X_IN  : REAL;
    N     : INT;     // Window size, 1..9
    RESET : BOOL;
END_VAR
VAR_OUTPUT
    X_OUT : REAL;
END_VAR
VAR
    Buffer: ARRAY[1..9] OF REAL;
    Sorted: ARRAY[1..9] OF REAL;
    Head  : INT;
    i, j  : INT;
    Tmp   : REAL;
    Mid   : INT;
END_VAR
BEGIN
    IF RESET OR (N < 1) OR (N > 9) THEN
        FOR i := 1 TO 9 DO Buffer[i] := 0.0; END_FOR;
        Head := 1;
        X_OUT := X_IN;
    ELSE
        Buffer[Head] := X_IN;
        Head := Head MOD N + 1;
        Mid := (N + 1) / 2;
        FOR i := 1 TO N DO Sorted[i] := Buffer[i]; END_FOR;
        // Simple bubble sort, fine for small N
        FOR i := 1 TO N-1 DO
            FOR j := 1 TO N-i DO
                IF Sorted[j] > Sorted[j+1] THEN
                    Tmp := Sorted[j];
                    Sorted[j] := Sorted[j+1];
                    Sorted[j+1] := Tmp;
                END_IF;
            END_FOR;
        END_FOR;
        X_OUT := Sorted[Mid];
    END_IF;
END_FUNCTION_BLOCK

For a 5-sample median at 100 ms update, a single-sample spike is fully rejected, two-sample spikes pass through, and steady-state values are passed unchanged. This filter is the standard choice for reed-switch debounce, encoder noise, and noisy switches - it is less appropriate for continuous process variables where a moving average gives a smoother output.

9. Using FB41 CONT_C TM_LAG as an Inline Filter

The standard PID control block FB41 (CONT_C) - from the STEP 7 V5.x Standard Library, "PID Control Blocks" - has a process-variable smoothing input named TM_LAG (lag time constant, in seconds). Setting TM_LAG > 0 applies a PT1 filter identical in shape to the FB_PT1_Filter above, but reuses code you may already have loaded. To use it purely as a filter without controlling anything, set GAIN = 1, TI = 99999 (effectively no integral), TD = 0 (no derivative), and wire your scaled PV into SP_INT and PV_IN. The output LMN will be the filtered value, scaled to the 0-100% range of FB41.

This is a quick solution for one or two channels where loading a custom SCL block is not desired, but it has two practical drawbacks: (1) it consumes a 36-byte instance DB and the FB41 itself is large; (2) the input range is fixed to 0-100% engineering, requiring extra scaling. For high-channel-count or high-performance filtering, use the custom SCL blocks above.

10. Filter Selection Matrix

Filter Type Memory CPU / Sample Group Delay Spike Rejection Passband Ripple Best For
Hardware integration time (HW) None (on PLC) None ½ × TA None None Line-frequency rejection
Module firmware smoothing (HW) None (on PLC) None N/2 × TA Partial None General purpose on supported modules
PT1 / exponential (SW) 1 REAL ~10 µs ~T1 None None Slow process values
Moving average (SW) N REAL ~5 µs + 1 div (N-1)/2 × TA Partial None White noise, level, flow
Median (SW) N REAL + scratch ~50 µs for N=5 (N-1)/2 × TA Excellent None Spike-laden signals, switches
Cascaded PT1+Median (SW) 2N REAL Sum of both Combined Excellent None Field-proven all-purpose

For the displacement-sensor scenario in section 1, the recommended cascade is: module integration time = 20 ms → firmware smoothing = Medium → software PT1 with T1 = 0.5 s. This combination reduces 50 Hz line pickup at the module, attenuates broadband quantization noise in the firmware, and produces a clean value for the controller at the application layer.

11. Verification and Diagnostics

After applying a filter, verify the result with a quantitative test rather than visual inspection of the HMI. The standard procedure is:

  1. Open a Watch Table (VAT) in STEP 7 and add the raw PEW and the filtered output (DBW or DBD in REAL form).
  2. Force the PLC into RUN with the process at a known steady state.
  3. Read 500 to 1000 samples into the VAT by setting a trigger on the value and using Monitor/Modify with a continuous update, or use the S7-PLCSIM or HMI trend for a long capture.
  4. Calculate standard deviation and peak-to-peak in Excel, or in the PLC itself with a small block that maintains running min, max, and sum-of-squares.
  5. Apply a step change (move the target by a known amount) and capture the step response. Time the rise from 10% to 90% and confirm it matches the filter time constant you set.

A correctly tuned filter on a displacement sensor should produce a peak-to-peak of less than 1 LSB of the engineering-unit display and a step-response rise time of 3 × T1 (for PT1) or N × TA (for moving average). If the standard deviation drops but the step response is too slow, reduce T1 or N. If the step response is fine but the noise is still present, increase T1 or N or add the second filter stage.

Common mistake: Filtering the same value twice with independent PT1 blocks in series and the same time constant does not give a 2× T1 filter - it gives a second-order filter with overshoot and ringing. If two stages are used, the second stage time constant should be roughly ½ of the first.

12. Troubleshooting Matrix

Symptom Likely Cause Fix
PEW value jumps by 0-32767 occasionally Open thermocouple, broken wire, or out-of-range Check wiring, sensor supply, and channel diagnostics in HW Config
PEW stable but PEW → engineering value oscillates Sub-LSB jitter scaled up by FC105 Apply module smoothing or PT1 before FC105
Smoothing field greyed out in HW Config Module is 6ES7331-1KF02 or 7KB02 (no smoothing) Replace module with 7KF02/7NF10/7PF11 or filter in software
Filter has no effect SCL block is in OB1 with non-cyclic call; TA passed as 0 Move call to OB35, set TA = OB35 period in seconds
Filtered value lags process by 5+ seconds T1 or N too large, or OB35 period 1000 ms Reduce OB35 period to 100 ms and reduce T1 to 0.5 s
Filter output stuck at one value RESET input held TRUE; or ALPHA = 0 from bad T1 Reset RESET bit; check T1 > 0
Filter introduces 50 Hz ripple Integration time set to 2.5 ms; smoothing off Set integration time to 20 ms at 50 Hz mains
CPU cycle time increases by 5+ ms Filter block called in OB1 with very large N or sort loop Move to OB35; cap moving-average N at 50; cap median N at 9
Value drifts up over time DC ground loop; or sensor warming up Check shield termination; allow 10-min warm-up

13. Related Methods on Other Siemens Platforms

The same filtering techniques apply to S7-1200 and S7-1500, with different block names. On the S7-1500, the Siemens Industry Online Support portal distributes the "LGF" (Library of General Functions) that contains FB "Filter_PT1" and FB "Filter_PT2" with integrated runtime diagnostics. On the S7-1200, the same PT1 / moving average / median code can be written in SCL and called from a cyclic OB. For S7-1500, the analog input module (e.g. 6ES7531-7NF10-0AB0) also supports per-channel smoothing and integration time in its module parameters, identical in concept to the S7-300.

For background on the analog filter theory referenced throughout this guide - including the noise-bandwidth trade-off and the Bode plot of the PT1 transfer function - the Analog Devices Basic Linear Design handbook, Chapter 8 is the standard reference. An overview of analog filter definitions and topologies is available from ScienceDirect Topics - Analog Filter.

Which SM331 integration time should I choose for a 50 Hz mains environment?

Set the integration time to 20 ms. This makes one A/D conversion span exactly one full 50 Hz cycle, and the module's integrator rejects the line-frequency component by averaging. For 60 Hz mains, choose 16.67 ms. Setting 2.5 ms (the default) leaves 50/60 Hz pickup fully visible at the PEW.

My SM331 6ES7331-1KF02 module does not show a smoothing parameter in HW Config - is the module defective?

No. The 1KF02 variant (8 AI, 13-bit) does not support firmware channel smoothing. It supports integration time selection only. To obtain smoothing, either replace the module with a 6ES7331-7KF02-0AB0 (8 AI, 14/15-bit with smoothing) or 6ES7331-7NF10-0AB0 (8 AI, 16-bit with smoothing), or implement the filter in SCL on the existing module.

Can I change the integration time online without stopping the PLC?

On the S7-300, hardware-parameter changes in HW Config require a STOP → download → RUN cycle. To change parameters at runtime without a stop, use SFC55 "WR_PARM" to write the parameter assignment record (record 0 for SM331) to the module, then call SFC56 "WR_DPARM" to distribute the new parameters. Note that the module briefly goes through re-initialization; PV may glitch for 1-2 OB1 cycles.

What is the recommended OB for calling the SCL filter block?

OB35 (cyclic interrupt), with a configured period of 100 ms by default. OB35 guarantees a fixed TA, which the PT1 block needs for the ALPHA calculation. Calling the filter in OB1 produces a variable TA tied to the OB1 cycle time, which makes the filter time constant drift with the CPU load.

Is a moving average with N=1 different from no filter?

Effectively no, modulo integer division. With N=1, the output equals the most recent input sample, and the filter adds a single subtraction and addition per cycle. For N=1 the sum variable accumulates noise; if you must call the block, set N=2 or higher. Otherwise, gate the block call with a N > 1 check and bypass the instance DB entirely.

How do I tune the PT1 time constant T1 for a displacement sensor with 0.22 mm peak-to-peak noise?

Start with T1 equal to 2-3 times the dominant noise period. If the noise is white, start with T1 = 1.0 s and an OB35 period of 100 ms. Capture 1000 samples before and after; if the peak-to-peak is still larger than 1 LSB of the engineering display, double T1. If the step response is too slow for the controller bandwidth, halve T1 and add a 3-sample median in front of the PT1 to reject single-sample spikes without slowing the response.

Can I reuse the standard PID block FB41 as a filter?

Yes. Set GAIN = 1, TI = 99999 (disables integral), TD = 0 (disables derivative), and set TM_LAG to the desired filter time constant in seconds. Wire the scaled PV to PV_IN. The output LMN is the filtered value in 0-100% engineering range. This is acceptable for one or two channels but inefficient for many channels because of FB41's code and DB footprint.

Back to blog