Overview
Many industrial sensors present their output in an inverted manner when paired with a 4–20 mA loop resistor and 0–10 V analog input. A common case is the VEGA VEGAPULS WL 61 ultrasonic level sensor: at 0 m measuring distance the output sits near 9.95 V, and the voltage falls as the level rises. The PLC does not work in volts — it works in raw integer counts — so the engineer must rescale and mathematically reverse the relationship between the raw count and the engineering unit (meters or millimeters).
This article walks through three production-ready methods for the Siemens SIMATIC S7-1212C AC/DC/Rly using the default SM 1231 analog input signal board (or signal module) and TIA Portal ladder logic:
- Direct integer inversion by subtracting from 27648.
- Two-step NORM_X + SCALE_X rescaling with sign inversion.
- Single CALCULATE block using the linear equation y = mx + b.
The recommended final stage depends on whether you need only the polarity inverted (Method 1 or 2) or both inverted and rescaled to engineering units (Method 3).
Prerequisites
Hardware
- Siemens SIMATIC S7-1212C CPU (article number 6ES7212-1BE40-0XB0 or compatible AC/DC/Rly variant).
- Signal board SB 1231 (e.g., 6ES7231-4HA30-0XB0, 1 AI, 0–10 V / ±10 V / 0–20 mA) or signal module SM 1231 (e.g., 6ES7231-4HF32-0XB0, 4 AI).
- VEGA VEGAPULS WL 61 ultrasonic sensor with 4–20 mA current output.
- 500 Ω precision sense resistor (≤ 0.1 % tolerance, ≥ 0.25 W) wired across the analog input terminals to convert 4–20 mA to 2–10 V, or a 250 Ω resistor to convert to 1–5 V. The original application uses a 500 Ω resistor, which produces a 2–10 V signal under the 4–20 mA loop.
- 24 VDC loop power supply, shielded two-wire cable, and an earth-grounded shield termination at the cabinet entry.
Software
- TIA Portal V16 or newer (V17/V18 supported for the same instructions).
- S7-1200 CPU firmware V4.2 or newer (NORM_X / SCALE_X are available from firmware V4.0 onwards).
- CPU configuration: analog input channel configured for voltage measurement, 0–10 V range, no smoothing filter (or a filter of 1–4 cycles if the level surface is turbulent).
Understanding the Raw Analog Value
For the SIMATIC S7-1200 SM/SB 1231 in the unipolar 0–10 V range, the conversion formula from the official Siemens manual is:
Digital value = (Voltage / 10 V) × 27648
So 0 V → 0, 10 V → 27648, with 9.95 V → 27504. For the VEGAPULS WL 61 in this application the relationship between measured distance and raw count is therefore:
| Distance (m) | Sensor output (V) | Raw count (IW) |
|---|---|---|
| 0.0 | 9.95 | 27504 |
| 1.0 | 9.00 | 24883 |
| 2.0 | 8.20 | 22671 |
| 3.0 | 7.40 | 20460 |
| 4.0 | 6.60 | 18248 |
| 5.0 | 5.80 | 16036 |
By default the CPU places the input word in %IW64 for the SB 1231 in slot 1 of the CPU. Verify the address in Device View → Analog inputs → Properties → I/O addresses; the system manual S7-1200 System Manual (entry ID 39334504) documents the default addresses per slot.
Method 1 — Direct Integer Inversion
The fastest path: subtract the raw count from the full-scale integer 27648. This gives a value that is large at far distance (10 V → 0) and small at near distance (0 V → 27648), but it is unsigned and not scaled to engineering units. Use it only as an intermediate or when you only need a polarity-flipped count.
// LAD network 1
+----[ %IW64 ]----[ MOVE ]----+ // read raw input
| |
+----[ 27648 ]----[ SUB ]------+ // 27648 - %IW64
| |
| +----[ %QW80 ]+ // optional: write to analog output
+----( Distance_Raw ) // tag of type INT
In ladder: place a SUB (SUB_I) block, set IN1 = 27648, IN2 = %IW64, OUT = a tag such as Distance_Raw of type INT. The result is a value 0 at full-scale and 27648 at zero. Wire the negated count to a SCALE_X stage if you need engineering units.
Method 2 — NORM_X then SCALE_X with Sign Inversion
This is the canonical Siemens pattern documented in the TIA Portal help (instruction NORM_X / SCALE_X). The chain is: raw integer → normalized REAL 0.0–1.0 → engineering unit with reversed slope.
Network 1: NORM_X
| Pin | Value | Notes |
|---|---|---|
| MIN | 0 | Minimum integer of the analog range |
| VALUE | %IW64 | Raw analog input |
| MAX | 27648 | Maximum integer of the analog range |
| OUT | NormValue (REAL) | Output 0.0–1.0 |
Network 2: SCALE_X (inverted)
| Pin | Value | Notes |
|---|---|---|
| MIN | 5.0 | Maximum distance in meters (corresponds to 0 V after inversion) |
| VALUE | 1.0 - NormValue | Flipped normalized value: 0 V → 1.0, 10 V → 0.0 |
| MAX | 0.0 | Minimum distance in meters (corresponds to 10 V after inversion) |
| OUT | Distance_m (REAL) | Engineering unit, in meters |
Subtracting the NORM_X output from 1.0 inverts the slope, then the SCALE_X block maps the inverted normalized value into the engineering range (0 m to 5 m in this example). Adjust MIN/MAX to your tank geometry.
// ST equivalent for Method 2
NormValue := NORM_X(MIN := 0, VALUE := %IW64, MAX := 27648);
Distance_m := SCALE_X(MIN := 5.0, VALUE := 1.0 - NormValue, MAX := 0.0);
Method 3 — CALCULATE Block with Linear Equation
For a single network that does everything, use the CALCULATE instruction. Starting from a normalized 0.0–1.0 input (post-NORM_X), the linear equation for an inverted sensor is derived from y = mx + b. The two calibration points given in the application (1 m @ 9.0 V and 2 m @ 8.2 V) yield:
m = (2 − 1) / (0.82 − 0.90) = −12.5 m per unit
Flipping the sign and solving for the offset that gives 0 m at 0.995 (9.95 V):
Distance_m = 12.5 × NormValue − 12.4375
CALCULATE configuration
// CALCULATE block: y = 12.5 * x - 12.4375
// IN1 = NormValue (REAL, 0.0 to 1.0)
// OUT = Distance_m (REAL)
// Expression: 12.5 * IN1 - 12.4375
| NormValue (x) | Calculated distance (m) | Comment |
|---|---|---|
| 0.000 | −12.4375 | Below valid sensor range — clamp in HMI |
| 0.820 | −2.1875 | Below valid sensor range — clamp in HMI |
| 0.900 | −1.1875 | Below valid sensor range — clamp in HMI |
| 0.995 | 0.0000 | 0 m — sensor bottom |
| 1.000 | 0.0625 | Sensor bottom + a few cm |
MIN(MAX(Out, 0.0), 8.0) in the same CALCULATE expression prevents negative or out-of-range values from reaching the HMI.
Complete Ladder Example (Method 3)
Network 1: NORM_X
+----[ %IW64 ]----[ NORM_X ]----+ // MIN=0, MAX=27648
| |
+---- NormValue ---+ // tag REAL
Network 2: CALCULATE
+----[ NormValue ]----[ CALCULATE ]----+ // 12.5*IN1 - 12.4375
| |
+---- Distance_m -------+ // tag REAL
Network 3: Clamp
+----[ Distance_m ]----[ MIN ]----[ MAX ]----+ // MAX(0, MIN(8, x))
| |
+---- Level_m -------------------+ // tag REAL for HMI
Hardware Verification
- With the sensor powered and aimed at the empty vessel, force the watch table to display
%IW64. Confirm the value is near 27504 ± 50 counts (9.95 V ± 0.02 V). - Move a hard target 1 m from the sensor face. Confirm the count drops to approximately 24883.
- Observe the
Distance_mtag in the watch table — it should read 0.000 at step 1 and 1.000 ± 0.02 at step 2. - Trigger an online trace in TIA Portal (Tools → Trace) to log
%IW64,NormValue, andDistance_mwhile the level changes. Verify monotonic, linear behavior with no ringing. - Compare against an independent reference (e.g., a tape measure) over the full 0–5 m range. Acceptable error band for the WL 61 is ±0.2 % of full scale per the manufacturer datasheet.
Troubleshooting Matrix
| Symptom | Likely cause | Action |
|---|---|---|
| %IW64 stuck at 0 or 32767 | Channel disabled or overflow | Verify analog input is configured for voltage in Device Configuration. Check the “Enable” check-box for the channel. |
| Reading jumps erratically | No shield / shield grounded at sensor only | Ground the shield at the cabinet end only, with a parallel 1 MΩ / 100 nF network if the sensor manufacturer allows it. |
| Distance is always half of expected | Wrong resistor value (250 Ω instead of 500 Ω) | Recalculate using 1–5 V (250 Ω) or 2–10 V (500 Ω). Adjust the SCALE_X MIN/MAX or the CALCULATE coefficients. |
| Distance is correct but inverted (e.g., 0 m shows 5 m) | Forgot the 1.0 - NormValue step in Method 2, or sign error in Method 3 | Add the inverting subtract; verify y = mx + b against the two calibration points. |
| Counts saturated at 27648 with the sensor at 0 m | Loop supply missing, current loop is open-circuit (4 mA → 0 V across 500 Ω) | Check 24 VDC loop supply; check that the 500 Ω resistor is in parallel with the AI terminals, not in series. |
| Distance drifts with temperature | Speed-of-sound correction not enabled | Enable the VEGA WL 61 built-in temperature compensation or apply an external 4-wire RTD (Pt100) on the AI2 channel and use it as a correction factor in CALCULATE. |
Field Tips
- Sense resistor tolerance. A 0.1 % 500 Ω resistor produces a 0.05 % full-scale error contribution — an order of magnitude better than a 1 % resistor. For the WL 61 this is the dominant scaling error source.
- Block dead-band. Configure the SB/SM 1231 with a 50/60 Hz hardware filter at 50 Hz (line frequency in EU/Asia) to suppress mains pickup on long cable runs.
- Wire length. Keep the sensor cable shield continuous through the cabinet wall; the VEGAPULS WL 61 datasheet recommends ≤ 250 m with a 0.5 mm² shielded two-wire cable.
-
Overflow diagnostic. Wire
%IW64into a LIMIT block so the HMI displays "Sensor fault" if the value falls below 0 or exceeds 27648 (overrange bit 32767). - Multiple calibration points. For highest accuracy, perform a 2-point calibration in the actual process medium (water vs. oil changes the speed of sound) and replace 12.5 and 12.4375 with the measured coefficients.
Standards and References
For engineers documenting the design, the relevant signal ranges, error bands, and EMC requirements are governed by:
- Siemens SIMATIC S7-1200 Programmable Controller System Manual (entry ID 39334504) — analog input wiring, default addresses, and 0–10 V scaling table.
- IEC 61131-2:2017 — Programmable controllers, equipment requirements and tests, including analog signal levels.
- IEC 60584 / NAMUR NE 43 — 4–20 mA current loop conventions and fault current levels (3.6 mA / 21 mA for sensor diagnostics).
- Balluff: Analog signals 0…10 V vs. 4…20 mA — guidance on choosing voltage vs. current loop for industrial position sensors.
FAQ
Why does the sensor show 9.95 V at 0 m and not 10 V?
The VEGAPULS WL 61 is a 4–20 mA two-wire loop sensor. With a 500 Ω sense resistor the live-zero at 4 mA is 2 V, so the output is offset; the 9.95 V at 0 m reflects a 4.020 mA live-zero, which is within the IEC 61131-2 tolerance and normal for this sensor family.
Can I use the 1.0 - NormValue trick with a bipolar ±10 V range?
No. The 1.0 - x inversion only works for unipolar 0–10 V. For ±10 V the full-scale integer is 27648 and the input includes negative counts; you must apply the full NORM_X / SCALE_X chain with a min/max of −27648 / +27648 and shift the offset accordingly.
What is the difference between NORM_X and the legacy SCALE / UNSCALE blocks?
NORM_X and SCALE_X are the unified 32-bit, type-flexible successors to the legacy SCALE and UNSCALE instructions in TIA Portal V13+ and require no manual shift-left of 3 bits. They support INT, DINT, REAL, and LREAL on the same block.
Do I need the CALCULATE block if I already use NORM_X and SCALE_X?
No. The CALCULATE block is only required when the relationship is not strictly linear, or when you want to add dead-zone, temperature compensation, or limit clamping in a single instruction. For simple inversion and rescaling, the NORM_X + SCALE_X method (Method 2) is more transparent to the next engineer.
How do I verify the inversion is correct on an empty vessel?
Force the watch table to display the final engineering unit (Distance_m). With the empty vessel the value should be at the maximum measurement range (5 m in the example), and the HMI level bar should drop as the vessel fills. If the bar rises as the vessel fills, the sign of m is still inverted.