Encoders are the eyes of a CNC machine. Every servo loop, every stepper verification, every closed-loop motion system depends on accurate, reliable position feedback. Two technologies dominate the encoder market: optical encoders and magnetic encoders. Both convert rotational (or linear) motion into electrical pulses, but they do so through fundamentally different physical principles — and that difference matters enormously in the harsh, contaminated environment of a CNC router, lathe, or plasma table.
How Optical Encoders Work
An optical encoder uses a disc (or linear strip) with alternating transparent and opaque regions etched or printed onto it. A light source — typically an infrared LED — shines through the disc onto a photodetector array. As the disc rotates, the photodetectors toggle between light and dark, generating electrical pulses. A second channel offset by 90° (quadrature) allows direction detection.
High-end optical encoders use a metal or glass disc with chrome-plated grating. Budget versions use plastic discs with printed patterns. The number of alternating lines per revolution (CPR — counts per revolution, or LPR — lines per revolution) is the fundamental resolution parameter. Quadrature encoding electronically multiplies this by 4× (counting both edges of both channels), so a 500 LPR disc gives 2,000 CPR in quadrature mode. Interpolation can push this further — some optical encoders reach 10 million counts per revolution.
How Magnetic Encoders Work
A magnetic encoder pairs a multi-pole permanent magnet ring (or disc) with a Hall-effect sensor IC or magneto-resistive (AMR/GMR) sensor. As the magnet rotates, the sensor detects changes in magnetic field direction and converts them to digital position data. Hall-effect encoders are typically lower resolution (12–14 bits per revolution = 4,096–16,384 CPR); advanced AMR/GMR sensors reach 19 bits (524,288 CPR). Most common integrated magnetic encoder ICs (AS5047P, MA730, MT6701) produce a digital SPI/ABZ output and require no additional decoder hardware.
The magnet and sensor need no physical contact and maintain a gap of 0.5–3 mm. There are no optical components to fog, crack, or contaminate. This robustness is the magnetic encoder’s defining advantage.
Resolution and CPR
| Encoder Type | Typical CPR Range | High-End CPR |
|---|---|---|
| Optical (incremental) | 100–10,000 | Up to 10,000,000 (with interpolation) |
| Magnetic (Hall-effect IC) | 256–4,096 | 16,384 |
| Magnetic (AMR/GMR) | 4,096–131,072 | 524,288 |
What CPR does your CNC need?
Required CPR depends on the mechanical transmission and desired linear resolution. For a ball screw with 5 mm pitch driving a servo motor:
- At 1,000 CPR: linear resolution = 5 mm / 1000 = 0.005 mm (5 microns) — adequate for woodworking CNC
- At 5,000 CPR: 1 micron resolution — adequate for aluminium milling
- At 10,000 CPR: 0.5 micron resolution — needed for precision grinding, PCB drilling
A standard hobbyist CNC router with 2 mm pitch GT2 belt and 1:1 drive needs at least 400 CPR to achieve 0.1 mm positioning. For servo-driven ball screw machines, 2,500–5,000 CPR is the typical sweet spot balancing cost, speed, and resolution.
Contamination and Environmental Resistance
This is where optical and magnetic encoders diverge most dramatically for CNC applications.
Optical Encoder Vulnerabilities
- Coolant and cutting fluid: Misting coolant can coat the LED, disc, and photodetector. Even a thin film of oil on the glass disc scatters light and causes missed pulses. Result: position errors that accumulate into tool crashes.
- Metal swarf: Fine aluminium or steel chips can physically scratch the disc grating, permanently degrading resolution.
- Dust: Wood routers generate fine particulate that clogs the optical path. A single large particle blocking the disc creates a persistent position error.
- Vibration: High-frequency vibration can cause the disc to wobble out of the narrow optical gap, especially with unsupported shaft-through designs.
Sealed IP67-rated optical encoders exist but add significant cost (₹3,000–20,000) and bulk. Most budget optical encoders are IP40 or unrated.
Magnetic Encoder Advantages
Magnetic encoders have no optical path. Coolant, oil, grease, chips, and dust do not affect the Hall-effect or magneto-resistive sensor as long as they do not completely displace the air gap with ferromagnetic material. Stainless steel and aluminium chips are non-magnetic — completely harmless. Only iron or steel chip accumulation directly in the gap could theoretically cause issues, and this is prevented by a simple non-magnetic shroud.
The magnet and sensor can be separated by a sealed bearing or even a thin stainless steel wall. Many CNC builders mount the magnet inside the motor hub and the sensor outside the motor, reading through a 1–2 mm gap with zero wear and zero contamination risk.
Maximum Speed and Frequency Response
At high RPM the encoder output frequency can overwhelm the decoder input. At 3,000 RPM with a 1,000 CPR encoder in quadrature mode: output frequency = 3,000/60 × 1,000 × 4 = 200,000 Hz. Arduino’s hardware interrupt-based decoder can handle this, but software polling cannot. At 10,000 CPR and 6,000 RPM, the frequency reaches 4 MHz — requiring dedicated motion controller chips (FPGA or DSP-based) or hardware encoder counters.
Optical encoders with fine pitch discs can mis-read at high speeds if the photodetector slew rate cannot follow the pulse edges — they specify a maximum line frequency (kHz) in the datasheet. Magnetic encoders using SPI (reading absolute position registers) are rate-limited by SPI clock speed and sample rate — typically 10,000–100,000 position reads per second, which translates to angular bandwidth, not pulse frequency.
Positional Accuracy and Repeatability
Optical encoders with glass discs and fine grating achieve sub-arc-second accuracy — better than most CNC mechanical systems. The disc grating is typically accurate to ±0.1% of one cycle, which at 2,500 LPR means ±0.144 arc-seconds systematic error per line. Eccentricity of the disc mounting contributes a once-per-revolution sinusoidal error that can be compensated in firmware.
Magnetic encoders have two accuracy considerations: magnet pole uniformity (affects cyclical error) and sensor interpolation accuracy. Hall-effect sensors have typically ±1–2° accuracy per pole pair, which at 8 pole pairs = ±0.125–0.25° per revolution. AMR-based encoders (AS5047P family) achieve ±0.18° (±10.8 arc-minutes). For CNC with mechanical backlash typically 0.02–0.1 mm, this level of accuracy is more than sufficient — the mechanical system is usually the limiting factor, not the encoder.
Installation and Alignment Requirements
Optical Encoder Installation
Optical encoders require careful alignment of the disc relative to the optical sensor. The gap between disc and reader is typically 0.3–0.5 mm — tight enough that vibration can cause intermittent contact. Shaft encoders (mounted directly on motor shaft) are simpler to install. Linear encoders require parallel strip alignment within ±0.2 mm over the full travel. Mounting errors cause brightness variations that degrade signal quality.
Magnetic Encoder Installation
Magnetic encoders tolerate much wider mounting gaps (0.5–3 mm). Axial and radial eccentricity up to ±0.5 mm is typically acceptable. The magnet can be press-fit onto a shaft stub or embedded in a rotor hub with epoxy. No optical path to protect means the installation tolerances are friendly to home workshop manufacturing methods. Many open-source CNC servo drives (ODrive, VESC) are designed around the AS5047P magnetic encoder specifically because it is easy to integrate.
Cost Comparison
| Product Category | Optical | Magnetic |
|---|---|---|
| Budget rotary encoder module (hobby) | ₹60–150 | ₹150–400 (IC only) |
| Industrial incremental (600–2500 CPR) | ₹800–3,000 | ₹600–2,000 |
| Sealed/IP67 industrial | ₹3,000–20,000 | ₹800–4,000 |
| High-resolution (>10,000 CPR) | ₹5,000–50,000 | ₹2,000–8,000 (AMR) |
Magnetic encoders deliver better contamination resistance at lower or comparable cost, which explains their growing adoption in industrial servos and CNC applications.
Selection Guide by CNC Machine Type
Wood Router / Laser Engraver (Open Loop Stepper)
Most wood routers use open-loop steppers with no encoder at all. If adding encoder feedback (closed-loop stepper driver), the environment is dusty from wood chips. A magnetic encoder is far more appropriate: mount the AS5047P or MT6701 directly on the stepper motor rear shaft. No optical path to clog, easy installation through the motor end cap.
Aluminium / PCB Mill (Servo System)
Aluminium and PCB milling generate coolant mist and conductive chips. Optical encoders can work if fully sealed (IP67), but add cost and complexity. Magnetic encoders are preferred by most DIY CNC servo builders using ODrive or custom BLDC drives. A 14-bit (16,384 CPR) magnetic encoder on a 5 mm pitch ball screw gives 0.3 micron theoretical linear resolution — more than adequate.
Plasma / Waterjet Cutting Table
The plasma environment is extremely hostile: electromagnetic interference (EMI) from the arc, water vapour, and metallic dust. Optical encoders are vulnerable to EMI-induced signal corruption on their analog photodetector outputs. Magnetic encoders with differential ABZ output lines (RS-422) and shielded cables handle this environment better. Linear magnetic scales (magnetic tape + read head) are purpose-designed for plasma tables.
Precision Grinding / Surface Grinder
Grinding requires the highest positional accuracy, with tolerances of 1–5 microns. This is where optical encoders shine — a sealed 10,000 CPR optical encoder from Heidenhain or Renishaw is the industry standard. The machine enclosure protects the encoder, coolant is water-based (not oil mist), and the grinding wheel’s regular rotary motion does not generate the random debris of a router. Budget: ₹20,000–200,000 for these units.
3D Printer / Delta Robot Arm
Clean environment, moderate speeds, low cost priority. Either works. Budget optical encoders in the ₹100–300 range are common in delta robots and some CoreXY printers. However, for BLDC-servo 3D printer conversions, magnetic encoders (AS5047P) are near-universally used because of the simple mounting on the motor rear shaft.
Reading Encoders: Quadrature Decoding on Arduino
// Hardware interrupt-based quadrature decoder
// Connect encoder A to D2 (INT0), B to D3 (INT1)
volatile long encoder_pos = 0;
void setup() {
Serial.begin(115200);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), enc_isr_A, CHANGE);
attachInterrupt(digitalPinToInterrupt(3), enc_isr_B, CHANGE);
}
void enc_isr_A() {
bool A = digitalRead(2);
bool B = digitalRead(3);
encoder_pos += (A == B) ? 1 : -1;
}
void enc_isr_B() {
bool A = digitalRead(2);
bool B = digitalRead(3);
encoder_pos += (A != B) ? 1 : -1;
}
void loop() {
static long last_pos = 0;
long pos = encoder_pos; // atomic read
if (pos != last_pos) {
Serial.println(pos);
last_pos = pos;
}
}
For high-CPR encoders at high speeds, use the Encoder library or, better, a dedicated encoder counter IC (LS7366R) or an FPGA. The LS7366R handles 32-bit quadrature counting in hardware, freeing the microcontroller for motion control math.
Recommended Products from Zbotic
ACS712 20A Current Sensor Module
Used in CNC servo drives for current feedback — pairs with encoder position feedback for complete closed-loop FOC motor control.
INA219 I2C Bi-directional Current / Power Monitoring Module
Ideal for CNC power supply monitoring — measures bus voltage and current over I2C, complementing encoder-based position feedback in servo systems.
Benewake AD2-S-X3 Automotive-Grade LiDAR
For AGV and autonomous CNC tool changers requiring absolute position sensing beyond what rotary encoders provide — full 3D environmental mapping.
Frequently Asked Questions
Can I replace an optical encoder on my servo motor with a magnetic encoder?
Yes, if the servo drive supports the output format (ABZ quadrature, SPI, or BiSS-C). Many open-source servo drives like ODrive explicitly support the AS5047P magnetic encoder. For commercial servo drives, check whether the encoder port accepts incremental ABZ signals — most do, and a magnetic encoder producing compatible ABZ output is a direct drop-in replacement. Just match the CPR value in the drive firmware.
What is the difference between incremental and absolute encoders?
An incremental encoder outputs pulses as the shaft rotates. Position is tracked by counting pulses from a reference (home) position. After power loss, you must home the machine. An absolute encoder outputs a unique code representing the absolute angular position at any moment — no homing required after power cycling. Most magnetic encoder ICs like AS5047P are absolute (they know position without movement). Optical absolute encoders are expensive; magnetic absolute encoders are affordable and therefore preferred for CNC homing systems.
My optical encoder gives intermittent missed pulses at high speed. What is wrong?
The most common causes: (1) Photodetector frequency bandwidth exceeded — check maximum line frequency spec; (2) Power supply noise on the 5V rail causing false triggers — decouple with 100 nF ceramic cap directly at the encoder; (3) Long unshielded cables picking up noise from motor PWM — use shielded twisted-pair cable; (4) Dirty disc — clean gently with isopropyl alcohol; (5) Software decoder missing pulses — switch to hardware interrupt or dedicated counter IC.
Do magnetic encoders work with stepper motors?
Yes. Closed-loop stepper drives (Trinamic TMC4671, StepperOnline iHSS, StepperStudio drivers) commonly use magnetic encoders on the stepper rear shaft for stall detection and position verification. The encoder feedback lets the driver detect missed steps and either correct them (true closed loop) or alarm and halt. This is far cheaper than converting to servo and gives most of the positioning reliability benefits.
What encoder CPR is needed for 0.01 mm resolution on a CNC router?
It depends on the mechanical drive. For a GT2 belt with 20-tooth pulley (40 mm circumference): CPR = 40 mm / 0.01 mm = 4,000 CPR per revolution. In quadrature (4× mode), a 1,000 LPR optical encoder achieves this. For a 5 mm pitch ball screw: CPR = 5 mm / 0.01 mm = 500 CPR — even a basic 360 CPR magnetic encoder covers this with 4× quadrature (1,440 effective CPR). In practice, mechanical backlash (typically 0.02–0.1 mm on budget hardware) limits achievable positioning accuracy far below encoder resolution.
Explore Zbotic’s sensors and measurement modules including current sensors and motion feedback components. Free shipping above ₹999, same-day dispatch.
Add comment