AutomateDirectUSAMotion LibraryIndustrial monitors & PCs

Explainer

Reading a Gray Code Absolute Encoder Into a PLC

Sensors, encoders and machine vision on the machine Published 10 min read By AutomateDirectUSA engineering team

Short answer

A five-line NPN absolute encoder needs no interface module: wire its five open-collector outputs to ordinary DC input points, read all five bits as one word each scan, and convert Gray code to binary before using the number. The catch is speed — scan-based sampling tracks a 32-position shaft reliably only below roughly half a revolution per second, so continuous multi-turn conveyor tracking belongs on a quadrature encoder and a high-speed counter with a home reference.

On this page
  1. What module do I need to read a five-wire absolute encoder into a PLC?
  2. How do you wire NPN encoder outputs to DC PLC input points?
  3. Why does an absolute encoder output Gray code instead of binary?
  4. How do you convert Gray code to binary in ladder logic?
  5. How fast can a PLC sample a Gray code through ordinary inputs?
  6. What is the difference between single-turn and multi-turn absolute encoders?
  7. When does a five-input absolute encoder beat an incremental one on a conveyor?
  8. How do you prove the reading is correct before trusting it?
  9. Questions asked

A five-line NPN absolute encoder needs no interface module: its five open-collector outputs land on ordinary DC input points, the PLC copies all five bits as one word each scan, and a short conversion turns the Gray code into a position number you can compare and use in math. The hard limit is not wiring or code — it is scan speed. Scan-sampled inputs follow a shaft only while its code steps slower than the program samples, which keeps this kind of encoder useful for position snapshots at crawl speeds and rules it out for continuous tracking across many revolutions. This article walks the wiring, the Gray-to-binary conversion with the arithmetic shown, the speed ceiling with numbers, and the decision between a five-input snapshot and a quadrature encoder on counter hardware.

What module do I need to read a five-wire absolute encoder into a PLC? #

None: five open-collector NPN outputs carry 32 positions per revolution (2⁵ codes), they sink DC input points toward common the way a limit switch does, and this signal type belongs on general-purpose DC inputs, not on high-speed counter inputs.

Each of the five lines is a transistor that pulls its wire to the encoder's 0 V side when the bit is active and releases it when the bit is off — electrically the same event any switch or two-wire sensor presents to an input point, so a general-purpose DC input card of the matching polarity reads it. Encoder documentation is explicit about the boundary: parallel absolute encoders are not compatible with high-speed counter modules, but they work on general-purpose DC input cards.

Five bits give 2⁵ = 32 distinct code values, so the disc divides one revolution into 32 positions: 360° ÷ 32 = 11.25° of shaft angle per step. That is coarse by design — this family reports a shaft angle, it does not resolve one finely. If the built-in DC points on the PLC are already committed, an ordinary DC input card adds points; no counter module or specialty interface enters the picture.

How do you wire NPN encoder outputs to DC PLC input points? #

Wire each output line to its own DC input point — five adjacent points work best — with the input card's polarity matching the encoder's NPN sinking outputs, the commons referenced per both manuals so an active bit and an energized point agree, and the supply voltage taken from the encoder datasheet.

NPN means sinking: the conducting transistor connects the output line to the encoder's 0 V side, and a released line floats until something pulls it up. The input point has to be the polarity that works with that; an NPN output on an input expecting the opposite polarity simply reports inverted states, which looks exactly like a failed encoder. The input circuit drawings in the PLC manual settle which polarity you have.

Three wiring rules earn their keep:

  • Put the five code bits on adjacent points of one card and reserve those points for the encoder alone, so one instruction copies the whole word in a single operation.
  • Take the encoder's rated supply voltage and output ratings from the encoder datasheet, and confirm the input card's on and off thresholds accept those levels.
  • Label the five points and keep other devices off them; a shared point corrupts the word silently.

Treat the terminal strip like any field wiring: power the machine down and lock out before you land the wires.

Why does an absolute encoder output Gray code instead of binary? #

Gray code exists because adjacent positions differ in exactly one bit, so a read caught halfway through a step still lands on one of the two true positions instead of a nonsense value — the whole reason a snapshot taken from scan-sampled inputs can be trusted at all.

Watch what plain binary would do moving from position 7 to position 8: 00111 becomes 01000, four bits flip at once, and a read taken mid-transition can return any of the sixteen patterns in between — each one a plausible position as far as the program knows. A Gray-coded disc is sequenced so consecutive positions change one bit, so the worst mid-step read is off by a single step, never wild.

The price is that the pattern is not a number: Gray 10110 is not decimal 22, and sorting, comparing or scaling the raw word gives answers in the wrong order. Convert before any math. For five bits, a lookup table with one entry per code also works and never miscomputes; the XOR chain in the next section scales to any bit count.

How do you convert Gray code to binary in ladder logic? #

Copy the top bit unchanged, then XOR each remaining Gray bit with the binary bit immediately above it; applying that chain to Gray 10110 gives binary 11011, decimal position 27, and a PLC instruction named for Gray conversion, where one exists, runs exactly this chain on the captured word.

For a five-bit word G with bits G4 (top) through G0 (bottom):

  • B4 = G4
  • B3 = B4 XOR G3
  • B2 = B3 XOR G2
  • B1 = B2 XOR G1
  • B0 = B1 XOR G0

Worked example with the captured word G = 10110 (illustrative inputs, not a measured reading):

  • B4 = 1
  • B3 = 1 XOR 0 = 1
  • B2 = 1 XOR 1 = 0
  • B1 = 0 XOR 1 = 1
  • B0 = 1 XOR 0 = 1

Binary 11011 is 16 + 8 + 2 + 1 = decimal 27, so the shaft sits at position 27. The test that proves the chain: encode 27 back the other way — 11011 XOR 01101 (11011 shifted right one place) = 10110, the original word — so the conversion is correct for this input.

In the PLC, keep the raw captured word in its own register and run the conversion into a second register, then use only the converted one for comparisons and math; with both visible, a wiring fault looks different from a conversion fault. If the instruction set includes a built-in Gray conversion instruction, one rung — load the raw word, apply the conversion, store the result — replaces the whole chain, and several small-PLC families ship one.

How fast can a PLC sample a Gray code through ordinary inputs? #

Scan sampling caps you at a few code steps per second in careful designs — the working rule of thumb says a typical scan-based program registers about 15 input changes per second at best — which for a 32-step encoder means well under one reliable revolution per second, about 28 rpm.

The mechanism is plain: ordinary DC inputs update the input image once per scan, after the input filter, so the program sees the shaft wherever it stood at refresh time. If the code moved several steps since the previous look, the positions in between never existed for the program — the sample jumps. A fast-input designation on one point does not make the five-bit word atomic either; the word is only as simultaneous as the scan that samples all five points.

Scan-time arithmetic alone allows more: divide the refresh rate by the step count and a 10 ms scan hints near three revolutions per second. Field practice lands far lower, because input filtering, where the read sits in the scan and scan jitter all take their share. Run the arithmetic with the numbers that decide the job:

  • Step rate = 32 steps per revolution × shaft revolutions per second.
  • At the 15 Hz ceiling: 15 ÷ 32 ≈ 0.47 rev/s, about 28 rpm.
  • At a 5 Hz design target: 5 ÷ 32 ≈ 0.16 rev/s, about 9 rpm.

These are calculated examples for those inputs, not measured results. Two refinements keep the conclusion honest. Because the value is absolute, a late sample is still a true angle — it just may not neighbor the previous read, which is fine when the program asks where the shaft is now and useless when it tries to count travel between reads. And this is the same physics that makes naive counting encoder pulses on a PLC without losing position drop steps on ordinary inputs.

What is the difference between single-turn and multi-turn absolute encoders? #

A single-turn absolute encoder reports shaft angle within one revolution and then repeats, forgetting its turn count at power-off, while a multi-turn encoder counts revolutions internally over a declared range — so a single-turn value alone cannot tell a running conveyor where a carton has travelled.

A single-turn disc codes its range over 360° and starts over; the angle itself survives power-off, because nothing in a passive coded disc needs a backup source to know which code window it occupies. The revolution count is a different matter: it evaporates the moment the machine stops and restarts elsewhere. A multi-turn unit adds a second counting mechanism with a total range the datasheet declares, so it wakes up knowing both angle and turns.

For the conveyor question this decides a lot before any wiring. If the carton's journey completes inside one encoder revolution, a single-turn unit geared so the interesting span fills most of a revolution is enough. If the shaft turns many times between the moments you care about, the single-turn value repeats on itself and cannot express travel; the turn counting has to live in logic the application maintains and re-establishes, because a five-bit parallel word offers no place to keep it.

When does a five-input absolute encoder beat an incremental one on a conveyor? #

Choose the five-input snapshot when the program reads position while the shaft is stopped or barely moving — index positions, jog stops, a docking or print station — and choose a quadrature encoder on a high-speed counter with a home reference when tracking must continue through many revolutions at line speed.

On the snapshot side, the five-DC-input read returns a valid absolute angle at every power-up with no homing move, no battery and no count to lose, and scan timing never bites because nothing moves fast when the program looks. That combination — position present at power-up, on plain input points — is the whole argument for the parallel absolute encoder.

On the tracking side, counter hardware accumulates the quadrature encoder's edge transitions in silicon, independent of the scan, so the ceiling moves from the program to the electronics. The costs are real: an incremental count dies at power-off, so the axis needs a home reference to re-anchor and the application must define what happens to tracked parts across that stop. If that is the shape of the job, configuring a high-speed counter for an encoder input walks the counter side of the change. The tracking logic that replaces the snapshot is a different pattern too — how to track conveyor parts with an encoder and a FIFO buffer shows it built out.

Higher-resolution absolute encoders exist, but they speak serial protocols that need interface hardware of their own; the parallel five-line style is the only absolute output that lands directly on plain DC input points.

How do you prove the reading is correct before trusting it? #

Prove capture and conversion separately: with the shaft stopped, watch all five points and confirm the raw word matches the encoder's stated code, then rotate the shaft slowly and confirm the raw word steps one code at a time while the converted position increments by exactly one.

Run the checks in this order, because each isolates one fault:

  1. Stopped-shaft check: rotate the shaft by hand to a few visible mechanical marks and compare the raw word against the full code table in the encoder datasheet.
  2. Step test: advance one position at a time and confirm the raw word changes by exactly one bit each step — a two-bit change means bits were read at different moments or a connection is loose.
  3. Conversion test: confirm the converted register moves by exactly one position per step, up and down, through a full revolution.
  4. Wrap test: cross the boundary between the last position and the first and confirm a clean single-bit step with no mid-range jump; this also proves code direction matches shaft direction.
  5. Speed test: run the application at its real speed and log consecutive reads; wherever the logic needs adjacent positions, confirm the reads actually differ by one.

A reading that passes those tests is proven for that encoder, those input points and that program — rerun the speed test whenever line speed or scan load changes, and treat any later two-bit jump as a wiring or sampling fault, not as a bad encoder.

Questions asked

How many positions does an eight-bit single-turn encoder give?

Each added bit doubles the count: eight bits give 256 distinct positions per revolution and ten bits give 1024, which is 360 ÷ 256 ≈ 1.4 degrees of shaft angle per step for the eight-bit part. The wiring, the whole-word sampling and the Gray-to-binary method stay exactly the same; every extra bit is one more input point and one more signal that must land in the same scan.

What happens to the position value when the shaft crosses zero?

A single-turn code wraps: the last position steps straight to the first with no carry into any register, so logic that expects values to march upward sees an apparent jump once per revolution. Handle the wrap explicitly, or track whole turns in a register the application maintains. The Gray pattern itself crosses cleanly, because exactly one bit changes at the zero boundary too.

Does the scan-speed limit also apply to quadrature encoders?

Not in the same way. A quadrature encoder feeds counter hardware that accumulates edge transitions independently of the program scan, so its usable speed is bounded by the counter's electronics rather than by scan time. That is precisely why continuous multi-turn tracking moves to a quadrature encoder and a high-speed counter: the scan-based ceiling that governs parallel Gray-code inputs never governs that path.

Can I read the five encoder bits from two different input cards?

Avoid it. Bits refreshed on different cards can update at different moments in the scan, so a moving shaft can hand the program a mixed word the encoder never emitted, with two points claiming one position. Keep the five code bits on adjacent points of one card and copy them with a single instruction; Gray coding limits the damage of a mid-step read but cannot repair bits sampled at different times.

Keep reading

  • How-to

    Counting Encoder Pulses on a PLC Without Losing Position

    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.

    Sensors, encoders and machine vision on the machine

  • How-to

    Connecting a 5V Servo Encoder to 24V PLC Inputs

    No interface can raise the PLC input's own frequency limit, so a 20 kHz optocoupler board does not make a slower high-speed input faster. Compute f = RPM × pulses per revolution ÷ 60 first; if it exceeds the input's documented maximum, use a faster counter input. If it fits, translate the 0-5 V signal through an optocoupler whose LED current, output topology and switching times all check out.

    Sensors, encoders and machine vision on the machine

  • Troubleshooting

    Troubleshooting an Absolute Encoder Multi-Turn Counter Alarm

    A multi-turn counter alarm means the encoder's accumulated-revolution count is corrupt or lost. Work the causes in cost order — backup battery (replace any cell under 3.0 V), cable and shielding, the drive's documented counter reset — and replace the encoder only when the alarm survives all three. The alarm names the fault domain, not the failed part.

    Sensors, encoders and machine vision on the machine