Free engineering calculators and unit convertersceo@instrumentationblog.in
PLC Digital Signal · Free Calculator

Gray Code Converter for Absolute Encoders

Convert between binary and Gray code, as used by absolute encoders, and see the encoder position in degrees for its resolution.

Gray codeAbsolute encoderBit viewPosition angle
Decimal position300
Gray code (binary)0110111010
Gray code (hex)0x1BA
Plain binary0100101100
Angle105.47°
Resolution0.3516° per step
How this result was calculated
  1. Gray = binary XOR (binary shifted right 1) = 0100101100 XOR 0010010110
  2. Gray = 0110111010
Advertisement

Gray Code Conversion

Gray = B XOR (B >> 1)
Binary: btop = gtop, bn = bn+1 XOR gn
Angle = position ÷ 2bits × 360°

B is the binary value and >> a right shift by one bit. In Gray code only one bit changes between neighbouring positions, so an encoder read during a transition is never off by more than one step.

Worked example

Position 300 on a 10 bit encoder is Gray 0110111010 (0x1BA) and corresponds to 105.47°.

Key insight: if an encoder value jumps by large amounts at certain positions, the PLC is probably reading Gray code as binary or the bit order is reversed.
Advertisement

Frequently Asked Questions

Why do encoders use Gray code?

Only one bit changes per step, so reading the code while it changes cannot produce a wildly wrong position.

How do I convert Gray to binary in a PLC?

Start from the top bit and XOR each Gray bit with the previous binary bit, or use a loop with shifts and XOR.

Do SSI encoders send Gray code?

Many SSI encoders can send binary or Gray; it is usually a configuration option in the encoder.

You May Also Like

Learn more on the blog

Introduction to number systems

A practical, field focused guide on instrumentationblog.in that explains the theory behind this calculator and how engineers apply it on real plants.

Read Full Article

More PLC Digital Signal Calculators

More Calculator Categories