Gray Code's Error Reduction with Encoders

Initializing live version
Download to Desktop

Requires a Wolfram Notebook System

Interact on desktop, mobile and cloud with the free Wolfram Player or other Wolfram Language products.

Unlike binary, which follows the sequence 000, 001, 010, 011, 100, and so on, a Gray code is one in which only one bit changes between consecutive steps, such as in the sequence 000, 001, 011, 010, 110, and so on. This has many applications, such as reducing the likelihood of reading an incorrect code from encoders. Two types of encoders are shown in this Demonstration, a rotary encoder used to measure rotation, and a linear encoder used to measure absolute distance. The red sensor reads white contacts as zeros and black contacts as ones, so each position of the sensor yields a unique four-digit code. However, if the positions of the contacts on the encoder are anything less than perfect, the sensor might read an incorrect code. Since a Gray code only varies by one bit for consecutive steps, this will never cause the sensor to read a code that is more than one step away from what it should be, which is shown as the "real step" in the Demonstration. Binary has no limit to how many bits can change between steps, so the transition might cause an illogical sequence, such as from steps three to zero to four. The Demonstration shows this by allowing the contacts for one of the bits to become misaligned. If the sensor reads the correct code, the step is displayed in black; if it is one step away, it is displayed in orange; and if it is more than one step away, it is displayed in red.


Because a Gray code can help reduce error, it is often used for error correction in transmissions. Since only one bit changes per step, errors can be more easily identified and corrected. Communication satellites are often programmed with Gray code for this reason.


Contributed by: Mark Peterson (August 2011)
Suggested by: Ed Pegg Jr.
Open content licensed under CC BY-NC-SA



Snapshot 1: these linear encoders have no misalignment; they will both read the code perfectly

Snapshot 2: the third digit is incorrect for both encoders; however, the Gray encoder will still follow the sequence of steps three to four, while the binary encoder will go from steps three to zero to four

Snapshot 3: the third digit is incorrect for the binary encoder, but correct for the Gray encoder; errors are both smaller and less frequent with Gray code than binary

Feedback (field required)
Email (field required) Name
Occupation Organization
Note: Your message & contact information may be shared with the author of any specific Demonstration for which you give feedback.