S7-1200 Closed-Loop Stepper Speed Control via PTO and Encoder

David Krause12 min read
S7-1200SiemensTechnical 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

Overview

A stepper motor is by definition an open-loop actuator: the drive issues a fixed number of pulses and the rotor is expected to advance by one electrical step per pulse. When pulse rate is stable, speed is stable; the only failure mode is a stall, which an incremental encoder cannot "rescue" because the rotor has already lost synchronism with the magnetic field. Despite that fundamental limit, an encoder on the shaft is still useful in three practical situations: detecting stall, holding the average speed constant against varying mechanical load, and replacing the stepper with a servo-class profile once torque margins are exhausted. All three are feasible on a SIMATIC S7-1200 CPU by combining the on-board Pulse Train Output (PTO) with a High Speed Counter (HSC) in frequency mode and a software PI corrector, or by offloading the loop to a dedicated closed-loop stepper drive or a real servo drive.

This reference consolidates the engineering decisions for the S7-1200 path: which CPU and signal-board to pick, how to wire an incremental encoder into the HSC, how to configure the PTO technology object in TIA Portal, how to scale and tune the velocity PI loop, and what verification tests prove the loop is closed. The official S7-1200 Motion Control documentation describes the PTO mechanism in detail at Stepper motor on the PTO (S7-1200).

Why Closed-Loop Control on a Stepper?

The open-loop assumption is only valid while the rotor can keep up with the commanded step rate. Once load torque approaches the available motor torque — typically above 70–80 % of the motor's pull-out curve — the rotor starts to lag and eventually stalls. An encoder alone cannot prevent stall in a pure stepper drive; once a step is missed the magnetic field has moved on. What an encoder can do is:

  • Detect stall quickly. A frequency mismatch between commanded pulse rate and measured pulse rate from the encoder is the cleanest stall indicator.
  • Hold average speed against friction or windage changes. A PI controller that compares setpoint frequency against HSC-measured frequency can trim the PTO velocity command by 5–15 % to compensate for load drift, without ever changing the underlying step resolution.
  • Detect missed steps. A position counter fed by the encoder Z pulse and by the PTO pulse counter diverges by the missing-step count; the controller can then trigger a controlled stop.
Engineering rule: If the application needs sustained torque above the stepper's pull-out curve, or requires dynamic accelerations that exceed stepper mechanical limits, switch to a real servo drive. A PI loop around a PTO cannot regenerate lost torque.

System Architecture

S7-1200 CPU PTO Technology Object HSC Frequency Mode PI Velocity Controller Stepper Drive PUL/DIR inputs Incremental Encoder A/B/Z, 24 V HTL Stepper Motor 2-phase, NEMA 23/34 Mechanical Load Conveyor / spool / wheel PI compares setpoint vs. HSC frequency and trims PTO velocity

Hardware Prerequisites

The S7-1200 platform supports the required I/O on most current CPU models. Selection is driven by how many high-speed channels you need and whether the CPU already has PTO channels on-board or requires a Signal Board.

Item Specification Notes
CPU SIMATIC S7-1200, firmware V4.2 or later (V4.4+ recommended for TIA V17/V18 motion object compatibility) 1211C/1212C/1214C/1215C/1217C supported; PTO available on all
PTO source On-board outputs Q0.0 (PUL) and Q0.1 (DIR) of the CPU, or DO Signal Board SB 1222 (e.g. 6ES7222-1AD30-0XB0) Two high-speed outputs per PTO channel; up to 100 kHz on CPU, 200 kHz on SB
Encoder input HSC channel 1–6, configured for frequency measurement 24 V HTL differential or single-ended; up to 100 kHz single-phase or 80 kHz quadrature
Stepper drive Pulse/direction input, optoisolated, 5–24 V Match drive input voltage to CPU output level (24 V typical)
Encoder Incremental, quadrature A/B with index Z, PPR chosen for control bandwidth 1000–2500 PPR common; higher PPR increases HSC update rate
Supply 24 VDC regulated, ≥ 2 A headroom for CPU + DO + encoder Use a Siemens SITOP or equivalent; share ground with drive supply return
The CPU outputs Q0.0/Q0.1 are push-pull on most S7-1200 CPUs and source current up to 0.5 A, which is sufficient to drive an optocoupler input on most stepper drives. For long cable runs, use shielded twisted pair and a differential receiver on the drive side.

Encoder Selection and Scaling

The HSC samples the encoder pulses over a configurable integration window. For a speed loop with cycle time T_c of 10 ms, the resolution of the frequency measurement is:

f_resolution = 1 / T_window

At T_window = 100 ms you get 10 Hz resolution. Choose the encoder PPR so that the worst-case operating speed produces at least 1000 counts per window. For example, a 1000 PPR encoder on a 600 RPM shaft produces 6000 Hz at top speed, comfortably above the threshold. The measured frequency in Hz from HSC is:

f_hz = encoder_counts / T_window

Convert to mechanical RPM:

n_rpm = 60 × f_hz / PPR

And to stepper-step equivalents (so the loop operates in the same units as PTO):

f_step_eq = f_hz × (PPR_stepper / PPR_encoder)

Keep this scaling factor k as a tag constant; it is the same multiplier referenced in the field-proven algorithm ERROR = k * A − B used to align commanded velocity with measured velocity.

PTO Output Configuration in TIA Portal

Configure the pulse generator as a Technology Object of type "PTO". TIA Portal handles the underlying PWM generator and the hardware-specific routing automatically.

  1. Add a new technology object under Technology objects > Add new object > Motion Control > TO_PulseGenerator (or use the axis-based "TO_SpeedAxis" if you want pre-built ramping).
  2. Select the signal source: Onboard CPU or Signal Board. For CPU outputs choose Q0.0 (pulse) and Q0.1 (direction).
  3. Set the maximum output frequency to the drive's input limit. Typical stepper drives accept 25 kHz, 50 kHz, 100 kHz, or 200 kHz; do not exceed the drive rating.
  4. Set the pulse/direction output mode to PTO (pulse A, direction B); the S7-1200 outputs a 50 % duty-cycle pulse train.
  5. Enable the configured output with MC_Power (axis variant) or CTRL_PTO (legacy variant), and start the train with MC_MoveVelocity or by writing to <PTO>.Frequency.
  6. Wire the pulse output to the drive PUL+ terminal and direction output to DIR+; tie PUL− and DIR− to the CPU M terminal (24 V common).
The S7-1200 PTO does not natively accept an in-line correction from a user program while the pulse train is running; you must stop the train, change the frequency setpoint, and restart, OR you must operate the pulse generator at a fixed high rate and modulate direction/duty-cycle yourself. A cleaner approach is to drive the velocity setpoint as a tag that the OB1 cyclic interrupt reads every 10 ms and writes into <PTO>.FrequencySetpoint via MC_MoveVelocity in "change on-the-fly" mode where supported, or by re-issuing the move block each cycle.

High Speed Counter in Frequency Mode

The HSC hardware on the S7-1200 supports four operating modes. Frequency mode is the one you want.

HSC mode Counts Use for this project?
Single-phase count Edge events No
Two-phase count (quadrature) Position Optional, only if you also need absolute position
Frequency measurement Counts per integration window Yes
Pulse width modulation (PWM) Duty cycle No

Add a Technology Object of type TO_HighSpeedCounter, set operating mode to Frequency, and configure the integration window to 100 ms (10 Hz update, 10 Hz control bandwidth). For a tighter loop, drop to 50 ms but expect more quantisation noise on the encoder signal. Configure the encoder input to be one of the supported HSC channels (I0.0–I0.5 on most CPUs).

The measured value is exposed at <HSC>.MeasuredValue in Hz. Read it in OB1 every cycle.

Closing the Loop with a PI Controller

The fundamental control law, as field-tested on stepper PTO systems, is:

ERROR = Setpoint_speed − Measured_speed

Output_freq = Setpoint_freq + (Kp × ERROR + Ki × ∫ERROR dt)

Implementation in Structured Text using the S7-1200 PID_Compact function block is overkill for a simple velocity trim; a hand-rolled PI in OB1 is more transparent.

// Velocity PI for stepper PTO — runs in OB1 (cyclic) every 10 ms
VAR
    fSetpointHz   : REAL; // commanded speed in Hz from HMI/tag
    fMeasuredHz   : REAL; // HSC frequency in Hz
    fError        : REAL;
    fIntegral     : REAL;
    fOutputHz     : REAL;
    fKp           : REAL := 0.8;
    fKi           : REAL := 0.05;
    fLastUpdate   : TIME;
END_VAR

fMeasuredHz := HSC1.MeasuredValue;
fError      := fSetpointHz - fMeasuredHz;

fIntegral   := fIntegral + (fError * fKi);
// Anti-windup clamp
IF fIntegral >  500.0 THEN fIntegral :=  500.0; END_IF;
IF fIntegral < -500.0 THEN fIntegral := -500.0; END_IF;

fOutputHz   := fSetpointHz + (fError * fKp) + fIntegral;

// Clamp to drive limits
IF fOutputHz > 100000.0 THEN fOutputHz := 100000.0; END_IF;
IF fOutputHz <     10.0 THEN fOutputHz :=     10.0; END_IF;

// Hand the corrected frequency to the PTO
PTO1.FrequencySetpoint := REAL_TO_UDINT(fOutputHz);

Tuning procedure for the loop:

  1. Disable the integrator (Ki = 0). With Kp = 0.5, command a step from 0 to 30 % of max speed and observe the encoder-derived frequency on a trace.
  2. Increase Kp until you see a small overshoot (5–10 %) on a step change; back off 30 %.
  3. Enable the integrator at 10 % of Kp and increase slowly until steady-state error is removed in < 500 ms.
  4. Apply a load step (e.g., a brake applied to the driven shaft) and verify recovery in < 1 s with no oscillation.
A pure stepper motor maintains commanded speed as long as it does not stall. The PI loop only matters during load transients and at low speeds where microstepping distortion appears. If the trace shows < 1 % speed deviation under load, you may not need the integrator at all; the proportional term alone is sufficient.

Alternative 1: Closed-Loop Stepper Drive (Recommended for Most Applications)

A dedicated closed-loop stepper drive (also called "step-servo" or "closed-loop stepper") accepts step/direction pulses exactly like a normal stepper drive, but adds an onboard encoder on the motor and an internal torque/velocity loop. From the S7-1200's perspective, nothing changes — the PTO still issues pulse/direction, and the drive guarantees that the rotor follows. The drive exposes an alarm output (open-collector or relay) that the PLC can read on a standard DI for fault handling.

This is the recommended architecture whenever the load is variable or unpredictable. The S7-1200 code reduces to open-loop control plus alarm handling, which dramatically simplifies commissioning and eliminates PI tuning.

Aspect Software PI on S7-1200 Closed-loop stepper drive
Stall recovery Detected, not corrected Corrected by drive
Code complexity PI block + scaling + alarms Pulse/direction only
Tuning time Hours Drive auto-tunes
Cost Lowest hardware cost Drive + motor ~30 % more
Torque margin Unchanged Up to 50 % higher continuous torque
Best for Steady load, low speeds, R&D lab Variable load, production machines

Alternative 2: Real Servo Drive

If the mechanical requirements exceed what a stepper — closed-loop or otherwise — can deliver (high acceleration, high peak torque, short settling time), step up to a SINAMICS V90 or similar servo drive. The S7-1200 PTO can still drive the V90's pulse input, and the V90 has its own internal position/velocity/torque cascade with an encoder on the motor. The PTO bandwidth on the S7-1200 (200 kHz with SB 1222) supports up to 6000 RPM on a 2500-line encoder in pulse/direction mode, which covers the vast majority of micro-servo applications.

For applications above ~6000 RPM or with extensive electronic gearing, move to PROFINET-based control with a SINAMICS S210 or V90 PN, which exposes the standard TO_SpeedAxis objects directly to TIA Portal.

Commissioning and Verification

  1. Open-loop sanity check. Disconnect the PI output and command a fixed frequency. Verify with a handheld tachometer or the encoder that motor RPM matches the expected value: RPM = f_pulse × 60 / (PPR_stepper × microstep_factor).
  2. Encoder sign check. Run the motor in positive direction, trace HSC frequency, confirm positive sign. Reverse A/B wires if negative.
  3. PTO update check. Trace PTO1.FrequencySetpoint and PTO1.ActualFrequency on a TIA trace. The actual should follow the setpoint within one cycle.
  4. HSC update check. Trace HSC1.MeasuredValue. The value should update every integration window (100 ms by default).
  5. Closed-loop step response. Apply a 10 % step to the setpoint with the PI enabled, trace fMeasuredHz and fOutputHz. Overshoot should be < 10 %, settling time < 500 ms.
  6. Load disturbance rejection. Apply a mechanical load step (e.g., a brake clamp). The PI should recover speed within 1 s with no sustained oscillation.
  7. Stall detection. Manually stall the shaft by holding it. The HSC frequency should drop to 0 while the PTO continues pulsing; this is the diagnostic condition for raising a fault.

Wiring Reference

S7-1200 CPU Q0.0 PUL+ Q0.1 DIR+ M GND I0.0 A+ I0.1 B+ I0.2 Z+ M GND Stepper Drive PUL+ DIR+ GND Encoder A+ B+ Z+ GND Stepper Motor + Encoder A+ Phase A B+ Phase B ENC A+ ENC B+ ENC Z+

Troubleshooting Matrix

Symptom Likely cause Action
Motor runs, encoder frequency reads 0 Encoder wiring swapped; HSC not in frequency mode; encoder power missing Verify 24 V at encoder; check HSC mode = Frequency; verify A/B not swapped
PI oscillates Ki too high; integration window too short Halve Ki; increase HSC window to 200 ms
Motor stalls under load Insufficient torque margin Reduce mechanical load, increase supply voltage, switch to closed-loop stepper drive
PTO frequency setpoint ignored PTO not powered (MC_Power); frequency written to wrong tag Check <PTO>.StatusBits.PowerOn; verify tag is FrequencySetpoint not Frequency
Speed reads half expected Operating in quadrature but counting only one edge Set HSC evaluation to 4x (quadrature x4) or check whether pulses are A only
HSC counts erratic Noise on encoder lines; missing shield ground Use shielded cable, ground shield at PLC end only, add 10 kohm pull-down on A/B if open-collector

Frequently Asked Questions

Can the S7-1200 truly close a velocity loop on a stepper motor?

Yes, but only for slow disturbance rejection and stall detection. The S7-1200 PTO generates pulse/direction, and a software PI in OB1 reads HSC frequency and trims the setpoint at 10–100 Hz. For stall recovery or high dynamic loads, use a closed-loop stepper drive or a real servo.

Which S7-1200 CPU model and firmware do I need?

Any current S7-1200 CPU (1211C through 1217C) with firmware V4.2 or later supports PTO and HSC in frequency mode. Firmware V4.4 or later is recommended for TIA Portal V17/V18 motion object compatibility and for the 200 kHz Signal Board option.

What HSC integration window should I use for the speed loop?

100 ms (10 Hz update) is a good starting point for conveyors and spools. Drop to 50 ms if your mechanical response is faster than 200 ms; raise to 200 ms if the encoder PPR is low and quantisation noise is visible on the trace.

Do I need a Signal Board or can I use the on-board Q outputs?

On-board Q0.0 and Q0.1 work for pulse/direction on all S7-1200 CPUs and support up to 100 kHz. Use a Signal Board SB 1222 (e.g., 6ES7222-1AD30-0XB0) only if you need 200 kHz or if Q0.0/Q0.1 are already used for another high-speed function.

Why not just use a SINAMICS V90 servo drive instead?

If your mechanical requirements (acceleration, peak torque, settling time) exceed what a stepper can deliver, switch to a SINAMICS V90 with PTO pulse input, or to a PROFINET-connected V90 PN / S210 for full TIA Portal motion object integration. The PI loop and HSC then disappear because the drive handles everything internally.

Back to blog