Why Rotary Encoders Use Gray Code Instead of Binary
Mechanical rotary encoders convert angular position into digital signals, and Gray code is preferred over standard binary to prevent read errors caused by physical switching delays. In standard binary systems, multiple bits often change simultaneously between adjacent states, which creates temporary false readings due to microscopic mechanical misalignments. Gray code solves this issue by ensuring that only one bit changes at any given transition, guaranteeing reliable and error-free positional tracking.
The Multi-Bit Switching Problem in Binary
In standard binary counting, transitioning from one position to the next frequently requires multiple bits to flip at the same time. For example, moving from 3 (011 in binary) to 4 (100 in binary) requires all three bits to change state.
In a physical rotary encoder, reading these bits relies on mechanical contacts or optical tracks passing over sensors. Because of mechanical tolerances, contact wear, and micro-imperfections, it is physically impossible for all electrical switches to make or break contact at the exact same microsecond.
If one contact switches slightly faster than the others, the encoder temporarily outputs intermediate, incorrect values known as “ghost states” or “glitches.” During the shift from 011 to 100, the system might briefly read 010 (2), 000 (0), or 111 (7). Even if these erroneous states last for only a fraction of a millisecond, a digital controller sampling at high speeds will register them as massive, erratic jumps in position.
How Gray Code Prevents Read Errors
Gray code is a non-weighted, cyclic binary system designed specifically to eliminate transition errors. Its defining characteristic is that between any two consecutive values, only one bit ever changes state.
For instance, the decimal sequence 0, 1, 2, 3 in 2-bit Gray code is
represented as: * 0: 00 * 1: 01 * 2:
11 * 3: 10
Because only a single bit changes at each step, there are no other
bits that need to synchronize. If the sensor is positioned directly on
the boundary between two sectors, the only possible readings are either
the current position or the adjacent position. A transition from
01 to 11 can only be read as 01
or 11—it is physically impossible to output a wildly
inaccurate intermediate value.
Handling Mechanical Bounce and Wear
Mechanical encoders suffer from contact bounce, where an electrical contact rapidly oscillates between open and closed states before settling. In standard binary, contact bounce during a multi-bit switch can introduce severe data corruption and false movement vectors.
With Gray code, contact bounce on a boundary simply toggles the single changing bit back and forth. The receiving processor interprets this merely as tiny jitter between two adjacent positions (\(N\) and \(N+1\)), preventing catastrophic position loss or false triggers in motion-control systems.
System Integration
While microcontrollers and digital computers process arithmetic using standard binary, the conversion from Gray code to binary is computationally inexpensive and easily executed in software or with simple logic gates (XOR circuits). Using Gray code at the physical measurement layer provides hardware-level fault tolerance, while the downstream system converts the data into standard binary for calculation.