IMDHMIControls LibraryShop main store

How-to

Why digital input counting misses fast encoder pulses

PLC programming Published 7 min read

Short answer

A STEP 7 counter fed from an ordinary digital input counts only the edges the scan observes, so a fast encoder outruns it and one axis can fail while an identical one works. On an S7-313C the documented fix is the CPU 31xC integrated technological counter, configured in the hardware configuration, with the pulse rate checked against the input rating. Worked check: 25 r/s times 1024 pulses per revolution gives 25.6 kHz, inside the rating.

On this page
  1. Why a counter rung on a digital input drops edges
  2. What the S7-313C documents for counting
  3. Worked example: a 1500 r/min motor and a 1024-pulse encoder on a CPU 313C
  4. Configure the counter and scale the count
  5. Steps to move encoder counting off the scan
  6. Verify each axis over a known travel
  7. Questions asked

A counter rung fed from an ordinary digital input cannot count a fast encoder. The scan reads its inputs once per cycle, so every edge that arrives and clears between two input reads never reaches the counter logic, and the missed fraction grows as the pulse rate climbs. On an S7-313C the documented fix is to stop scanning the pulses: the CPU 31xC compact family carries integrated technological counters that acquire edges in hardware, and the program reads the finished count each cycle. Before configuring anything, put a number on the signal — motor speed in revolutions per second times pulses per motor revolution — and set that rate against the counter input's documented rating. This article works the arithmetic on a concrete example, walks through the configuration the CPU 31xC Technological Functions manual specifies, and covers the scaling and per-axis verification that make the count trustworthy.

Why a counter rung on a digital input drops edges #

The cyclic scan hands the program a snapshot of the inputs once per cycle. An encoder pulse that rises and falls between two snapshots — its period shorter than the scan time — never exists as far as the logic is concerned. A counter block driven by that input increments only when a snapshot lands on an edge, so the count comes out low by a fraction that grows with pulse frequency and scan length. No cleaner signal or tighter wiring changes this; it is how cyclic scanning works.

The case behind this question shows the structure of the fault rather than a wiring defect: the same program counts one axis of a two-motor machine and drops the other, while both encoder connections test good. Two identical axes rarely run at the same speed, so the failing channel is simply the one whose pulses arrive faster than the scan samples. The report that the encoder resolution is 30 times the motor's cannot settle the rate by itself, because it does not say whether that means pulses per motor revolution, pulses per encoder revolution, or counts after edge multiplication; the speed measurement and the encoder datasheet settle it.

For an isolated short pulse, such as a limit switch that blips for less than one scan, the scan-side remedies are real, and why a PLC misses short input pulses and how to catch them covers them. A pulse-catch input is hardware assistance for the same sampling gap, and it has limits of its own — why a pulse-catch input on a small PLC can still miss a fast sensor signal works through one such case. A continuous encoder train is a different job: no filter or catch window scales to thousands of edges per second, so the right tool is a counter that watches the wire in hardware.

What the S7-313C documents for counting #

The S7-313C belongs to the CPU 31xC compact family, which carries counting inside the CPU. The SIMATIC S7-300 CPU 31xC and CPU 31x Technical Specifications manual lists three integrated counters and three frequency meters for it, with inputs rated up to 30 kHz; the frequency-measurement operating parameters in the CPU properties agree, spanning 1 mHz to 30,000,000 mHz on a CPU 313C and 1 mHz to 60,000,000 mHz on a CPU 314C. A counter input rated like this increments on the wire, independent of the scan: the hardware tracks every edge up to its rating, and the program reads the accumulated value and does its comparisons once per cycle. The SIMATIC S7-300 CPU 31xC Technological Functions manual specifies the counting modes, the gate control and the operating parameters, which live in the CPU's properties in the STEP 7 hardware configuration.

Worked example: a 1500 r/min motor and a 1024-pulse encoder on a CPU 313C #

Inputs. Motor speed 1500 r/min and 1024 pulses per motor revolution are example values: the reported project never measured its speed, and "resolution 30 times the motor's" does not define a rate, so these stand in for a typical test speed and a common incremental encoder. Distance per motor revolution 10 mm and a fixed travel of 250 mm are example mechanics for a 10 mm pitch ball screw and a commanded index move. The limit is not an example: it is the CPU 313C's documented counter input rating from the specifications manual.

Rule. Pulse rate in pulses per second equals motor speed in revolutions per second times pulses per motor revolution. Counts over a fixed travel equal the travel divided by the distance per motor revolution, times pulses per motor revolution.

Arithmetic:

  • 1500 r/min ÷ 60 s/min = 25 r/s
  • 25 r/s × 1024 pulses/rev = 25,600 pulses/s = 25.6 kHz
  • 250 mm ÷ 10 mm/rev = 25 revolutions per move
  • 25 revolutions × 1024 pulses/rev = 25,600 pulses per 250 mm
  • 1 s ÷ 25,600 ≈ 0.000039 s, so one edge arrives about every 39 microseconds

Result. A 25.6 kHz pulse train carrying 25,600 pulses for the 250 mm move, with single-edge evaluation. A quadrature interface evaluated on four edges multiplies the count to 102,400 without changing the frequency on the wire.

Decision. At 25.6 kHz, under the documented 30 kHz rating, the CPU 313C's integrated counter acquires every edge, while a scan-driven rung cannot promise to observe a pulse that lives about 39 microseconds when scans run in the millisecond range. Above 30 kHz the same arithmetic disqualifies the 313C counter and moves the decision to a CPU 314C, whose rating is twice as high, or to a dedicated counter module sized to the rate. This is exactly the shape of the reported fault: the failing axis outruns the scan even with verified wiring.

Configure the counter and scale the count #

In the STEP 7 hardware configuration, open the compact CPU's parameters, enable the counting function on the integrated counter inputs, and set the operating parameters the Technological Functions manual describes: counting mode, gate control and the input limits. Move the encoder wires to the terminals the manual assigns to the integrated counter, not to the general-purpose digital inputs the scan reads. The program's job then shrinks to reading the hardware count once per cycle, capturing the value at the start of a move, forming the difference, and comparing it with the counts the commanded travel should produce.

Scaling the count into machine units uses the conversion already worked above: 25,600 pulses per 250 mm of travel, from pulses per revolution and distance per revolution. Converting a raw pulse count into commanded units is the same arithmetic behind how to calculate step and direction servo pulse scaling, and the worked method on that page transfers directly to an encoder count. Keep pulses and decoded counts distinct whenever the configured mode evaluates more than one edge per pulse period, because the mode multiplies counts while the wire frequency stays put.

Steps to move encoder counting off the scan #

  1. Measure the axis's top motor speed in r/min and read the encoder's documented pulses per motor revolution from its datasheet; note whether the counting mode evaluates one edge or several, because edge evaluation multiplies counts but not wire frequency.
  2. Convert the rate: divide the speed by 60 for revolutions per second, then multiply by pulses per revolution. If the result exceeds the counter input's documented rating for your CPU, choose hardware that covers the rate before wiring anything — a CPU with a higher-rated counter or a dedicated counter module.
  3. In the STEP 7 hardware configuration, enable the counting function on the integrated counter of the CPU 31xC and set its operating parameters per the Technological Functions manual.
  4. Move the encoder wiring to the terminals assigned to the integrated counter.
  5. Compute counts per fixed distance: fixed distance divided by distance per motor revolution, times pulses per motor revolution, adjusted for the edge evaluation the configured mode uses.
  6. In the program, read the hardware-managed count, subtract the value captured at the start of the move, and compare the difference with the counts per fixed distance.
  7. Verify each axis over the same controlled travel before trusting either count.

Verify each axis over a known travel #

Run each motor over the fixed travel from the scaling arithmetic and record the hardware count. Compare the two axes only after equal travel, comparable speed, the same encoder type and the same counting mode; a difference in any of those makes the comparison meaningless. If the configuration matches and one axis still miscounts, the remaining suspects are that channel's assigned counter and configured signal source, the actual rate on its wire, and the mechanical ratio — not the scan, and not the connection the bench test already proved good.

For the first verification run, jog at reduced speed with the machine in a state where a wrong position cannot strike anything; the count is unproven until the travel check passes.

Questions asked

Why does only one of two identical encoder axes count correctly?

Identical logic on two axes points at pulse rate, not wiring. Each axis runs at its own speed, so one channel's pulse period can still be longer than the scan while the other's falls between two input reads and disappears. A passing connection test measures continuity, not rate. Measure each axis's top speed, compute pulses per second from the encoder's documented pulses per revolution, and only then decide whether hardware needs changing.

How fast can the integrated counter input of a CPU 313C count?

The technical specifications manual for the compact CPUs rates the 313C's integrated counting and frequency-measurement inputs up to 30 kHz, and the frequency-measurement operating parameters span 1 mHz to 30,000,000 mHz on that CPU. A CPU 314C is documented to 60,000,000 mHz. Compute your pulse rate in kHz and compare it with the rating of the exact CPU you have, because the rating is what decides whether the integrated counter can take the signal.

Do you need a hardware interrupt to count encoder pulses on a 313C?

No. The documented route is the CPU's integrated technological counter, which acquires edges in hardware regardless of where the scan is when a pulse arrives. Configure the counting function first, wire the encoder to the counter's assigned terminals, and check the measured rate against the input rating. Only when the rate exceeds that rating does the hardware change, to a CPU with a higher-rated counter or a dedicated counter module.

Keep reading

  • Troubleshooting

    Fix block format not recognised when uploading an S7-200 program

    A completed upload that Micro/WIN V4.0 cannot display means the transfer worked and the parser failed: the block in the CPU belongs to an older toolchain generation. Save the upload untouched, open the original project in the Micro/DOS or Micro/WIN release that created it, and convert a copy in V4.0. Download nothing until the recovered project opens, compiles and matches the block inventory you recorded.

    PLC programming

  • How-to

    Migrating a Standard PID Control PID_CP block to an S7-1500

    PID_CP from Standard PID Control does not convert into an S7-1500 project, and know-how protection blocks the conversion outright. Rebuild the loop on PID_Compact, reproduce the missing LMN_OP function with a small pre-call block, and prove identical response on trends before you commission.

    PLC programming

  • How-to

    Replacing the S7-200 CPU 224 battery module safely

    Yes, you can exchange it with the CPU powered: stop and secure the machine, leave control power on, and swap the BC 291 cartridge in one brief motion. Test any cartridge unplugged, at a minimum of 2.2 V through 100 kOhm between pins 2 and 6. Upload the program first, because the cartridge's life is estimated at about 10 years and it is not a copy of the program.

    PLC programming