Sign In / Join Cloud 360
Engineering • Innovation • Learning
Sensors & Transducers Intermediate Level 20 min

Optical Quadrature Encoders: Decoding & Velocity Estimation

Channel A/B 90° phase offset, 1X/2X/4X state-machine decoding, hardware timer quadrature decoders, and digital filtering for velocity estimation.

Prerequisites: Digital logic square waves, microcontroller external interrupts, timer capture registers, and angular kinematics.

1. Overview & Concept Definition

Rotary electromechanical sensor that converts the angular displacement and rotational velocity of a mechanical shaft into two square-wave digital output signals phase-shifted by 90 electrical degrees.

2. Engineering & Industrial Significance

Enables closed-loop positioning and speed regulation in motor control, CNC milling machines, and autonomous robotics with sub-millimeter positioning precision and immediate direction sensing.

3. Physical Working Principle & Transducer Dynamics

An etched glass or slotted plastic code disc spins between infrared LED emitters and photodiode receivers. The optical gratings produce two square-wave channels ($A$ and $B$) in electrical quadrature ($90^\circ$ relative phase shift). Clockwise rotation causes Channel A to lead Channel B; counter-clockwise rotation causes Channel B to lead Channel A. By monitoring rising and falling edges of both channels, a 4X hardware decoding counter achieves four counting pulses per physical grating cycle ($CPR_{effective} = 4 \times CPR_{optical}$). An optional Channel Z (Index) pulse fires once per mechanical revolution for absolute zero-reference homing.

4. Governing Equations & Mathematical Models

Effective Counts per Revolution: $PPR_{4X} = 4 \times PPR_{optical}$
Angular Resolution: $\Delta\theta = \frac{360^\circ}{PPR_{4X}} = \frac{2\pi}{4 \times PPR_{optical}} \text{ radians}$
Velocity by Frequency (Pulse Counting): $\omega = \frac{2\pi \cdot \Delta N}{PPR_{4X} \cdot T_s} \text{ rad/s}$
Velocity by Period (Time Interval): $\omega = \frac{2\pi}{PPR_{4X} \cdot \Delta t_{pulse}} \text{ rad/s}$
Maximum Slew Rate Frequency: $f_{max} = \frac{RPM_{max} \times PPR_{optical}}{60} \text{ Hz}$

5. Practical Implementation

// C++: State-Machine Quadrature Decoder Table (4X Resolution)
#include <stdint.h>

// Gray code state-transition table: returns +1 (CW), -1 (CCW), 0 (no move), 2 (invalid/missed step)
static const int8_t QUAD_TABLE[16] = {
     0, -1,  1,  2,
     1,  0,  2, -1,
    -1,  2,  0,  1,
     2,  1, -1,  0
};

struct QuadratureEncoder {
    volatile int64_t positionCount;
    uint8_t prevState;

    void init(uint8_t pinA, uint8_t pinB) {
        positionCount = 0;
        prevState = ((pinA & 1) << 1) | (pinB & 1);
    }

    void update(uint8_t pinA, uint8_t pinB) {
        uint8_t currentState = ((pinA & 1) << 1) | (pinB & 1);
        uint8_t index = (prevState << 2) | currentState;
        int8_t movement = QUAD_TABLE[index];
        if (movement != 2) {
            positionCount += movement;
        }
        prevState = currentState;
    }
};

6. Common Engineering Pitfalls & Troubleshooting

Frequent Engineering Pitfalls
  • Using simple edge interrupts on noisy mechanical switch encoders without debouncing: Contact bounce triggers dozens of spurious interrupts, corrupting position counters. Optical encoders do not bounce, but high-speed electrical cable noise requires Schmitt triggers and twisted shielded cabling.
  • Velocity estimation quantization error at low speeds: Counting pulses over a fixed interval $T_s$ results in severe velocity step quantization when only 0 or 1 pulses occur per sample period. Switch to pulse-interval timer measurement at low RPM.
Diagnostic & Troubleshooting Checklist
  • Encoder counts backwards when motor moves forward: Swap physical wiring of Channel A and Channel B, or invert the direction sign in software.
  • Position drift over long operational runs: Test whether Channel Z pulses align with expected count modulo $PPR_{4X}$. Missing counts indicate input interrupt latency or electrical noise spikes on ground rails.

7. Applications & Physical Constraints

TYPICAL APPLICATIONS
  • Servo motor closed-loop position and velocity controllers (CNC, 3D printers)
  • Mobile robot wheel odometry dead-reckoning navigation
  • Haptic force-feedback teleoperation rotary input dials
PHYSICAL LIMITATIONS
  • Relative incremental encoders lose absolute position reference during power outage or reset.
  • High resolution at high RPM requires high-speed hardware timer decoding blocks to avoid overwhelming MCU CPU interrupts.
Pinout & Interface Specs
PIN / SIGNAL FUNCTION / ELECTRICAL SPEC
VCC +5V DC sensor power supply
GND Sensor signal ground
Channel A Quadrature Phase A square-wave output (pulled up to VCC)
Channel B Quadrature Phase B square-wave output (pulled up to VCC)
Channel Z / Index Single pulse per revolution zero-point reference
Have questions on this topic?

Ask LUNA to calculate component values or explain alternative control methods for Optical Quadrature Encoders: Decoding & Velocity Estimation.