AutomateDirectUSAMotion LibraryIndustrial monitors & PCs

How-to

Converting gray code encoder inputs to press angle degrees

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

Short answer

Assemble the ten encoder inputs into one Gray-code word, convert it once with the MicroLogix 1400 GCD instruction, then scale: angle equals the converted count times 360 degrees over 720 counts, so one count is 0.5 degrees. Subtract a measured zero offset, wrap the result into one revolution, and the press reads a stable 0-360 degree position.

On this page
  1. What the TRD-NA720NWD sends
  2. Prove the ten inputs before writing logic
  3. Assemble one clean Gray-code word
  4. Convert once with the GCD instruction
  5. Scale the count to press degrees
  6. Worked example: a count of 450 at mid-stroke with a 90-count offset
  7. Verify through a full revolution
  8. Questions asked

The inputs switching on and off only proves that signals reach the PLC. To get a press position, assemble the ten encoder outputs into one Gray-code word, convert that word once with the MicroLogix 1400 GCD instruction, and scale the result: angle equals the converted count multiplied by 360 degrees and divided by the encoder's 720 counts per revolution, so one count is worth 0.5 degrees. Over one turn the converted count runs from 0 to 719. The steps below prove the raw bits, build the word, apply the conversion, set the zero offset and handle the rollover, so the press reads a stable position across its full circle.

What the TRD-NA720NWD sends #

The TRD-NA720NWD is a single-turn absolute rotary encoder from the TRD-NA series with a 10-bit Gray code output at 720 ppr. Its cut sheet lists a 10–26 VDC supply, HTL NPN open-collector outputs, an 8 mm solid shaft, an IP65 housing and a 2 m (6.5 ft) pigtail cable. Confirm the resolution on the cut sheet of the exact part rather than assuming it from the model number. A 10-bit word can hold 1024 codes; the head assigns one code per mechanical position, so most possible codes never appear. The input side of the job, wiring the ten NPN outputs to a DC input card, is covered in Wiring a 10-bit Gray Code Absolute Encoder to Standard DC Inputs.

Prove the ten inputs before writing logic #

Conversion and scaling cannot repair a bad raw word, so settle the inputs first:

  1. Jog the press slowly through one full revolution and watch every input LED; each of the ten must change state somewhere in the turn.
  2. Stop at several positions on both sides of press zero, record the complete on/off pattern, and revisit each stop from both directions: the pattern must be identical.
  3. Between two adjacent stops only one input should change. A bit that never changes, flickers at a standstill, or changes together with its neighbors marks a wiring or bit-order fault.
  4. Identify the least-significant and most-significant Gray bits from the encoder documentation; terminal order is not necessarily numeric order.

Fix any dead, inverted or misordered channel now, before a single scaling constant exists. Note the input module's documented on/off response as well: if the shaft can pass through a count faster than that response time, the PLC never captures that position, and no logic recovers it.

Assemble one clean Gray-code word #

The GCD instruction needs the ten bits presented as one integer in the encoder's bit order:

  1. If the signals already fill consecutive bits of one input word in the documented order, use that word directly or a masked copy of it.
  2. Mask out any unrelated devices sharing the word; an unrelated on-bit becomes part of the encoded position and corrupts the result.
  3. If the signals are split across input words or out of order, copy each input into its documented bit position of a dedicated integer every scan, forcing unused bits to zero.
  4. Overwrite the destination completely each scan and do not latch the mapped bits; the word must represent the simultaneous state of the inputs.
  5. Monitor the assembled word while turning the shaft by hand and compare it against the input LEDs.

Convert once with the GCD instruction #

On the MicroLogix 1400, GCD is a Gray-code-to-binary converter: the source is the assembled Gray word and the destination is a separate integer, executed every scan that uses the position. The destination is the ordinary binary count that the rest of the program scales.

The conversion works because each binary bit is built from the bits above it: the highest binary bit equals the highest Gray bit, and each lower binary bit is the exclusive OR of the binary bit above it with the Gray bit beside it. As a four-bit concept check, Gray 1011 becomes binary 1101, decimal 13: 1, then 1 XOR 0 = 1, then 1 XOR 1 = 0, then 0 XOR 1 = 1. Decoding the ten inputs one at a time, as if each were its own switch, cannot reproduce that relationship, and a hand-built decoder that tries it can miscount, as Why a Gray Code to Binary Ladder Conversion Counts Wrong shows.

Acceptance test: turn the shaft slowly and watch source and destination together. The destination must step one count at a time and roll from the top count to zero exactly once per revolution. A destination that jumps sends you back to the bit-order check, not to a filter; averaging a jumping word only hides the fault while delaying the press signal.

Scale the count to press degrees #

The scaling is one line: AngleDegrees = ConvertedCount * 360 / CountsPerRevolution, with the counts per revolution taken from the cut sheet. Multiply before dividing so integer math keeps the fraction, and keep full precision until the value is used.

Press zero rarely coincides with the encoder's zero. Jog the press to its zero position, read the converted count there and store it as the zero-offset constant. For every reading: subtract the offset, then wrap (if the result is negative, add one revolution of counts; if it reaches the counts per revolution, subtract them), then scale.

If the angle decreases as the press advances, reverse in the calculation, not at the wiring: subtract the converted count from the full count, wrap the single result that equals the full count back to zero, then apply the offset. Rewiring to change direction breaks the bit significance verified earlier. The rollover handling follows the method described for scaling a full-turn servo position without wraparound faults. The same count-to-degree arithmetic for a much finer absolute encoder is worked in Scaling a 262144-count absolute encoder to 0-360 degrees.

Worked example: a count of 450 at mid-stroke with a 90-count offset #

Inputs, each with its source:

  • Converted count at the press's mid-stroke position: 450 counts. This is an example reading from the GCD destination; use the number your installation shows.
  • Counts per revolution: 720, the documented resolution of the TRD-NA720NWD on its cut sheet.
  • Zero offset: 90 counts, an example value captured as the converted count with the press parked at its zero position.
  • Degrees in one revolution: 360, the full press circle the display must cover.

Rule in words: subtract the zero offset from the converted count, wrap the result into one revolution by adding or subtracting the full count when it falls outside, then multiply by the degrees in one revolution and divide by the counts per revolution, multiplying before dividing.

Arithmetic:

  1. Offset: 450 − 90 = 360 counts, inside the valid range, so no wrap is needed.
  2. Scale: 360 × 360 = 129600, and 129600 ÷ 720 = 180.0 degrees.
  3. Wrap check at a low reading: with the raw count at 45, 45 − 90 = −45; add one revolution for 675 counts, and 675 × 0.5 degrees = 337.5 degrees.

Result: the mid-stroke position displays 180.0 degrees and a point just below press zero displays 337.5 degrees, with no jump when the count rolls over. This is a calculated example for these inputs, not a measured press reading.

Two decisions follow. If your program displays 0.0 degrees at the checked position, the division ran before the multiplication (integer math truncated the ratio to zero), so reorder the rung. And if the press job needs position windows narrower than 0.5 degrees, no scaling changes that: one count is the floor, and a finer window needs a higher-resolution encoder.

Verify through a full revolution #

  1. Correct the wiring, commons and bit order found in the raw-input test before judging the scaled result.
  2. Trend the assembled Gray word, the GCD destination and the calculated angle together through one slow revolution.
  3. Confirm the step-and-rollover behavior from the acceptance test still holds with scaling active, and the angle follows without a jump at the rollover.
  4. Park the press at zero, mid-stroke and both quarter points and check the displayed degrees against reference marks on the machine.
What you see Most likely cause Confirming check
Angle jumps in large steps Gray bits read as straight binary, or wrong bit order Destination fails the slow-turn step test
Angle smooth but decreasing as the press advances Encoder direction opposite the press convention Destination counts down while the press advances; reversing rung not applied
Zero degrees at the wrong press position Offset constant wrong or not yet entered Re-read the converted count with the press at zero
Jumps at one or two fixed positions One input stuck, inverted, intermittent or misordered Raw-input test shows a bit that never changes or flickers
Wrong only at the rollover Wraparound rung missing or ordered after scaling Watch the last count become zero on the trend

One boundary: this angle is control feedback only. ANSI B11 and ISO 12100 require press guarding and stop functions to come from the machine's risk assessment and to stay effective regardless of what this logic computes, so nothing safety-related reads the scaled value.

Questions asked

The converted count reads higher than any real press position. What does that mean?

The head defines a fixed count of positions per revolution, so the GCD destination can never legitimately reach the counts-per-revolution value itself or beyond. A higher number means one input is stuck on, inverted or sitting in the wrong bit position, and scaling it would produce an angle past the full circle. Flag that word as a sensor fault and rerun the raw-input check instead of trusting or clamping the angle.

Do I have to re-home the press after a power outage?

No, within one revolution. A single-turn absolute head reports its position as soon as its supply is up, so the first scan after power-up already delivers a valid Gray word. Store the zero-offset constant retentively so the degree mapping survives the outage. Homing is needed only if the encoder, its coupling or the press zero was mechanically moved while the machine was down.

Can the press screen show tenths of a degree?

It can be formatted that way, but the column would invent detail: this head resolves 0.5 degrees per count, so five consecutive tenths readings are the same physical step. Keep full precision through the calculation, round only the displayed value, and treat any control window narrower than one count as unmeasurable with this encoder.

Why does the encoder output Gray code if the PLC converts it to binary anyway?

Between any two adjacent positions exactly one bit changes, so a word sampled in the middle of a transition decodes to one of the two neighboring positions, never to a far-off value. Straight binary changes several bits at once, and a sample taken mid-change can read wildly wrong. Gray code protects the sampling moment; the conversion then restores the numeric count that scaling needs.

Why does the angle corrupt partway up the scale?

Signed 16-bit integers hold up to 32767, and the product of count and degrees-per-revolution crosses that at a count of 92, since 92 times 360 is 33120. Run the multiply and the divide in a 32-bit integer or a floating-point value and the scaling stays clean through the whole turn; the overflow point shows exactly where the 16-bit product wraps.

Keep reading

  • Selection guide

    Absolute encoder input card response time vs count rate

    Keep the 3-9 ms DC input card. Gray code changes one bit per count, so at a 10 ms count rate each input holds its level for two counts, 20 ms, against a 9 ms worst-case delay, and no transition is lost. The lag to budget is card delay plus PLC scan, about one to three encoder counts, which cam-switch replacement tolerates; the 0.8 ms card buys less than one count, so it is not worth a PLC family change.

    Sensors, encoders and machine vision on the machine

  • Explainer

    What it takes to read an absolute encoder into discrete PLC inputs

    Yes — a bit-parallel absolute encoder connects directly to discrete PLC inputs, with no special card, once four checks pass: output voltage matches the input card, one input is free for every bit, the logic converts Gray code to binary where needed, and the shaft stays under the scan-time speed limit. Networked Ethernet or Modbus encoders need documented protocol support in the PLC first.

    Sensors, encoders and machine vision on the machine

  • Troubleshooting

    Why a Gray Code to Binary Ladder Conversion Counts Wrong

    A Gray code to binary ladder conversion counts wrong when each XOR rung is fed the previous Gray bit instead of the previous binary bit, or when the nine input bits are mapped in a crossed order. Decode from the top bit down: copy the most significant bit, then take every lower bit as the exclusive-OR of the binary bit above it and the Gray bit at the same position. On a 9-bit, 512-step encoder the result must run 0 to 511, step by one.

    Sensors, encoders and machine vision on the machine