S7-1200 Closed-Loop Position Control with Quadrature Encoders

David Krause12 min read
Motion ControlSiemensTechnical Reference
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. Overview: The S7-1200 Closed-Loop Position Problem

The SIMATIC S7-1200 CPU family (firmware V4.0 through V4.6, configurable in TIA Portal V15 through V21) ships with an integrated Motion Control engine that supports pulse-train output (PTO) axes, analog ±10 V speed-controlled axes, and PROFINET/PROFIdrive axes. However, the technology object TO_PositioningAxis in PTO and analog mode is fundamentally an open-loop controlled axis: the axis commands a velocity or pulse stream and assumes the mechanical load follows. When the connected servo drive operates in pure velocity or torque mode (for example, a Lenze 9300 or similar inverter with a SinCos resolver but no internal absolute-position tracking), the position loop must be closed inside the PLC.

This article documents the canonical architecture for closing that loop on an S7-1200:

  1. Route the driver's buffered encoder image (TTL/HTL quadrature) into an S7-1200 high-speed counter (HSC).
  2. Read the HSC count as the actual position value (PV).
  3. Run a PID_Compact block that compares PV against the motion setpoint.
  4. Output the corrective velocity as either a PTO pulse/direction stream or an analog ±10 V reference.

This architecture is a user-logic closed loop. It is not equivalent to a true onboard position controller and carries distinct precision, bandwidth, and stability limitations that are quantified later in this document.

2. Prerequisites

Component Specification
CPU S7-1200 — CPU 1214C DC/DC/DC, 1215C DC/DC/DC, or 1217C DC/DC/DC (4 PTO outputs / 6 HSC channels minimum)
Firmware V4.4 or later (V4.2 minimum for full HSC filter range; V4.0 lacks some PID_Compact features)
Engineering TIA Portal V18 or V21 with installed S7-1200 HSP
Encoder source TTL/HTL quadrature (A, B, Z) from driver buffered output, 24 V single-ended or RS-422 differential
Servo drive Velocity or torque mode, pulse+direction input or ±10 V analog input, with encoder-emulation output
Signal conditioning RS-422 line driver (e.g., 6SL3055-0AA00-5BA3 signal adapter) for long cable runs, or shielded twisted pair for short runs

3. Why the S7-1200 Motion Engine Is Open-Loop in PTO Mode

Per the TIA Portal documentation, when you configure a positioning axis with PTO, the axis technology object operates without encoder feedback as its primary position reference. The "Configuration – Encoder" page is only relevant when the axis is a PROFINET/PROFIdrive axis pointing at a SINAMICS drive, or when you enable a software-based actual value from an HSC for monitoring. There is no automatic closed-loop position servo inside the technology object for PTO outputs.

The HSC can still be used to measure position for external user logic, and that is exactly the path this article describes. The configuration entry Encoder on High-Speed Counter (HSC) in the technology object's basic parameters links a configured HSC to the axis for monitoring and error-detection purposes, but it does not close a position loop automatically.

4. HSC Channel Allocation and Maximum Input Frequency

Each CPU model provides a fixed HSC resource pool. The following table lists the supported counters, their default assignment, and their maximum input frequency as defined by the S7-1200 system manual, edition 09/2024.

CPU Available HSCs 24 V Single-Ended Max Freq. RS-422 Differential Max Freq. Default Pin Assignment (HSC1 example)
1211C / 1212C 3 (HSC1–HSC3) 100 kHz 1 MHz I0.0 (A), I0.1 (B), I0.3 (Z)
1214C 6 (HSC1–HSC6) 100 kHz 1 MHz I0.0 (A), I0.1 (B), I0.3 (Z)
1215C 6 (HSC1–HSC6) 100 kHz 1 MHz I0.0 (A), I0.1 (B), I0.3 (Z)
1217C 6 (HSC1–HSC6) 100 kHz 1 MHz I0.0 (A), I0.1 (B), I0.3 (Z)
Critical wiring note: Differential RS-422 inputs require the corresponding onboard differential receiver to be enabled in the device configuration. Single-ended 24 V wiring to a differential input will not produce reliable counts above 30 kHz. Confirm the driver's encoder-emulation output voltage level before wiring.

5. Configuring the HSC for Quadrature with 4x Evaluation

Open the S7-1200 device configuration in TIA Portal, expand Properties → High-speed counters (HSC), and configure the selected HSC as follows:

Parameter Value Reason
Enable true Activates the HSC channel
Counting mode Count continuously Required for closed position loop; avoids roll-over overflow
Counting direction / phase 4-fold (A/B quadrature, both edges) Doubles resolution vs. 2-fold and detects direction
Initial count value 0 (or set by homing sequence) Reference for absolute moves after homing
Input filter A 0.1 ms (low noise) to 1 ms (high noise) Filter time directly limits maximum count rate. The maximum reliable input frequency equals 1 / (2 × filter time).
Input filter B Same as filter A Symmetric filtering avoids quadrature skew
Input filter Z (home) 0.1 ms Z pulse is single-shot; shortest filter recommended

After configuration, the HSC's count value is accessible in user logic via the tag {<PLC>}_HSC{1..6}.Count or, more commonly, via the CTRL_HSC instruction's CV output for direct readback.

6. The PID_Compact Closed-Loop Position Block

The PID_Compact instruction (Firmware V4.0+) provides the standard S7-1200 PID regulator. Wire it as follows:

PID_Compact Terminal Source Tag Notes
Setpoint (Input) Axis target position (pulse units) Connect to positioning axis Position tag
Input (PV) {<PLC>}_HSC{1..6}.Count Actual position from quadrature HSC
Output (LMN) Velocity reference to drive Scaled 0–100%
ManualEnable FALSE (or jog condition) Hold PID control of output
Limit_High / Limit_Low +100.0 / -100.0 Match axis dynamics

Initial tuning starting point (conservative):

  • Gain Retain.Ctrl.Gain = 0.20
  • Integral time Retain.Ctrl.Ti = 2,000 ms
  • Derivative time Retain.Ctrl.Td = 0 ms (disable for first pass)
  • Sample time Retain.Ctrl.SampleTime = 100 ms

The PID cycle time of 100 ms sets an absolute ceiling on achievable closed-loop bandwidth: at 100 ms, the theoretical crossover frequency is approximately 1.5 Hz. Higher sample rates (down to the OB1 cycle) improve bandwidth but load the CPU.

7. Converting PID Output to Drive Reference

7.1 Analog ±10 V Output (preferred)

For the cleanest closed-loop behavior, send the PID output as an analog speed reference. Wire the SM 1232 AO module output (or the CPU onboard AO on 1215C/1217C) to the drive's analog speed input:

  • PID output -100.0% → 0 V (full reverse)
  • PID output 0.0% → 5 V (zero speed, on 0–10 V scale) or 0 V (on ±10 V bipolar scale)
  • PID output +100.0% → 10 V (full forward)

Use SCALE or NORM_X / SCALE_X instructions to map the -100.0 to +100.0 PID output to the integer AO range.

7.2 Pulse + Direction Output

When the drive only accepts pulse+direction, the S7-1200 PTO hardware is the only native source. The TO_PositioningAxis with PTO controls pulse frequency, not PID output. You therefore cannot directly substitute PID output for the axis setpoint. Two workable patterns exist:

Pattern Implementation Caveat
PTO controlled by MC_MoveVelocity with override Use MC_MoveVelocity with VelocityOverride input driven by PID output VelocityOverride accepts 0–200%; combine with direction tag set by PID sign
PTO controlled by MC_MoveRelative at fixed increment + direction Issue small relative moves at high frequency proportional to PID output Inefficient; loses smooth velocity control

The first pattern is the field-proven approach. Use a single PTO axis in velocity mode, drive its VelocityOverride from the PID output, and drive its Direction tag from a comparator of PID sign. Pulse+direction output requires careful verification of the drive's input voltage level (Lenze 9300 manual page 156 lists 5 V differential as the default; the 24 V single-ended option requires the X5 terminal block configuration).

Signal level warning: S7-1200 PTO outputs are 24 V single-ended push-pull by default. If the drive expects 5 V TTL differential, insert a level translator (e.g., Phoenix Contact MINI MCR or Siemens 6AG1223-1BH32-2XB0 signal board) or wire through a 24 V differential line driver like the AM26LS31.

8. Application Example: PID_Compact + MC_MoveVelocity

Sample Structured Text (SCL) fragment implementing the closed-loop position controller on top of an open-loop PTO axis:

// Inputs
Axis_REF        : TO_PositioningAxis;   // PTO axis
HSC_Count       : DINT;                 // from CTRL_HSC.CV
TargetPosition  : LREAL;                // in HSC counts
PID_Compact_0   : PID_Compact;          // background instance

// Velocity override limit 0..100% mapped from PID output
IF PID_Compact_0.Output > 100.0 THEN
    VelOvr := 100.0;
    Dir    := 1;
ELSIF PID_Compact_0.Output < -100.0 THEN
    VelOvr := 100.0;
    Dir    := -1;
ELSE
    VelOvr := ABS(PID_Compact_0.Output);
    Dir    := SGN(PID_Compact_0.Output);
END_IF;

// Clamp setpoint into integer velocity override (axis expects 0..200)
VelOvrInt := REAL_TO_INT(VelOvr * 1.0);

// Issue continuous velocity motion
MC_MoveVelocity(
    Axis       := Axis_REF,
    Execute    := TRUE,
    Velocity   := 50.0,                  // baseline 50% setpoint
    Direction  := Dir,
    VelocityOverride := VelOvrInt,
    CurrentDirection := 0,
    Mode       := 0
);

This pattern keeps the axis in continuous-velocity mode and uses the PID output to modulate the velocity rather than commanding absolute position. The HSC provides real actual value; the PID closes the position loop in user logic.

9. Commissioning and Verification

  1. Encoder wiring test: Manually rotate the load by hand. Monitor the HSC count via a watch table. The count must increment/decrement smoothly and match the expected quadrature-4x count per mechanical revolution.
  2. PID polarity check: Command a small setpoint offset (e.g., 1000 counts above actual). The PID output must drive the axis in the correct direction to reduce the error. Reverse Gain sign if the axis runs away.
  3. Step response: Apply a step setpoint of 10,000 counts (≈ one motor revolution for a 2500-line encoder with 4x eval). Record actual vs. setpoint in a trace. Verify overshoot < 5% and settling time within application requirement.
  4. Steady-state error: Hold setpoint constant for 60 s. With integral action enabled, steady-state error must converge to < 1 count.
  5. Anti-windup validation: Command a saturated output (e.g., -100%) for 10 s. When the error reduces, the integral term must not overshoot catastrophically; the drive must decelerate smoothly.

10. Precision and Bandwidth Limitations

The user-logic closed loop on S7-1200 has quantifiable limits. Use the following calculation to estimate system accuracy before committing to a design:

Error Source Magnitude Mitigation
HSC quantization 1 count = 360° / (encoder PPR × 4) Use higher PPR encoder; 4x eval is mandatory
HSC filter delay 1 × filter time (e.g., 0.1 ms = 0.1 ms lag) Reduce filter; validate with scope
PID sample time 100 ms typical → ~1.5 Hz bandwidth Lower sample time; raise CPU load impact
PTO frequency granularity 1 Hz at 1 MHz PTO output = 0.0001% velocity step Acceptable for most applications
Mechanical backlash Application-dependent, often 0.05–0.5 mm Add backlash compensation in user logic (reverse direction offset)
Drive velocity loop lag 1–10 ms typical for a velocity-mode servo Select drive with high-bandwidth current/velocity loop

Worked example: A 2500-line incremental encoder with 4x evaluation gives 10,000 counts/rev. With a 10 mm/rev leadscrew, one count equals 1 µm. The HSC filter of 0.1 ms adds 0.1 ms of position-measurement lag. The PID running at 100 ms limits achievable position bandwidth to roughly 1.5 Hz. Under steady state with integral action engaged, position accuracy is ±1 count (±1 µm) plus any mechanical backlash. Under dynamic motion, expect overshoot of 2–5% and settling time of 0.5–2 s depending on load inertia and PID tuning.

11. When to Upgrade to S7-1500 Instead

If the application requires any of the following, the S7-1200 user-logic loop will not meet specification:

  • Position loop bandwidth above 10 Hz
  • Synchronized axes (electronic gearing, camming)
  • Sub-millisecond position sampling
  • Native PROFIdrive closed-loop control with SINAMICS
  • Tolerance of dynamic disturbances (load impact, vibration)

The S7-1500 / ET 200SP CPU 1515SP supports the TO_PositioningAxis technology object with native closed-loop position control, optional TO_SynchronousAxis for camming, and PROFIdrive / PROFINET IRT for deterministic drive communication. The S7-1500T adds advanced motion functions including cam profiles and cross-PLC synchronization.

12. Troubleshooting Matrix

Symptom Likely Cause Diagnostic Corrective Action
HSC count stays at 0 while motor rotates Wiring or input filter mismatch Scope A/B channels at PLC terminal Verify 24 V differential vs. single-ended configuration; reduce filter time
PID drives axis away from setpoint Reversed PID gain sign Watch PID output sign while error is positive Negate Gain; or swap HSC A/B wiring
Continuous oscillation around setpoint Gain too high or sample time too long Trace Setpoint vs. Actual Reduce Gain by 50%; enable Derivative with small Td (50–100 ms)
Steady-state offset remains Integral action disabled or anti-windup locked Inspect PID_Compact.Retain.Ctrl.Ti Set Ti = 1,000–2,000 ms; confirm ManualEnable = FALSE
Drive fault on enable (overcurrent) PID output saturated before motion starts Check Output with no setpoint Reset integrator (sRet = iRet = 0) on MC_Power rising edge
Lost position after power cycle No absolute encoder; HSC initial value is 0 Check homing sequence Implement homing routine using Z pulse and limit switches; store position retentive if drive permits

13. Field Commissioning Notes

  • Homing: Without an absolute encoder, the position reference is lost on every power cycle. Implement a homing sequence that drives the axis to a limit switch at slow speed, captures the Z pulse, and resets the HSC count to a known reference.
  • Retentive position: If the axis must remember a coarse position across power cycles without full rehoming, store the last HSC count in a retentive DB. On power-up, validate against a single Z-pulse detection before resuming closed-loop operation.
  • CPU load: PID_Compact running at 50 ms sample time on a CPU 1214C adds approximately 8–12% CPU utilization. Verify CPU load remains < 70% after enabling the loop.
  • EMC: Route encoder and pulse signals in shielded twisted pair with shield grounded at the PLC end only. Keep encoder cable physically separated from motor power cables by at least 200 mm.
  • Drive enable sequencing: Always reset the PID integrator (zero sRet and iRet) on the rising edge of MC_Power. Otherwise the integrator may carry a stale value from before enable and slam the axis at full speed on the first cycle.

Can the S7-1200 close a position loop natively without user logic?

No. The TO_PositioningAxis technology object in PTO and analog modes is an open-loop controlled axis. True closed-loop position control with encoder feedback requires either a SINAMICS drive over PROFINET/PROFIdrive, or the S7-1500 platform with its native closed-loop positioning axis object.

What is the maximum HSC input frequency on S7-1200?

Up to 100 kHz for 24 V single-ended signals and up to 1 MHz for RS-422 differential signals, per the S7-1200 system manual. The input filter time further limits the maximum count rate to 1 / (2 × filter_time). A 0.1 ms filter therefore allows a maximum count rate of 5 kHz on 24 V inputs.

How do I convert the PID output into pulse frequency for a pulse+direction drive?

The cleanest approach is to keep the axis in MC_MoveVelocity mode and drive the VelocityOverride input from the absolute PID output while setting Direction from the PID output sign. The PTO hardware then generates the pulse train at the correct frequency and direction without manual pulse generation in user logic.

What bandwidth can I realistically achieve with this user-logic closed loop?

With PID_Compact running at a 100 ms sample time, the theoretical closed-loop crossover is approximately 1.5 Hz. Reducing the sample time to 20 ms raises this to around 7 Hz. Beyond that, you must move to S7-1500 where the technology object supports sub-millisecond cycle times and PROFIdrive IRT communication.

Do I need a Z (index) pulse for homing, or can I use only limit switches?

You can home on a limit switch alone for a coarse reference, but the resulting position is repeatable only to within one count of HSC uncertainty. The Z pulse provides a single-pulse-per-revolution reference that gives sub-count repeatability. For precision applications (< 5 µm), use Z pulse plus one limit switch; the limit switch establishes coarse direction and the Z pulse gives the fine reference.

Back to blog