How-to
Counting Encoder Pulses on a PLC Without Losing Position
Short answer
Count encoder pulses with a hardware high-speed counter input wired for quadrature, never with a count instruction in the scan. At 214 rpm and 75 pulses per revolution the encoder delivers 267.5 pulses per second, one every 3.7 ms, while a 10 ms scan cannot guarantee seeing even one in three. Zero the counter at a reference switch and treat every target as counts from that switch.
On this page
- How fast do encoder pulses arrive at the PLC input?
- Can the PLC scan count pulses that fast?
- What does a high-speed counter input do that the scan cannot?
- Does it matter which shaft the encoder is mounted on?
- How should the encoder be wired to the PLC?
- How do I turn counts into a position?
- How do I commission the counting so nothing is lost?
- Why does my count still drift or sit a few pulses off?
- Questions asked
Count encoder pulses with the controller's hardware high-speed counter inputs, not with a count instruction in ladder logic. The deciding quantity is the pulse rate at the terminals: a shaft turning 214 rpm with a 75 pulses-per-revolution encoder delivers (214 × 75) / 60 = 267.5 pulses per second, one every 3.7 ms, while a typical 10 ms program scan cannot see every third pulse. This article computes that rate, sets the wiring and counting decisions it forces, and gives the commissioning sequence that proves the position is real: quadrature direction, a reference-switch zero, and a full-speed repeatability test.
How fast do encoder pulses arrive at the PLC input? #
Frequency is shaft speed multiplied by pulses per revolution, divided by 60: f = (n × PPR) / 60. At 214 rpm and 75 PPR that gives (214 × 75) / 60 = 267.5 pulses per second, one rising edge every 3.7 ms.
The period between rising edges is T = 1/f, and incremental encoders hold each channel near 50% duty, so the signal is high for roughly T/2 and low for roughly T/2. That shortest high time, not the period, is what an input must actually accept. Three calculated examples, not measured results:
| Shaft speed | PPR | Frequency | Period | Shortest state at 50% duty |
|---|---|---|---|---|
| 214 rpm | 75 | (214 × 75) / 60 = 267.5 Hz | 1 / 267.5 = 3.74 ms | about 1.9 ms |
| 214 rpm | 3 | (214 × 3) / 60 = 10.7 Hz | 1 / 10.7 = 93.5 ms | about 47 ms |
| 900 rpm | 1024 | (900 × 1024) / 60 = 15,360 Hz | 1 / 15,360 = 65 µs | about 33 µs |
Speed moves every number linearly: double the rpm and you double the frequency and halve the times. Compute at the maximum commanded speed of the axis, not the rated point, because the fastest move is the one that drops pulses. If you are still free to pick the resolution, matching pulses per revolution to the rate you can capture is the same step as sizing a spindle encoder input for servo feed control.
Can the PLC scan count pulses that fast? #
No, not at that rate. A count instruction runs once per scan and can register at most one rising edge per scan, so a 10 ms scan tops out near 100 Hz, and with a 50% duty cycle the safe figure is half that, about 50 Hz.
The instruction only sees the input image, which refreshes once per scan. Program scans of 10 to 12 ms are common on small machines and larger programs run slower, so treat that as a rule of thumb and measure yours under full load. At a 3.74 ms period, whole pulses rise and fall between updates and never enter the count; the ones that land on an update alias the position short, and the error grows with every move. The mechanism behind why a PLC misses short input pulses and how to catch them is exactly this sampling gap.
The input adds its own limit before the scan does: an input filter, often a few milliseconds, must see each state as stable before reporting it, and a filter longer than the shortest state deletes the edge outright. Take the adjustable filter range from the documentation of the exact input.
Decision: if the calculated period is less than twice your measured scan time, software counting is disqualified, and with the 75 PPR case it is out by more than a factor of five. The 3 PPR case, at 93.5 ms per pulse against a 10 ms scan, leaves nine samples per pulse and would count in software, but it earns the hardware input anyway when one is free.
What does a high-speed counter input do that the scan cannot? #
A high-speed counter is dedicated hardware that clocks every qualifying edge into an accumulator independently of the scan, so the ladder reads a value that is already correct, and its quadrature decoding gives direction from the measured A/B phase order rather than from your motor command.
Counting modes set the scale: ×1 counts rising edges of channel A, ×2 counts both edges of A, and ×4 counts every edge of both channels, so a 75 PPR encoder yields 75, 150 or 300 counts per revolution. Use ×4 for position: it uses every edge and cuts the quantization from 360 / 75 = 4.8 degrees per count to 360 / 300 = 1.2 degrees. Direction comes from phase: one rotation direction produces A leading B by 90 degrees, the reverse produces B leading A, so the counter follows coasting, pushes and reversals the command signal knows nothing about.
Take the counter's maximum input frequency and its eligible terminals from the documentation of the exact controller; that figure is a hardware rating, not a setting you can raise. In an IEC 61131-3 programming environment the counter appears as a function block configured once, with the counting happening outside the scan. And if the count feeds a PC-side application instead of the PLC's own logic, the same sampling argument is why PC-polled remote I/O cannot count encoder pulses for SCADA: a polling interval is only a slower scan.
Does it matter which shaft the encoder is mounted on? #
Yes, because pulses per revolution belong to the shaft the encoder actually rides, so rpm and PPR must describe the same shaft. The same drive gives 267.5 pulses per second with 75 PPR on the motor, or 10.7 pulses per second with 3 PPR there.
Both numbers turn up in the same kind of documentation, and they answer different questions. With 75 PPR on a 214 rpm motor shaft you need the hardware counter, no exceptions. With 3 PPR on that shaft the 93.5 ms period is software-countable, at the price of 4.8 degrees of resolution per count in ×1 mode.
If the quoted PPR sits after the gearing, divide the motor speed by the gear ratio before multiplying, so rpm and PPR name the same shaft. Where the encoder sits also fixes what the count means mechanically, and the tradeoffs of mounting a motor encoder behind a belt reduction apply to any geared axis. Write one line on the drawing: shaft, PPR, maximum rpm. Those three numbers choose the whole counting architecture.
How should the encoder be wired to the PLC? #
Wire A and B to the controller's two documented high-speed inputs on the correct common, verify the encoder's output type against those inputs before powering anything, and keep the encoder supply reference consistent with the input circuit it feeds.
Two output families exist. Single-ended 24 V DC outputs, push-pull or open collector, drive 24 V inputs directly; the on and off voltage thresholds those inputs must accept are set by IEC 61131-2, so an encoder documented against that signaling convention settles the match. Differential outputs with complementary A and B pairs want differential receivers and are the choice for long cables, noisy cabinets and high frequencies. An open-collector output needs a pull-up to the input's supply, and its rise time, not the counter, sets the practical frequency limit.
The input common decides which rail the commons tie to: one input type wants its common on +24 V DC, the other on 0 V, and a rail that suits one type reads dead on the other. Confirm by measurement rather than by trusting the terminal you inherited: turn the shaft slowly and look for clean high and low levels at the input. Never put 24 V on a conductor documented as a 5 V signal line. Isolate the machine, electrically and its stored energy, before touching the wiring, and bring the axis back at reduced speed with its guards closed, because a mis-counting axis moves to wherever the wrong count sends it.
How do I turn counts into a position? #
Zero the counter on a fixed reference switch on the axis and define every target as counts from that switch — never as motor run time, which drifts with load, supply voltage and friction.
The homing routine: jog slowly toward the switch, zero or latch the counter on its edge, back off, approach again, and require the same zero twice; that repeatability is the whole value of the switch. Scale from there. In rotary terms, counts per revolution divided by 360 gives degrees per count, so 300 counts per revolution means 1.2 degrees per count and a 90 degree target is 90 × 300 / 360 = 75 counts. On a linear axis, counts per millimeter equal counts per revolution divided by travel per revolution: belt pitch times pulley teeth, or screw lead.
Set the sign by jogging both ways: forward travel must count up, and if it does not, swap A and B at the input or invert the counter's direction setting, then verify the scale in that one place.
How do I commission the counting so nothing is lost? #
Work the numbers first, then wire, then configure, then prove the count at full speed, in that order — each step depends on the one before it, and the two-speed repeatability test is what finally signs off the axis.
- Compute the maximum rate: f = (n × PPR) / 60 on the encoder's own shaft at maximum commanded speed, T = 1/f, shortest state about T/2. Write the three numbers on the drawing.
- Compare T with the measured scan time and the input filter. A period below twice the scan disqualifies software counting; with 75 PPR at 214 rpm the hardware counter is mandatory.
- Wire A and B to the documented high-speed inputs on the verified common, with the output family matched to the input circuit.
- Configure the counter for quadrature ×4 and set the input filter as low as the noise on the line allows, since every millisecond of filter is a millisecond the pulse must be able to spare.
- Verify the scale: turn the axis exactly one revolution and read the accumulator. 75 PPR in ×4 must show 300 counts, exactly. Record the mode next to the number.
- Home the axis on the reference switch with a slow approach, zero on the edge, retract and re-approach, and require the same zero twice.
- Prove capture at speed: run one identical move at low speed and at maximum speed and demand equal counts. A shortfall that appears only at speed is a capture fault, and the axis does not run production until it is gone.
Why does my count still drift or sit a few pulses off? #
A count that loses pulses only at speed is a capture problem — wrong input, filter too long, or a scan-executed instruction — while a constant offset on every repeated move is an arming problem: the counter was enabled late or read early.
Work the table from the top, cheapest check first, and confirm each fix with the two-speed test before moving down:
| Symptom | Likely causes, cheapest first | Confirming check |
|---|---|---|
| Pulses lost only at speed | Counting in ladder or on an ordinary input; input filter too long | Move the count to the hardware input, shorten the filter, repeat the two-speed test |
| Same few pulses short every move | Counter armed after the first edges; accumulator read before standstill | Enable counting before the motor output, read after the axis settles |
| Count never changes at any speed | Wrong common; incompatible output family; no encoder supply; open conductor | Measure the signal at the input terminal while turning the shaft slowly |
| Count moves with the axis stationary | Noise or chatter on A or B from unscreened wiring near motor leads | Observe the terminal waveform with the drive enabled and the axis stopped |
| Sign wrong on reversal | A and B swapped at the input; direction setting inverted | Jog both directions and compare travel against the signed count |
Fix capture before touching anything downstream. No scaling value or motion gain recovers pulses the input never clocked, and every missed pulse is a position error waiting for the longest travel of the day.
Questions asked
Does an input interrupt or edge-catch function solve this instead of a high-speed counter?
Partly. An input-change interrupt catches each edge in firmware, so it beats the scan, but every edge costs controller time and the response jitters as program load changes. That suits isolated events such as reference switches and part detection, not a continuous stream of hundreds of pulses per second; for feedback, the hardware counter is the dependable path.
Do I need the encoder's Z index channel for positioning?
Only within one revolution. Z pulses once per turn and gives an exact electrical reference for short travels, but it cannot tell you which revolution you are in, so a multi-turn axis still homes on the reference switch. Latch Z as the zero only when the whole travel fits inside one turn.
How do I confirm the counts-per-revolution the PLC will actually see?
Rotate the axis exactly one revolution by hand and read the accumulated count: 75 PPR gives 75 counts in x1 mode, 150 in x2 and 300 in x4. Record which mode you used, because changing it later silently rescales every stored target.
Why is my count short by the same few pulses on every move?
That points at arming rather than capture: the counter starts after the first edges have passed, or you read it before the axis stands still. Enable counting before switching the motor output on, sample after motion has settled, and the constant offset disappears. Losses that grow with speed are the capture fault instead.