Configuring 10 S7-300 Analog Inputs with a Generic Pressure FB

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

Overview: 10-Channel Analog Pressure Acquisition on SIMATIC S7-300

A SIMATIC S7-300 station can be expanded to 10 analog inputs (AI) and 10 analog outputs (AO) using a combination of SM 331 analog input modules and SM 332 analog output modules configured in HWConfig. The CPU 315-2DP supports up to 32 analog channels in its standard address space, and STEP 7 automatically assigns peripheral input/output (PII/PIQ) addresses outside the process image (PI/PIQ) — typically starting at byte 256 for SM modules on rack 0.

For 10 pressure transmitters with 4–20 mA or 0–10 V outputs, the cleanest approach is to use a generic function block (FB) that wraps the standard Siemens converter FC105 "SCALE" and is called ten times from OB1 with different instance DBs. Each call uses its own peripheral input word (PIW) and its own engineering scale. This article documents the hardware configuration, the address map, the FB code, the wiring rules, and the verification procedure.

Peripheral vs. process-image addressing: Analog channels on the S7-300 live in the peripheral address area, not in the standard process image. The PII/PIQ is only refreshed by the CPU automatically for selected bytes in the process image. Analog channels must be read with PIW (peripheral input word) and written with PQW (peripheral output word). IW and QW only return the last snapshot loaded by the CPU and will not reflect a fresh analog reading if the channel is not part of the configured process image.

Prerequisites

  • STEP 7 V5.5 (or compatible) with Simatic Manager and HWConfig installed.
  • CPU 315-2DP (6ES7 315-2AG10-0AB0 or later) with sufficient free slots in the central rack.
  • SM 331 analog input module(s) supporting the required signal type. For 4–20 mA: SM 331-7KF02 (8AI, 12-bit) or SM 331-1KF02 (8AI, 13-bit). For 0–10 V: same modules with the appropriate range card.
  • SM 332 analog output module(s) for any companion outputs (8AO: 6ES7 332-5HF00).
  • 10 pressure transmitters (4–20 mA, 2-wire or 4-wire) with documented full-scale and zero values.
  • 24 V DC sensor supply (PS 307 5A or separate DC supply) and shielded twisted-pair signal cable.
  • TI-S7 Converting Blocks library (installed by default with STEP 7 V5.5; contains FC105 and FC106).

S7-300 Analog Module Selection for 10 Channels

For 10 AI and 10 AO, the cleanest hardware combination is two SM 331s and two SM 332s in the central rack. The peripheral address map generated by HWConfig for this combination is:

Slot Module Order Number Channels Input Addresses (PII) Output Addresses (PIQ)
1 PS 307 5A 6ES7 307-1EA00-0AA0
2 CPU 315-2DP 6ES7 315-2AG10-0AB0
3
4 SM 331 AI8 6ES7 331-7KF02-0AB0 8 AI PIW 256..271
5 SM 331 AI2 6ES7 331-7KB02-0AB0 2 AI PIW 272..275
6 SM 332 AO8 6ES7 332-5HF00-0AB0 8 AO PQW 288..303
7 SM 332 AO2 6ES7 332-5HB01-0AB0 2 AO PQW 304..307

The split at 256/288 reflects the S7-300 convention: SM 331 input modules start the analog PII at byte 256, and SM 332 output modules start the analog PIQ at byte 288. The byte numbers shown above are produced by a CPU 315-2DP in slot 2 with no other analog modules on rack 0; if additional analog modules are already configured, the addresses will shift accordingly.

For 0–10 V pressure transducers, set the range card on the SM 331 side to the "B" position (voltage, 0–10 V). For 4–20 mA transmitters, use the "C" or "D" position depending on the exact SM 331 variant — see the range-card legend printed on the module housing and the S7-300 Module Data manual.

HWConfig: Building the Rack and Allocating Peripheral Addresses

Open Simatic Manager, double-click the S7-300 station, and launch HWConfig. Drag the modules from the hardware catalog into slots in the following order:

  1. Slot 1: PS 307 power supply.
  2. Slot 2: CPU 315-2DP.
  3. Slot 3: leave empty or place a digital module (digital I/O does not consume the analog PII/PIQ area).
  4. Slot 4: SM 331 (8AI) — addresses PIW 256..271 are assigned automatically.
  5. Slot 5: SM 331 (2AI) — addresses PIW 272..275 are assigned automatically.
  6. Slot 6: SM 332 (8AO) — addresses PQW 288..303 are assigned automatically.
  7. Slot 7: SM 332 (2AO) — addresses PQW 304..307 are assigned automatically.

Save and compile (Station → Save and Compile). HWConfig downloads the configuration to the CPU on the next programming device online connection; the CPU will not enter RUN until the actual module types match.

S7-300 Rack 0 — Address Map for 10 AI / 10 AO PS 307Slot 1 CPU 315-2DP(slot 2)Slot 2 Empty / DI/DOSlot 3 SM 331 AI8PIW 256..271Slot 4 SM 331 AI2PIW 272..275Slot 5 SM 332 AO8PQW 288..303Slot 6 SM 332 AO2PQW 304..307Slot 7 Free slots 8..11 (max 8 SM modules on CPU 315-2DP rack 0)

The ten 16-bit peripheral input words are at byte addresses 256, 258, 260, 262, 264, 266, 268, 270, 272, 274. STEP 7 numbers analog channels from 0 to 7 inside an 8AI module and 0 to 1 inside a 2AI module; the byte offset is module base + (channel × 2). Do not use the bit-level I and Q operand areas for these words — they only exist in the PII/PIQ peripheral area.

FC105 SCALE — Inputs, Outputs, and Bipolar Handling

FC105 ("SCALE") is part of the TI-S7 Converting Blocks library. It converts a 16-bit peripheral input word into a REAL engineering value using the formula:

OUT = ((IN - IN_LO) / (IN_HI - IN_LO)) × (HI_LIM - LO_LIM) + LO_LIM

where:

  • IN is the raw peripheral word (0..27648 unipolar, -27648..+27648 bipolar).
  • IN_LO / IN_HI are fixed by the module and the BIPOLAR pin: 0/27648 (unipolar) or -27648/+27648 (bipolar).
  • LO_LIM / HI_LIM are the engineering low and high values supplied by the user as REAL.
  • OUT is the scaled REAL engineering value.
  • RET_VAL is W#16#0000 on success; W#16#0007 indicates an IN value out of range; W#16#0008 indicates a LO_LIM ≥ HI_LIM configuration error.
FC105 Pin Type Meaning / Source
IN INT Peripheral input word, e.g. PIW 256.
HI_LIM REAL Engineering high, e.g. 350.0 (bar at 20 mA).
LO_LIM REAL Engineering low, e.g. 0.0 (bar at 4 mA).
BIPOLAR BOOL FALSE = 0..27648 (unipolar), TRUE = -27648..+27648 (bipolar). See the "Always On / Always Off" note below.
RET_VAL WORD Return code: W#16#0000 = OK. Non-zero = configuration error or out-of-range input.
OUT REAL Engineering value, e.g. MD200 or a data word in the instance DB.
Why "Always_On / Always_Off" flags? In STEP 7 V5.5, you cannot wire a literal TRUE or FALSE constant directly to a BOOL input of a system block in the LAD/FBD editor. The accepted workaround is to define two flag bits — typically M0.0 = TRUE and M0.1 = FALSE — in the M-flag area and wire those to the BIPOLAR pin. Inside our generic FB the same effect is achieved with two static BOOLs initialised at TRUE and FALSE, so the user does not have to manage M0.0 / M0.1 at the call site.

For 4–20 mA pressure transmitters, the correct FC105 parameters are:

CALL "SCALE"   // FC105
    IN      := PIW256          // raw 0..27648
    HI_LIM  := 3.500002E+02    // 350.0 bar at 20 mA
    LO_LIM  := 0.0             // 0.0 bar at 4 mA
    BIPOLAR := "M0.1"          // FALSE — unipolar
    RET_VAL := MW100
    OUT     := MD200

For ±10 V bipolar transducers the same block is used with BIPOLAR := "M0.0" (TRUE) and the LO_LIM / HI_LIM are set to negative/positive full scale, e.g. -10.0 / +10.0 V, or to the engineering equivalent of ±1000 mbar for a vacuum-pressure sensor.

Designing the Generic Pressure Function Block (FB100)

The user requirement is a single FB called ten times, one per gauge, with different peripheral addresses and engineering ranges. STEP 7 supports this directly: create an FB with IN/OUT/STAT/TEMP parameters, call it from OB1 (or a cyclic OB such as OB35) ten times with separate instance DBs, and the FB holds the per-channel scaling parameters in its instance DB.

Insert a new FB in the S7 program: Insert → S7 Block → Function Block, name it FB100, family "PRESSURE". Open the declaration table and define the following interface:

Section Name Type Initial Value Comment
VAR_INPUT i_RawValue INT 0 Peripheral input word (PIW)
VAR_INPUT i_LoLim REAL 0.0 Engineering low (e.g. 0.0 bar)
VAR_INPUT i_HiLim REAL 100.0 Engineering high (e.g. 350.0 bar)
VAR_INPUT i_bBipolar BOOL FALSE TRUE for ±10 V, FALSE for 4–20 mA
VAR_OUTPUT o_EngValue REAL Scaled engineering value
VAR_OUTPUT o_wError WORD W#16#0 FC105 return code
VAR s_bZero BOOL FALSE Static FALSE for BIPOLAR unipolar use
VAR s_bOne BOOL TRUE Static TRUE for BIPOLAR bipolar use
VAR_TEMP No temporaries required

The body of FB100 contains a single FC105 call. The BIPOLAR pin is wired to a conditional that selects s_bZero or s_bOne based on the i_bBipolar input:

// FB100 - PRESSURE_SCALE (generic pressure scaling FB)
//
// One instance per gauge. The calling block (typically OB1 or
// a cyclic OB such as OB35) supplies the peripheral input word
// and the engineering limits. This block internally calls the
// standard Siemens FC105 "SCALE" converter to produce the
// engineering value in REAL.

      A     #i_bBipolar
      JCN   uni
// --- bipolar branch (e.g. ±10 V) ---
      L     #s_bOne                  // TRUE
      T     #s_bBipolarWire          // internal helper flag
      JU    call
uni:  L     #s_bZero                 // FALSE
      T     #s_bBipolarWire
call: CALL "SCALE"                   // FC105
          IN     := #i_RawValue
          HI_LIM := #i_HiLim
          LO_LIM := #i_LoLim
          BIPOLAR:= #s_bBipolarWire
          RET_VAL:= #o_wError
          OUT    := #o_EngValue

The LAD/FBD view of the same code uses a comparator and a MOVE / assignment to switch the BIPOLAR input. From the caller's perspective, no M-flag is required and the wiring of BIPOLAR is handled inside the FB.

Create the instance DBs: in the S7 program, insert ten Data Blocks — DB101..DB110 — each as Instance DB associated with FB100. Alternatively, build a multi-instance DB that owns all ten FB100 instances from a single parent FB. STEP 7 will pre-allocate the static variables (s_bZero, s_bOne) once per instance.

OB1 Cyclic Call: Instantiating the FB Ten Times

In OB1, call FB100 ten times. Use a separate instance DB for each call so the per-channel limits and current scaled value are isolated:

// ===========================================
// Pressure gauge scaling — 10 channels
// ===========================================
//
// Address assignments from HWConfig:
//   Gauge 1  -> PIW 256    Gauge 2  -> PIW 258
//   Gauge 3  -> PIW 260    Gauge 4  -> PIW 262
//   Gauge 5  -> PIW 264    Gauge 6  -> PIW 266
//   Gauge 7  -> PIW 268    Gauge 8  -> PIW 270
//   Gauge 9  -> PIW 272    Gauge 10 -> PIW 274
//
// All gauges are 0–350 bar, 4–20 mA (unipolar).

      CALL FB100, DB101                 // Gauge 1
        i_RawValue  := PIW256
        i_LoLim     := 0.0
        i_HiLim     := 350.0
        i_bBipolar  := FALSE
        o_EngValue  := "PRESS".Gauge1
        o_wError    := "PRESS".Err1

      CALL FB100, DB102                 // Gauge 2
        i_RawValue  := PIW258
        i_LoLim     := 0.0
        i_HiLim     := 350.0
        i_bBipolar  := FALSE
        o_EngValue  := "PRESS".Gauge2
        o_wError    := "PRESS".Err2

      // ... repeat for DB103..DB110 / PIW260..PIW274 ...

The symbol table for the data block "PRESS" (e.g. DB120 — a shared global DB holding the ten REAL results) is:

Symbol Address Type Comment
PRESS.Gauge1 DB120.DBD0 REAL Pressure reading gauge 1 (bar)
PRESS.Gauge2 DB120.DBD4 REAL Pressure reading gauge 2 (bar)
... ... REAL ...
PRESS.Gauge10 DB120.DBD36 REAL Pressure reading gauge 10 (bar)
PRESS.Err1 DB120.DBW40 WORD FC105 return code gauge 1
... ... WORD ...
PRESS.Err10 DB120.DBW58 WORD FC105 return code gauge 10

Pressure Gauge Scaling — Worked Examples

For a 0–350 bar gauge with a 4–20 mA output connected to an SM 331 channel configured for 4–20 mA:

P_bar = (PIW - 0) / (27648 - 0) × (350 - 0) + 0

Worked points:

  • 4.000 mA → raw 0 → 0.000 bar
  • 8.000 mA → raw 5529.6 → 70.00 bar (20 % of FS)
  • 12.000 mA → raw 11059.2 → 140.00 bar (40 % of FS)
  • 16.000 mA → raw 16588.8 → 210.00 bar (60 % of FS)
  • 20.000 mA → raw 27648 → 350.00 bar (100 % of FS)

For a -1..+10 bar compound gauge (vacuum to 10 bar, ±10 V bipolar), the formula is:

P_bar = (PIW - (-27648)) / (27648 - (-27648)) × (10 - (-1)) + (-1)

Note that the bipolar input range uses the full -27648..+27648 span, so the scaling is symmetric. For asymmetric ranges such as -0.5..+10.0 bar, set LO_LIM = -0.5 and HI_LIM = 10.0; FC105 will linearly scale the same input range to the asymmetric output.

Live-zero underrange detection: For 4–20 mA, the live-zero at 4 mA (raw 0) is a valid signal. Siemens convention treats values < 0 (which never occur with a healthy 4–20 mA loop) as wire break, but some transmitter designs present 3.6 mA as the underrange threshold. The recommended check is in the user program: L 0; L PIW256; <I; JC wire_break. A reading at exactly 0 is also ambiguous (4 mA vs. broken wire); a 4–20 mA gauge failing below 3.8 mA usually signals a wire break that the SM 331 already detects via the open-circuit LED and the diagnostic interrupt.

FC106 UNSCALE for Companion Analog Outputs

When the application also needs ten 0–10 V control outputs (e.g. proportional valve commands derived from the pressures), use FC106 "UNSCALE" — the inverse of FC105. The address map for outputs is PQW 288..307 in our hardware example.

CALL "UNSCALE"   // FC106
    IN      := "PRESS".CmdValve1   // REAL, e.g. 0.0..100.0 (%)
    HI_LIM  := 100.0
    LO_LIM  := 0.0
    BIPOLAR := "M0.1"              // FALSE for 0..10 V
    RET_VAL := "PRESS".AO_Err1
    OUT     := PQW288

The OUT pin writes to the peripheral output word and the SM 332 latches the value at the next analog output update. Use the same FB-wrapping technique to make a generic FB200 that contains the FC106 call and is called ten times with the appropriate PQW and engineering range.

Wiring, Shielding, and Range-Module Setup

  1. Range card on SM 331. The SM 331 AI8 (6ES7 331-7KF02) has a removable range card on the left side of the housing. Set the card to position "C" for 4–20 mA or "B" for 0–10 V. The range card is read at power-up; if you change it, cycle power to the module.
  2. Wiring topology. For 4-wire transmitters, use a separate 24 V supply to the transmitter and return the mA signal on its own pair. For 2-wire transmitters, use the SM 331 channel's M+ / M- terminals and the 24 V supplied by the module (U+ / U- terminal pair, current-limited to ~25 mA per channel).
  3. Shielding. Ground the cable shield at the cabinet entry using a PE rail or shield bar. Do not ground at both ends unless the building uses a single-point ground; ground loops will inject 50/60 Hz into the mA loop.
  4. Channel grounding. For 4–20 mA, connect Comp (compensation / common) of the channel to M- on the SM 331 terminal block. For 0–10 V with single-ended inputs, leave Comp open or connect to ground as documented.
  5. Unused channels. Short the unused channels' M+ to M- with a jumper and set the range card position to "B" (voltage) — open unused current inputs are a common source of diagnostic interrupts and channel fault LEDs.
  6. Cable type. Use LiYCY or similar shielded twisted-pair, ≤ 50 m for 4–20 mA to keep loop resistance below the transmitter's compliance voltage limit. For 0–10 V, keep ≤ 10 m to limit capacitive loading.
Common wiring mistake: tying the shield to the 0 V terminal of the 24 V supply (DC-) rather than to PE. This couples SM 331 common-mode noise directly into the analog ground and yields unstable readings on multiple channels.

Commissioning, Verification, and Online Monitoring

  1. Compile and download. In Simatic Manager, select the S7 program blocks and click PLC → Download. The CPU should go RUN after a STOP-to-RUN transition if the hardware matches HWConfig.
  2. Online → Monitor/Modify. Open OB1 in the online view. Confirm each PIW256..PIW274 shows a value in the expected range (e.g. 0..27648 for 4–20 mA). A reading of -32768 (W#16#8000) usually means wire break on a 4–20 mA channel.
  3. Force-test the FC105 call. Set PIW256 to a known value using Variable Table → Monitor/Modify. The corresponding DB120.DBD0 (Gauge1) should be 0.0 at raw=0 and 350.0 at raw=27648. If the value is stuck at 0 or shows a wildly large number, check the FC105 RET_VAL in DB120.DBW40 — non-zero indicates LO_LIM ≥ HI_LIM or an overflow.
  4. Calibration sweep. Apply 4 mA, 8 mA, 12 mA, 16 mA, 20 mA from a precision calibrator on each channel and record the engineering value. The deviation at any point should be within the SM 331's accuracy spec (typically ±0.3 % of full scale for the 6ES7 331-7KF02 at 25 °C, ±0.6 % over 0..60 °C).
  5. Cross-check FB instance DBs. Open DB101..DB110 online and verify the instance data: i_RawValue (last read), o_EngValue (last scaled), o_wError. If the instance DB and the OB1 call use mismatched names, the FB will be reinitialised each cycle and the static helper bits will lose their state.
  6. Diagnostic buffer. On any unexpected STOP, check the CPU diagnostic buffer (PLC → Diagnostic Buffer) for "OB not loaded", "Module failure", or "I/O access error" entries that point to the offending PIW address.

Fault Codes, Diagnostics, and Common Pitfalls

Symptom Likely Cause Fix
PIW shows -32768 (W#16#8000) 4–20 mA wire break or unconnected channel Check loop continuity, range card position (must be "C" for 4–20 mA), and that Comp is linked to M-.
PIW shows 27648 but engineering value is wrong HI_LIM/LO_LIM swapped or signed/unsigned mismatch Verify FC105 LO_LIM < HI_LIM; check that the transmitter range matches the engineering unit range.
o_wError ≠ W#16#0000 FC105 misconfiguration or input out of range W#16#0007 = IN out of range, W#16#0008 = LO_LIM ≥ HI_LIM. See the FC105 help entry in the STEP 7 standard library.
Reading drifts ±0.5 % over time 50/60 Hz ground loop, shared return Re-route shield to PE at cabinet only; separate analog and digital grounds.
CPU goes STOP after adding the FB OB1 stack overflow from ten FC105 calls Move FC105 calls into OB35 (cyclic interrupt) or call a single shared instance. S7-300 has limited local-stack space per priority class.
OB1 reports "I/O access error" at PIWxxx HWConfig module pulled or different module inserted Compare actual hardware to HWConfig; re-download HWConfig or remove the offending module from the rack.
Pressure reads negative for a 0–10 bar gauge Wrong BIPOLAR flag (TRUE on a unipolar signal) Set i_bBipolar = FALSE at the call site or check the static bit in the instance DB.
All pressures identical and frozen at one value One PIW was wired to all call sites; the FB always reads the same address Check that i_RawValue receives PIW256, PIW258, … in sequence — common typo when copy-pasting the call.
Reference materials: The FC105 / FC106 functions and the SM 331 / SM 332 module data are documented in the STEP 7 Standard Library help and the S7-300 Module Data manual. The Siemens Industry Online Support portal hosts the canonical entries for FC105 SCALE and the S7-300 Module Data manual. The TI-S7 Converting Blocks library is installed by default with STEP 7 V5.5 at the path ...\Step7\S7LIBS\Converting.

FAQ

Why do I have to use PIW and not IW for the analog inputs?

Analog channels on the SM 331 occupy the peripheral I/O area (PII/PIQ) starting at byte 256. The IW operand only sees the process-image snapshot that the CPU refreshes automatically for selected digital byte ranges; analog bytes are not part of the default process image. Reading PIW256 forces a direct peripheral access on the backplane bus, which is the supported method for S7-300 analog I/O.

Can I have more than one FB100 instance sharing the same code but with different DB numbers?

Yes — that is exactly the recommended pattern. STEP 7 supports multi-instance (one parent FB owning all ten instances) or ten separate instance DBs (DB101..DB110). Each instance DB has its own set of i_LoLim, i_HiLim, and the per-channel o_EngValue and o_wError static outputs, so you can mix 0–350 bar gauges with 0–10 bar gauges and ±10 V bipolar transducers in the same FB100 calls without rewriting code.

What is the difference between FC105 and FC106?

FC105 "SCALE" converts a 16-bit raw input (PIW) to a REAL engineering value (read direction); FC106 "UNSCALE" converts a REAL engineering value back to a 16-bit raw output (PQW) for analog output modules. They share the same BIPOLAR convention and the same RET_VAL error codes, so pairing FC105 on the input side and FC106 on the output side with symmetric limits is straightforward.

Do I have to set the SM 331 range card before powering up?

Yes. The SM 331 reads the range card position only at power-on. If you change a card after the CPU is in RUN, the new setting will not take effect until the next power cycle or until the channel is re-initialised via the diagnostic interrupt acknowledgement procedure. Always cycle power after changing a range card.

My FC105 RET_VAL shows W#16#0008. What does that mean?

W#16#0008 means that LO_LIM is greater than or equal to HI_LIM — a configuration error in the scaling range. Check the engineering limits passed to FB100 at the call site. For a 0–350 bar gauge, the correct values are LO_LIM = 0.0 and HI_LIM = 350.0. The error is non-fatal; FC105 will continue to return the last valid output, but the diagnostic should be cleared before relying on the reading.

Back to blog