S7-300 FM352 Speed-Dependent Cam Control for Labeling Machines

David Krause19 min read
S7-300SiemensTroubleshooting
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 Overview

A SIMATIC S7-300 station built around a CPU 315 and an FM 352 cam controller is used to drive a rotary bottle labelling machine. The cap presence sensor and the shaft encoder feed a double-word shift register in the user program; the resulting shifted bit triggers an external valve that dispenses the label.

At low shaft speeds the valve fires inside the dispense window. As the machine ramps up past its rated throughput, the same static shift position fires too late: the label drops behind the first bottle in a batch and a second label is wasted after the last bottle. Moving the trigger bit one position earlier fixes the high-speed case but causes the same valve to misfire at low speed.

This is a classic speed-synchronisation problem. The shift register position is not the variable that should drive the output directly. The output must be advanced in proportion to the current shaft speed so that the mechanical latency of the valve, the solenoid, and the label feeder is cancelled by a position offset that grows as speed increases.

Failure signature. The first bottle in a cycle loses its label and a phantom label appears after the last bottle whenever the shaft speed rises above the working point that the static shift position was tuned for. Lower speeds remain correct.

2. System Architecture

The hardware used in the field is the standard SIMATIC S7-300 cam-control configuration described in the FM 352 manual.

Component Order Number Role
CPU 315-2 DP 6ES7315-2AH14-0AB0 User program, shift register, OB1 cycle
FM 352 cam controller 6ES7352-1AH02-0AE0 Encoder input, cam switching, high-speed DI/DO
SM 321 digital input 6ES7321-1BL00-0AA0 Cap presence sensor (24 V inductive)
SM 322 digital output 6ES7322-1BH01-0AA0 Valve driver (problematic path)
Shaft encoder 24 V HTL or 5 V TTL Machine shaft position

The shaft encoder is wired to the FM 352 encoder inputs, not to a standard DI module. The FM 352 was selected precisely because the encoder pulses at high shaft speed are too short for a standard SM 321 input to debounce, and because the FM 352 can switch its onboard outputs without involving the CPU 315 OB1 cycle.

3. Root Cause Analysis

Three independent latencies add together between the moment the shifted bit goes true in the CPU and the moment the label physically leaves the valve.

  1. Process image update of the SM 322 output byte. The output byte written by the user program in OB1 is only transferred to the module at the end of OB1, on the OB1 PII/PIQ update, or via direct I/O access. With a CPU 315 in default scan, the worst-case time between a true condition in the shift register and the corresponding bit on the output terminal block is one full OB1 cycle plus the cycle time of the application program (typically 10 to 25 ms on a CPU 315).
  2. Solenoid and valve mechanical latency. The dispense valve has a fixed opening time (typically 8 to 15 ms for a 24 V DC label solenoid). This latency is constant; it is not a function of CPU scan.
  3. Mechanical transit between the valve and the bottle. The bottle moves a fixed physical distance between the moment the valve is opened and the moment the label is captured. At higher shaft speed that fixed distance represents a larger number of encoder counts.

The first term is the speed-dependent one. The CPU process image latency of roughly 10 to 25 ms, when expressed in encoder counts, is roughly proportional to shaft speed. At low speed the count loss is small and the label still lands in the window. At high speed the count loss exceeds the dispense window, the label trails the bottle, and a second label fires on the next bottle of the batch.

Replacing the SM 322 with the FM 352 onboard digital outputs removes the CPU process image term entirely: the FM 352 can switch outputs from a cam track in a deterministic response of a few microseconds after the encoder edge, independent of OB1. This is the first lever to pull.

4. FM 352 Onboard Resources

The FM 352 cam controller (6ES7352-1AH02-0AE0) is a high-speed Boolean processor with its own encoder, its own cam sequencer, and a set of onboard I/O that bypasses the backplane. The resources relevant to a labelling application are:

  • Encoder interface: one 24 V HTL or 5 V TTL incremental encoder input, RS422 differential option.
  • Digital inputs: 12 onboard 24 V inputs and 3 onboard 5 V differential inputs that can be used as triggers for cam tracks, tracks, or as fast inputs for the application.
  • Digital outputs: 8 onboard 24 V outputs that are switched directly by the FM 352 cam sequencer. These outputs are the correct destination for the dispense valve.
  • Cam tracks: 32 cam tracks, each containing up to 32 cam outputs per revolution. The number of cam tracks is selected in the FM 352 configuration in STEP 7 HW Config.

The dispense valve must be wired to one of the 8 FM 352 onboard outputs, not to the SM 322 behind the backplane. From that point on, the FM 352 issues the dispense command from a cam position that is updated by the encoder counter in hardware; OB1 is no longer in the critical path.

Why the FM 352-5 reference in some discussion threads is misleading. The FM 352-5 (6ES7352-5AH0x) is a separate module: a high-speed Boolean processor without cam switching. It is suitable for counting and high-speed I/O tasks. The labelling problem on a real S7-300 should be solved on the FM 352 that is already installed, using the cam-switching resources that the module is designed for. Adding a second module is unnecessary if the dispense command can be re-routed to the FM 352 outputs.

5. Speed-Compensated Cam Offset

Re-routing the output to the FM 352 is necessary but not sufficient. The dispense window has a fixed mechanical width, and the open-loop latency of the valve plus the label transit is fixed. As the shaft speed increases, the encoder count that represents the valve latency grows linearly with speed. The cam start position must therefore be advanced by a number of encoder counts proportional to the current shaft speed.

The speed value used as the compensator is the rotational speed value produced by the FM 352 in the configured measuring mode "Rotational speed measurement". In this mode the FM 352 returns the current shaft speed scaled in 1/min, and the user assigns the pulses per encoder revolution so that the value has engineering units.

Parameter Symbol Units Source
Pulses per encoder revolution PPR counts/rev FM 352 configuration, encoder data sheet
Current shaft speed n 1/min FM 352 rotational speed output
Sample period of the speed measurement T s FM 352 configuration, gate time
Valve open latency t_v s Solenoid data sheet, measured at 24 V
Mechanical transit valve to bottle t_t s Layout of the dispense head, dimensioned drawing
Compensation angle, in encoder counts N_off counts Computed

The compensation count is:

N_off = round( (t_v + t_t) * n * PPR / 60 )

For example, with PPR = 1024, t_v + t_t = 0.020 s, and n = 600 1/min:

N_off = round( 0.020 * 600 * 1024 / 60 ) = round( 204.8 ) = 205 counts

The same formula with n = 1500 1/min gives N_off = 512 counts, almost three times the position advance. That is exactly the kind of correction the static shift position was missing.

6. STEP 7 Implementation: Reading the Speed

The FM 352 exposes its measured rotational speed as a double word in the assigned input range. The actual byte offset is shown in the FM 352 properties dialog in HW Config under "Addresses". With a CPU 315 and the FM 352 in slot 4 the input range is typically PIW 256 to PIW 271. The speed value is a 16-bit signed integer scaled in 1/min and can be read with standard load instructions.

// Reading the current shaft speed from the FM 352
L PIW 256          // FM 352 measured speed, signed 16-bit, units = 1/min
T MW 100           // working copy

// Convert to counts-per-sample for the offset formula.
// N_off counts = speed[1/min] * PPR / 60 * (t_v + t_t)[s]
L 1024             // PPR, must match the encoder data sheet
L MW 100
*D
T MD 104           // PPR * speed

L 60
/D                 // PPR * speed / 60  -> counts per second of mechanical latency
T MD 108

L 20               // (t_v + t_t) * 100 to keep the integer math in range
*D                 // 0.020 s scaled by 100 = 20
T MD 112

L 100
/D                 // divide back the 100x scaling
T MD 116           // N_off raw, in counts

The working variable MD 116 is the count advance to subtract from the static dispense position. The subtraction and the cam track reprogramming are written in the same cycle so that the FM 352 sees a coherent setpoint.

7. Programming the FM 352 Cam Track in OB1

The FM 352 cam tracks are written via the standard FM 352 function blocks in the STEP 7 library "FM 352". The cam start and end positions for each cam are written with FB CAMSET, and the active track is selected with FB CAMTRCK. The compensation offset computed above is added to the cam start before the call.

// Compute cam start = static start - speed offset
L MW 200           // static cam start, in encoder counts
L MD 116           // N_off, compensation in counts
-I                 // static_start - N_off
T MW 204           // corrected start, signed 16-bit, modulo the encoder range

// Optional: clamp to a safe window so the start cannot wrap past the dispense window
L 0
>=I                // if MW204 < 0
SPB M001
L 0
T MW 204           // clamp to zero
M001: NOP 0

// Write the corrected start into the cam set on the FM 352
CALL FB 21, DB 21  // CAMSET, library "FM 352"
  CAM  := 1        // cam number 1
  ON   := TRUE
  P_ON := MW 204   // start, encoder counts, FM 352 loads the new value
  P_OFF:= MW 206   // end, fixed window width in counts
  ...

Notes on the call:

  • FB 21 and DB 21 must be present in the S7 program; the FM 352 manual lists the exact call interface for the firmware version installed on the module.
  • The cam end position is left at the static dispense window width so the dwell time of the valve is independent of speed. The start is what is advanced.
  • If MW 204 is updated faster than the cam can react, rate-limit the offset update to 10 to 50 ms in the CPU. The FM 352 reads the value at the next encoder zero cross and applies it on the next revolution.

8. Alternative Path: Direct Peripheral Access

If the application cannot be restructured to use the FM 352 onboard outputs, the next-best reduction in latency is to access the SM 322 through direct peripheral addressing, bypassing the process image. Direct peripheral access on a CPU 315 takes on the order of 0.05 microseconds per bit operation, which is two to three orders of magnitude faster than the process image path. It is implemented with the SFC/PERI peripheral load and transfer instructions, or with the L PQW / T PQW pattern.

// Direct peripheral write to the SM 322 output byte 0
L MW 120           // control word assembled in user program
T PQB 0            // direct peripheral write, no process image

// Direct peripheral read of the cap sensor (SM 321 input byte 0)
L PIB 0            // direct peripheral read, no process image
T MW 124

Direct peripheral access is fast, but it is not a substitute for the FM 352 cam path on a high-speed labelling line. The valve latency and the label transit latency still need to be compensated, and that compensation belongs in a cam start position, not in a faster write to the SM 322.

Watch the OB1 scan floor. Direct peripheral access removes the process image latency, but the CPU 315 still has to reach the bit. If OB1 runs at 20 ms, the worst-case latency is still 20 ms. On a labelling machine with sub-10 ms dispense windows this is not enough; the FM 352 path is required.

9. FM 352-5 High-Speed Boolean Processor as an Add-on

If the FM 352 onboard outputs are already used for other tracks and there is no free cam track for the dispense valve, a second module can be added. The FM 352-5 (6ES7352-5AH0x) is a high-speed Boolean processor with its own DI/DO and its own short cycle time. It can be loaded with a small ladder program that performs the speed-compensated output as a stand-alone task. The trigger from the FM 352 is wired to an FM 352-5 input, the rotational speed value is read by the FM 352-5 from the same encoder, and the FM 352-5 drives the dispense output directly.

The application example in the FM 352-5 manual, section 6.2 "Creating the application function block", shows the pattern: a single DI detects the cam trigger from the FM 352, a small FB computes the position advance, and a single DO drives the valve. The 12 onboard 24 V inputs and 8 onboard 24 V outputs of the FM 352-5 are sufficient for the task.

Resource FM 352 FM 352-5
Order number 6ES7352-1AH02-0AE0 6ES7352-5AH00-0AE0 (or -0AE0)
Primary role Cam switching High-speed Boolean
Onboard 24 V DI 12 12
Onboard 24 V DO 8 8
Encoder 1, 24 V or 5 V TTL 1, 24 V or 5 V TTL
Cycle / response Sub-millisecond, deterministic per encoder User-program, dependent on OB load

10. Measuring the Shaft Speed Correctly

The speed value used for the offset must be a rotational speed in 1/min, not a count rate. The FM 352 supports several measuring modes; the correct one for a labelling machine shaft is "Rotational speed measurement" with a configured gate time. The configuration is done in HW Config on the FM 352 properties dialog, under the "Measuring mode" tab. Assign the pulses per encoder revolution to the module so that the speed output is in engineering units.

The relevant settings are:

  • Mode: Rotational speed measurement
  • Gate control: internal (recommended for a continuous shaft) or external (recommended if the machine has a running signal that should enable the measurement)
  • Pulses per encoder revolution: match the encoder data sheet, e.g. 1024, 2048, 4096
  • Gate time T: 100 ms is a good starting point for a labelling machine; shorter T gives more noise, longer T gives slower response

For commissioning, write the live speed value to a VAT watch table and confirm that it tracks the machine HMI display before any of the compensation logic is enabled.

11. Commissioning Procedure

  1. Wire the dispense valve to a free FM 352 onboard output (e.g. output 0 of the FM 352). Disable the SM 322 channel that used to drive the valve and confirm the dispense command is now sourced only from the FM 352.
  2. Configure the FM 352 measuring mode as rotational speed with the encoder PPR and a 100 ms internal gate. Download the HW Config to the CPU.
  3. Create a watch table with PIW 256 (or the actual address from HW Config) and the working variable MW 100. Run the machine at low speed, then at high speed, and confirm the speed value is in 1/min and tracks the HMI.
  4. Add the speed-compensated offset code to OB1 as shown in section 6. Run the machine with a small bottle, watch the dispense, and verify visually that the label lands in the centre of the bottle at low speed and at high speed.
  5. At low speed, force MW 204 to the static start position; the dispense window should be correct. At high speed, remove the force and confirm the start is advanced by the computed N_off.
  6. Insert a label-after-label test: run a full batch at high speed, then at low speed, and confirm the first bottle has its label and no label drops after the last bottle.

12. Verification Checklist

Test Expected result Pass criterion
Static dispense at low speed (50 1/min) Label centred on bottle All bottles labelled, no lost labels
Static dispense at high speed (1500 1/min) Label trails the bottle Confirms the failure mode is speed-dependent
FM 352 onboard output with no compensation No process image jitter Latency is now dominated by valve mechanics, not CPU
FM 352 with computed N_off, 50 1/min MW 204 close to static start Compensation is small, label still centred
FM 352 with computed N_off, 1500 1/min MW 204 reduced by N_off Label centred on bottle at high speed
Step from 50 to 1500 1/min during a batch Smooth advance, no lost labels Compensation tracks the change in less than 200 ms
Stop the shaft with the valve enabled No additional dispense Cam dwell is bounded by the dispense window

13. Troubleshooting Matrix

Symptom Likely cause Fix
Label trails bottle only above a threshold speed Static shift position is correct at low speed but not at high Switch to FM 352 onboard output with speed-compensated cam start
Label trails bottle at all speeds Wrong cam end position; dispense window is too narrow Widen the cam dwell so the label can complete its travel
Double labels per bottle Cam end is later than the start of the next bottle in the register Add a one-revolution inhibit or shorten the dispense dwell
Compensation is too small at high speed Speed value is averaged over a long gate time Reduce the FM 352 gate time to 50 to 100 ms
Compensation overshoots, label leads the bottle Round-up of the offset formula, or t_v measured hot and used cold Re-measure t_v at operating temperature, clamp N_off to a max
Compensation updates too late, label still trails OB1 is too slow to push MW 204 into the FM 352 Use direct peripheral write of the setpoint word; or run the cam update in a faster OB
First bottle in a batch loses the label, last bottle gets a phantom The first/last bottle is at the edge of the shift register; the cap detect is not synchronised to encoder zero Trigger a one-shot on encoder zero; reset the shift register at zero

14. Tuning the Compensation

The two parameters in the offset formula that the field engineer can adjust are t_v and t_t. t_v is the solenoid opening time, which varies with temperature and supply voltage; measure it at 24 V and at the operating temperature, not at 24 V cold. t_t is the mechanical transit between the dispense nozzle and the bottle. Calculate t_t as the distance divided by the linear velocity of the bottle at the dispense point, where the linear velocity is r * omega and r is the radius of the dispense head.

For fine tuning, expose MW 204 as a VAT variable and add a manual offset MW 300 that the commissioning engineer can increment or decrement by 1 count at a time. The total correction at high speed is then N_off + MW 300. Trim MW 300 by watching a slow-motion video of the dispense event and stop adjusting when the label consistently lands within one bottle radius of the centre.

15. S7-300 Firmware and Compatibility Notes

The FM 352 cam controller is supported by STEP 7 V5.5 and the matching FM 352 configuration tool. CPU 315-2 DP in this article is at firmware V3.3, which is the highest firmware released for the 6ES7315-2AH14-0AB0. The FM 352 firmware is V2.0.0 on the cam controller referenced in this article; this firmware exposes 32 cam tracks and the rotational speed measurement described in section 10.

Migration path. The SIMATIC S7-300 is in the transition phase that Siemens has documented in the official S7 product family roadmap. New installations should use the S7-1500 platform, where the equivalent cam control function is the TO_Cam, TO_CamTrack, and TO_MeasuringInput technology objects in TIA Portal. The S7-300 / FM 352 procedure above is the right solution for an existing line that is being kept in service.

16. Worked Example

Take a labelling machine with the following parameters: encoder PPR = 1024, t_v = 0.008 s, t_t = 0.012 s, total mechanical latency t_v + t_t = 0.020 s, shaft speed range 100 to 1500 1/min.

Shaft speed n [1/min] N_off [counts] Static start [counts] Corrected start [counts]
100 34 500 466
300 102 500 398
600 205 500 295
1000 341 500 159
1500 512 500 -12 (clamp to 0)

At 1500 1/min the static start would have been advanced by 512 counts; the dispense window would have been completely missed. With the compensation the corrected start is 0 counts (clamped), and the cam end at the static window width still gives a usable dwell. If the window width is smaller than N_off, the cam start must clamp at the safe minimum and the dispensing problem has to be addressed mechanically (faster valve, shorter dispense head, or wider window).

17. Frequently Asked Questions

Why does the dispense fail only at high shaft speed when the shift register position is fixed?

The CPU 315 process image adds 10 to 25 ms of latency to the dispense command, plus the fixed 8 to 15 ms valve opening time. At low speed the lost encoder counts fit inside the dispense window, so the label still lands. At high speed the lost counts exceed the window, the label trails the bottle, and a second label fires on the next bottle of the batch.

Do I need an FM 352-5 Boolean processor, or can the FM 352 cam controller handle the dispense on its own?

The FM 352 cam controller has 8 onboard 24 V outputs that are switched directly by the encoder position, with sub-millisecond response. For most labelling lines the dispense valve can be moved to one of those 8 outputs and the FM 352-5 is not required. The FM 352-5 is only required if the FM 352 onboard outputs are all used by other cam tracks.

What measuring mode should be set on the FM 352 for this application?

Use the "Rotational speed measurement" mode with an internal gate of 100 ms. The pulses per encoder revolution must match the encoder data sheet, and the rotational speed is read as a signed 16-bit integer in 1/min. See the official FM 352 configuration page in the STEP 7 documentation.

How many encoder counts of advance are required at 1000 1/min with a 1024 PPR encoder and 20 ms total mechanical latency?

The formula is N_off = round( 0.020 * 1000 * 1024 / 60 ) = 341 counts. With a 1000 1/min speed, the dispense command has to fire 341 counts earlier on the encoder than the static low-speed position.

Can I just use direct peripheral access (T PQB 0) to remove the CPU latency?

Direct peripheral write removes the process image term and brings the CPU 315 latency down to about 0.05 microseconds per bit. It is a useful partial fix, but the dispense valve still has a fixed 8 to 15 ms opening time and the bottle has a fixed transit time. For a high-speed line the FM 352 cam path with speed-compensated offset is the correct solution.

Is the FM 352 still supported on a new installation, or should the line be migrated to S7-1500?

The S7-300 is in the phase of new shipments ending and the S7-1500 is the recommended platform. The FM 352 procedure in this article is the correct fix for an existing S7-300 line. For a new line, the equivalent functionality is the TO_Cam, TO_CamTrack, and TO_MeasuringInput technology objects in TIA Portal on the S7-1500 platform.

Back to blog