Rotary Encoder (Incremental / Absolute)
Sensor
Turns shaft rotation into digital pulses: quadrature A/B channels report every step and its direction. From the ₹30 knob on a 3D-printer panel to the 5000-line optical encoder closing a servo loop — position feedback is encoders.
What it looks like
Rotary Encoder (Incremental / Absolute)
A typical sensor — exact shape, colour and markings vary between manufacturers.
Types & variants
Key specs
Resolution
PPR / CPRPulses per revolution: 20–24 PPR panel knobs, 100–5000 PPR industrial. Quadrature decoding (both edges, both channels) multiplies PPR ×4 into counts (CPR).
Output channels
—A and B, 90° apart — B leading or lagging A encodes direction; an optional Z (index) pulse marks one point per rev for homing.
Output type
—Open-collector (needs pull-ups), push-pull, or differential line driver (RS-422 A/Ā B/B̄) for noisy industrial runs.
Max speed
RPM / kHzElectrical frequency = RPM/60 × PPR must stay within the encoder's and the MCU counter's limits — do this arithmetic before buying a 5000 PPR unit for a fast motor.
Absolute interface
—Absolute types speak SSI, BiSS, or Gray-code parallel — position survives power-off, no homing needed.
Markings
Industrial bodies label PPR, supply (5–24 V) and output type; wire colours per datasheet (commonly red 5V, black GND, white A, green B, yellow Z). KY-040 modules silk CLK (A), DT (B), SW (push switch).
Standard values
KY-040 20-PPR mechanical knob (every Arduino kit), 600/1000 PPR optical shaft encoders (₹500-class, the robotics staple), N20/JGA25 gearmotors with built-in magnetic quadrature encoders, AS5600 magnetic absolute module (12-bit I2C).
How to choose
1) UI knob: mechanical KY-040 (debounce in software). 2) Motor feedback: optical/magnetic incremental with PPR sized for your speed and precision; differential outputs if wires run near motors. 3) Position that must survive power-off: absolute (AS5600 for hobby, SSI/BiSS industrially). 4) Dusty/oily sites: magnetic over optical. 5) Check shaft type — solid vs hollow bore vs magnet-over-chip.
Pinout & package
Panel knobs: 3 pins + switch. Shaft encoders: 38 mm bodies, 6 mm shafts, flexible couplings. Motor encoders: PCB with magnetic disc on the rear shaft. Read via MCU interrupts, dedicated quadrature-counter peripherals (ESP32 PCNT, STM32 timers), or HCTL/LS7366 counter ICs.
Example circuits
- Menu knob: KY-040 on two interrupts scrolling an OLED menu
- PID speed control: gearmotor encoder feedback vs setpoint driving a PWM H-bridge
- Robot odometry: two wheel encoders dead-reckoning position
- CNC homing: Z-index pulse + limit switch establishing machine zero
Common failures
Mechanical types: contact bounce and wear (skipped/double detents — the KY-040's known vice). Optical: dust on the disc, dead IR emitter. All: miscounts from noise on long unshielded runs (fix: differential signals, shielded cable), crushed shafts/couplings, and counts lost to slow polling (use interrupts/hardware counters).
How to test
Scope A and B while turning slowly: clean 90°-offset square waves, phase order flipping with direction. On a bench MCU, a simple count sketch should track detents exactly. For mechanical knobs, watch the bounce on the scope — then tune debounce accordingly.
Substitutes
Potentiometers substitute for limited-angle absolute position (cheap, analog). Tachogenerators give speed only. Hall sensors on a magnet ring make a coarse DIY encoder. Steppers open-loop substitute for position control where missed steps are tolerable — encoders exist precisely because they aren't.