Vocademy

Gray Code

A shaft encoder is used to convert the position of a rotating shaft to a digital number. This is usually done by shining LEDs through a disk with clear, and opaque areas or by similar means.

A schematic of an optical shaft encoder using the 8421 binary code.

With an optical encoder, LEDs shine through the encoder wheel, and are blocked by the opaque parts at certain angles of rotation. Photodiodes (or phototransistors) are aligned with the LEDs on the opposite side of the wheel. As the wheel turns, different parts of the wheel are presented to the LEDs, and photodiodes. This results in a different binary code being presented by the reader for the different positions of the encoder. The above encoder has a resolution of 45 degrees, and can encode eight positions. Real-world shaft encoders have many more elements, and a much finer resolution.

Here's the problem with using the 8421 code. If the LED/photodiode pairs aren't in perfect alignment, spurious numbers are reported by the encoder.

8421 shaft encoder with a misaligned LED

The above illustration has an LED that is misaligned. Notice that when the shaft is turning from 011 to 100 that the encoder will momentarily show the shaft has jumped to the 110 position. That's more than 90 degrees from where the shaft actually is. In addition, the shaft appears to be jumping around.

The following illustration uses a Gray code, which never has more than one bit change at a time.

Gray code wheel with misaligned LED

With the Gray code, the encoder never sends a spurious number. There is a misalignment, but the error is a degree or two instead of 90-plus degrees, and the shaft doesn't appear to be jumping around.

 

A Gray code shaft encoder that uses reflection instead of light transmission.

Vocademy