Robotics

Quadrature Encoder

Two offset tracks reveal direction and position

A quadrature encoder is an incremental position sensor with two output channels, A and B, whose square waves sit 90° apart. The phase order — which channel leads — reveals the direction of motion, and counting the edges reveals position. Decoding all four edges of each cycle multiplies the disc's line count by four, giving the cheap, high-resolution feedback inside every servo, robot joint, and scroll wheel.

  • ChannelsA, B (90° apart) + optional Z index
  • DirectionSign of A-vs-B phase lead
  • Resolution gain4× decoding → 4 counts per line
  • Typical disc1,000–5,000 PPR → 4,000–20,000 CPR
  • Angular step0.09° at 1,000 PPR, 4× decode
  • SignalingRS-422 differential, A/A̅ B/B̅ pairs

Interactive visualization

Press play, or step through manually. The visualization is yours to drive — try it before reading on.

Open visualization fullscreen ↗

Watch the 60-second explainer

A condensed visual walkthrough — narrated, captioned, under a minute.

What a quadrature encoder actually does

An encoder is a transducer that turns mechanical motion into a stream of electrical pulses. The simplest version — a single light beam interrupted by a slotted disc — produces one pulse per slot. Count the pulses and you know how far the shaft has moved; divide by elapsed time and you know how fast. But that single channel hides a fatal ambiguity: a forward slot and a backward slot produce identical pulses. The sensor cannot tell clockwise from counter-clockwise. For a fan or a flywheel that only ever spins one way, that is fine. For a servo axis that must hold a setpoint, reverse on command, and never lose track of where it is, it is useless.

A quadrature encoder removes the ambiguity by adding a second channel. The two output tracks, A and B, are arranged so their square waves are offset by exactly one quarter of a cycle — 90 electrical degrees, the geometric meaning of the word "quadrature." Now the relationship between the two channels carries information that a single channel never could: the order in which their edges arrive tells you the direction, and the total number of edges tells you the position. Those two facts — direction from phase, position from counts — are the whole concept.

Quadrature encoders are everywhere precise motion feedback is needed and cost matters: brushed and brushless servo drives, CNC machine axes, robot joints, the scroll wheel under your finger, dial knobs on instruments, printer carriages, and the commutation feedback that lets a brushless DC motor know which winding to energize next. They are the default incremental feedback device of the entire motion-control industry.

Direction: the sign of the phase lead

Picture the disc rotating clockwise. The optics over track A see a transition first; a quarter cycle later the optics over track B see the matching transition. Channel A leads channel B. Reverse the rotation and the geometry reverses with it: now B's edge arrives first, and B leads A. The decoder never needs to know absolute angle to determine direction — it only needs to sample one channel at the instant the other one changes.

Formally, every edge of A or B is a state transition between four legal states of the two-bit code (A, B): 00, 01, 11, 10. As the shaft turns one way the code cycles 00 → 01 → 11 → 10 → 00; the other way it cycles 00 → 10 → 11 → 01 → 00 — the same four states in the reverse Gray-code order. A quadrature decoder is, at heart, a tiny state machine that watches these transitions:

prev = (A<<1) | B           // 2-bit state, last sample
curr = (A<<1) | B           // 2-bit state, this sample
delta = (prev, curr) lookup // +1, -1, or 0 (or 2 = error)

      curr→  00   01   11   10
prev 00       0   +1    X   -1
     01      -1    0   +1    X
     11       X   -1    0   +1
     10      +1    X   -1    0    (X = illegal jump = noise/missed edge)

Every legal step adds +1 or −1 to the position counter; the sign is the direction. An "X" entry — a jump of two states at once — is physically impossible at a fingernail of speed and flags a missed edge or a noise glitch, which is exactly how good decoders detect that something has gone wrong. Modern microcontrollers (STM32 timers in encoder mode, the TI C2000 eQEP peripheral, dedicated LS7366R counter chips) implement this table in hardware and maintain the count with zero CPU load.

Position and resolution: why 4× decoding is free

Manufacturers rate an encoder in PPR (pulses per revolution, also called lines) — the number of slots cut into the disc. But because the A/B pair has four distinguishable edges per cycle (A rises, B rises, A falls, B falls), a decoder that counts all of them resolves four counts for every line. The post-decode figure is CPR (counts per revolution):

CPR = PPR × decode_factor      decode_factor ∈ {1×, 2×, 4×}

Angular resolution  Δθ = 360° / CPR

Example — a common 1,000-PPR disc:
  1× decode:  CPR = 1,000   →  Δθ = 0.360°
  2× decode:  CPR = 2,000   →  Δθ = 0.180°
  4× decode:  CPR = 4,000   →  Δθ = 0.090°

Example — a 5,000-PPR servo encoder, 4× decode:
  CPR = 20,000  →  Δθ = 0.018°  (about 65 arc-seconds)

The factor-of-four improvement costs nothing — no finer slots, no better optics. It falls straight out of the 90° geometry, and every hardware decoder peripheral does it by default. This is the single most important practical fact about quadrature encoders: the number on the datasheet (PPR) is one quarter of the resolution you actually get (CPR). Engineers who size a system from PPR instead of CPR end up over-specifying the disc by 4× and paying for it.

Maximum count rate is the constraint that bites at the top end. The edge frequency the decoder must keep up with is:

f_edges = (rpm / 60) × CPR        [edges per second, Hz]

5,000-PPR encoder (20,000 CPR) at 6,000 rpm:
  f_edges = (6000 / 60) × 20,000 = 100 × 20,000 = 2,000,000 Hz = 2 MHz

If the decoder's input bandwidth is below this, edges are dropped and the count slips permanently — an error that never self-corrects until the next index pulse. This is why high-line-count encoders on fast spindles need fast differential receivers and short, shielded cable runs.

The index channel and absolute homing

Channels A and B are purely relative: they tell you the shaft moved 4,217 counts since you started counting, but not where it started. The third channel — the index, marked Z or the reference/marker pulse — fires once per revolution at a fixed mechanical angle. On the first index pulse after power-up, the controller latches or zeroes its counter, converting the relative count into an absolute angular reading good to a single count for the rest of that power cycle. This is the homing move every CNC and many servo systems perform at startup: jog the axis until the index appears, then declare that point zero.

Because A and B are exactly 90° apart, the index can be gated against a specific A/B state to guarantee it always latches at the same count — a "gated index" that removes the one-count ambiguity of where, within a cycle, the marker actually triggers. Lose power and the absolute reference evaporates; you must re-home. That single limitation is the dividing line between incremental quadrature encoders and absolute encoders, covered below.

Measuring velocity from the same two channels

The same edges that give position give velocity, and servo loops use both. There are two complementary methods:

  • M-method (frequency counting). Count the number of edges in a fixed sampling window and divide by the window time. Accurate at high speed where many edges arrive per sample, but quantizes badly at low speed — at one edge per window the velocity reading jumps between 0 and one full count.
  • T-method (period timing). Measure the elapsed time between two successive edges with a fast timer and invert it. Accurate at low speed where periods are long and easy to time precisely, but noisy at high speed where periods shrink toward the timer's resolution.
  • M/T-method (the practical blend). Count whole edges in the window and use the timer to capture the fractional period at each end of the window. This gives smooth, accurate velocity across the entire speed range and is what real servo drives implement. It is the standard answer to "how do I get clean velocity at both 1 rpm and 6,000 rpm from one encoder."

How the disc and read head are built

The sensing element splits into a few families, all producing the same A/B/Z output but with very different cost, robustness, and resolution ceilings:

  • Optical. A glass or metal disc with radial slots passes between an LED and two photodetectors spaced a quarter pitch apart. Highest resolution (up to tens of thousands of lines on a glass disc using interpolation), but sensitive to dust, oil, and shock. The default for precision CNC and lab instruments.
  • Magnetic. A magnetized ring with alternating north/south poles passes over two Hall-effect or magnetoresistive sensors offset by a quarter pole pitch. Robust against contamination, vibration, and temperature; lower native resolution but heavily used in automotive, off-highway, and harsh industrial drives.
  • Capacitive. An etched rotor changes the coupling between transmit and receive electrodes; signal processing extracts a high-resolution sinusoid that is interpolated into quadrature. Compact and immune to magnetic fields — the technology inside many digital calipers and the US Digital capacitive encoder line.
  • Sin/cos with interpolation. Rather than slots producing square waves directly, the read head produces two analog sinusoids 90° apart (sine and cosine). An interpolator computes the arctangent of their ratio to subdivide one electrical cycle into hundreds or thousands of steps, then re-emits clean A/B quadrature. This is how a 2,048-line disc reaches millions of counts per revolution in high-end servo encoders.

Quadrature versus the alternatives

The closest alternatives are a single-channel tachometer encoder, a true absolute encoder, and a resolver. Each trades resolution, cost, and power-loss behavior differently:

PropertyQuadrature (incremental)Single-channelAbsolute encoderResolver
Direction sensingYes (A/B phase)NoYes (inherent)Yes
Position at power-upLost — must re-home on indexLostKnown instantlyKnown instantly
Resolution4× PPR; 4,000–millions CPR1× PPRBit-depth of code disc (e.g. 17-bit)Interpolated, ~12–16-bit
Output2–3 square waves (A, B, Z)1 square waveSerial code (SSI, BiSS, EnDat) or parallel Gray2 analog sinusoids
CostLowLowestHighMedium–high
Environment toleranceOptical poor / magnetic goodSame as base techSame as base techExcellent (rugged, hot)
Typical useServos, robot joints, CNC, scroll wheelsFans, flow meters, RPM tachsMulti-turn joints, safety axesAerospace, traction motors

The headline trade-off is power-loss behavior versus cost. A quadrature encoder is the cheapest way to get high-resolution direction-and-position feedback, but it forgets where it is when power drops. An absolute encoder remembers, at several times the price and a serial protocol to read. Resolvers win where heat, vibration, and radiation would destroy an optical disc — traction motors, jet-engine accessories, missile fins — at the cost of an analog excitation and demodulation circuit.

Failure modes and the trade-offs that cause them

  • Standstill dither. When a controlled axis sits on a setpoint it can jitter back and forth across a single edge. A correct quadrature state machine counts these as +1 then −1 and nets to zero. A naive single-edge counter accumulates phantom counts and slowly drifts. The cure is to always decode true quadrature transitions, never just one channel's edges.
  • Electrical noise injecting false edges. Long unshielded cables next to a switching motor inverter pick up enough noise to fake an edge. The standard defense is differential RS-422 line drivers — each channel sent as a complementary pair (A and A̅, B and B̅) so a receiver rejects common-mode noise — plus Schmitt-trigger inputs whose hysteresis swallows glitches smaller than the band.
  • Exceeding maximum count rate. Shaft speed × CPR can outrun the decoder's bandwidth, dropping edges and slipping the count permanently. Size the encoder so peak edge frequency stays comfortably below the decoder's rated input frequency, and prefer differential signaling on fast spindles.
  • Phase error between A and B. If the two tracks are not exactly 90° apart (manufacturing tolerance, thermal drift, read-head misalignment), the four edges are no longer evenly spaced and 4×-decoded counts become non-uniform in angle. High-end encoders trim this; cheap ones can show several percent count-to-count angular error.
  • Optical contamination. Dust, oil mist, or condensation on an optical disc attenuates the beam until edges vanish. This is why magnetic or capacitive encoders dominate dirty environments despite lower native resolution.
  • Index miss or double-trigger. A marginal or un-gated index pulse can latch at the wrong count or be skipped entirely, corrupting the homing reference. Gating the index against a specific A/B state guarantees it always latches at the same count.

Design notes that catch people out

  • Spec from CPR, not PPR. The number on the box is one quarter of your usable resolution. Confusing the two over-specifies the disc by 4× and wastes money.
  • Budget the count rate before choosing line count. A high-PPR encoder on a fast shaft can exceed a microcontroller's timer input frequency; do the f_edges = (rpm/60) × CPR arithmetic first.
  • Always use the hardware quadrature decoder. Polling A and B in software invites missed edges under load. Dedicated timer/eQEP/counter peripherals validate legal transitions and never drop counts.
  • Plan a homing sequence. Incremental encoders have no absolute zero. Every power-up needs a move to the index pulse, or hard limit switches, to establish position.
  • Use differential signaling for anything beyond a desk. Single-ended A/B works on a breadboard; in a machine next to a motor drive, RS-422 pairs are non-negotiable.

Frequently asked questions

How does a quadrature encoder tell direction from just two channels?

The two output tracks, A and B, are offset so their square waves sit exactly 90° apart — a quarter of one cycle. Turn the shaft one way and channel A's rising edge arrives before B's; turn it the other way and B's edge arrives first. At every edge the decoder samples the other channel: if A rises while B is low it is one direction, if A rises while B is high it is the opposite. That single bit of phase order is the entire direction signal — a single-channel encoder produces identical pulses either way and can never tell forward from reverse.

What does 4× (quadrature) decoding mean and why does it quadruple resolution?

One full cycle of the A/B pair contains four evenly spaced transitions: A rises, B rises, A falls, B falls. A 1× decoder counts one of them per cycle, a 2× decoder counts both edges of A, and a 4× decoder counts every edge of both channels — four counts per disc line. So a 1,000-PPR disc yields 4,000 counts per revolution, an angular step of 360/4000 = 0.09°. The extra resolution is free: it comes from the 90° geometry, not from cutting finer slots, and hardware decoder peripherals do it by default.

What is the index (Z) channel for?

Channels A and B are incremental — they track how far the shaft has moved relative to power-up but have no absolute reference. The index channel (Z, or marker pulse) fires once per revolution at a fixed mechanical angle. On the first index pulse the controller zeroes or latches its count, establishing absolute home. That is why a CNC or servo performs a homing move at startup — it spins until the index appears, then knows exactly where zero is. Without it the encoder is purely relative.

How is a quadrature encoder different from an absolute encoder?

An incremental quadrature encoder outputs two pulse trains; the controller counts edges and remembers the total, so losing power loses position until you re-home on the index. It is cheap and has effectively unlimited resolution. An absolute encoder outputs a unique code for every angle (Gray-coded disc, magnetic array, or capacitive pattern), so position is known the instant power returns, with no homing — at higher cost, more output lines or a serial protocol, and a resolution capped by the code's bit depth. Use quadrature where a homing move is acceptable; use absolute where losing position at power-down is not.

What causes an encoder to miscount, and how is it prevented?

Three modes dominate. Standstill dither across one edge produces phantom counts that a proper quadrature state machine cancels but a naive single-edge counter accumulates. Electrical noise injects false edges — cured by differential RS-422 pairs (A/A̅, B/B̅) and Schmitt-trigger hysteresis. And exceeding the maximum count rate drops edges and slips the count permanently: a 5,000-PPR encoder (20,000 CPR after 4× decode) at 6,000 rpm produces 2 MHz of edges, so the decoder must keep up. Differential signaling, hysteresis, and a hardware quadrature decoder that validates legal state transitions are the standard defenses.

Can a quadrature encoder measure velocity as well as position?

Yes — most servo systems use it for both. The M-method counts edges in a fixed window (accurate at high speed, coarse at low speed); the T-method times the interval between edges (accurate at low speed, noisy at high speed); and the M/T-method blends them by counting whole edges in the window and timing the partial periods at each end, giving clean velocity across the whole range. The encoder's CPR sets the floor, so low-speed velocity leans on T-method timing rather than edge counting.