Overview

Encoder blocks provide hardware-based position and velocity measurement using quadrature encoders, essential for motor control and motion feedback applications. The MPLAB Blockset offers dedicated encoder interfaces (QEI, PDEC, QDEC) that decode incremental encoder signals with zero CPU overhead, enabling precise position tracking and velocity calculation for motor control feedback loops.

Available Encoder Blocks

QEI - Quadrature Encoder Interface for dsPIC


PDEC - Position Decoder for SAM ARM


QDEC SAMx - Quadrature Decoder using TC for SAM


ChangeNotification - Software Encoder Decoding


Block Selection Guide

By Device Family

Device FamilyRecommended BlockAlternativeNotes
dsPIC30F / 33F / 33EQEIChangeNotificationQEI preferred for motor control
dsPIC33C / 33AQEIChangeNotificationEnhanced QEI with improved filtering
SAM E5x / D5x / E7x / S7x / V7xPDECQDEC_SAMxPDEC preferred for angular output
SAM C20 / C21 / D20 / D21QDEC_SAMxChangeNotificationTC-based quadrature mode
PIC24ChangeNotification-No dedicated QEI on PIC24
PIC32ChangeNotification-No dedicated QEI on PIC32

By Encoder Speed

Encoder SpeedRecommended ApproachMaximum PPRBlock Choice
< 1000 RPMSoftware or HardwareUp to 5000 PPRChangeNotification or QEI
1000-5000 RPMHardware onlyUp to 2048 PPRQEI, PDEC, QDEC_SAMx
5000-10000 RPMHardware with filteringUp to 1024 PPRQEI (4x mode), PDEC
> 10000 RPMHardware only, low PPRUp to 512 PPRQEI with digital filter

PPR Calculation: Maximum edge frequency = (RPM × PPR × 4) / 60

  • Example: 10000 RPM × 1024 PPR × 4 = 682 kHz edge rate (challenging for most MCUs)

By Application Requirements

ApplicationBlock ChoiceWhy?
3-Phase FOC Motor ControlQEI (dsPIC) or PDEC (SAM)Hardware velocity measurement, zero CPU overhead
Servo PositioningQEI or PDECIndex pulse support, high-resolution position
Robotic JointPDEC or QEIAngular position output, multi-turn tracking
BLDC Sensorless + Encoder BackupQEI or PDECRedundant position feedback
Low-Cost Motor ControlQDEC_SAMxTC-based, available on entry-level SAM
Legacy Device EncoderChangeNotificationSoftware decoding, no dedicated peripheral needed

Encoder Fundamentals

Quadrature Encoding Basics

Quadrature encoder signals:

  • Channel A: Primary position signal
  • Channel B: Phase-shifted signal (90° ahead for forward rotation)
  • Index (Z): One pulse per revolution (optional, for absolute reference)

Decoding Modes:

  • 1x Mode: Count edges of Channel A only (lowest resolution)
  • 2x Mode: Count rising and falling edges of Channel A
  • 4x Mode: Count all edges of A and B (highest resolution, 4× PPR)

Direction Detection:

  • Forward: Channel A leads Channel B by 90°
  • Reverse: Channel B leads Channel A by 90°

Position and Velocity Calculation

Position:

Position (counts) = Edge_Counter / 4   (for 4x mode)
Position (mechanical degrees) = (Position_counts / PPR) × 360°
Position (electrical degrees) = Position_mechanical × Pole_Pairs

Velocity:

RPM = (Delta_Position / Delta_Time) × 60 / PPR

Example:
- PPR = 1024
- Delta_Position = 5000 counts (in 4x mode)
- Delta_Time = 10 ms (0.01 s)
- RPM = (5000 / (1024 × 4)) / 0.01 × 60 = 7324 RPM

Hardware velocity measurement (QEI, PDEC):

  • Uses timer to measure interval between encoder edges
  • More accurate at low speeds than position differentiation
  • Automatically handles direction

Encoder Wiring and Electrical Considerations

Encoder Types

Encoder TypeOutputsVoltageRecommended PullUse Case
Push-Pull (TTL)Active high/low5V or 3.3VNoneHigh-speed, short cables
Open CollectorOpen drain5-24VExternal pull-up (2.2kΩ - 10kΩ)Industrial, long cables
Differential (RS-422)A/A-, B/B-5V differentialLine receiver ICNoisy environments, long cables (>10 m)

Noise Immunity

Digital Filter (QEI/PDEC):

  • Purpose: Reject switch bounce and electrical noise
  • Setting: 3-7 clock cycles typical
  • Trade-off: More filtering = slower response to high-speed edges

Hardware Recommendations:

  • Twisted pair wiring: Reduces EMI pickup
  • Shielded cable: Ground shield at one end only
  • Bypass capacitors: 100 nF at encoder connector (close to MCU pins)
  • Series resistors: 100Ω on A/B/Z lines (current limiting, ESD protection)

Troubleshooting Common Issues

Encoder Not Counting

Check:

  • ✅ Encoder power supply (5V or 12V/24V for industrial encoders)
  • ✅ Ground connection between encoder and MCU
  • ✅ QEI/PDEC peripheral enabled in block
  • ✅ Pin assignments correct (use Port Info block to verify)
  • ✅ Encoder actually rotating (use Digital_Input block to check raw signals)

Wrong Direction

Check:

  • ✅ Swap Channel A and B in block configuration
  • ✅ Or use “Invert Direction” option if available
  • ✅ Verify encoder wiring matches datasheet

Position Jumps or Noise

Check:

  • ✅ Increase digital filter setting (5-7 clock cycles)
  • ✅ Add external 100 nF capacitors on A/B/Z lines
  • ✅ Use shielded twisted pair cable
  • ✅ Check for EMI sources (PWM switching, motor cables)
  • ✅ Reduce maximum encoder speed (lower PPR encoder)

Velocity Measurement Unstable

Check:

  • ✅ Use hardware velocity measurement (QEI/PDEC) instead of position differentiation
  • ✅ Increase velocity measurement time window
  • ✅ Add low-pass filter on velocity output (cutoff ~10-50 Hz)
  • ✅ Check encoder PPR not too high for motor speed range

Device Family Support Summary

Encoder BlockdsPIC30FdsPIC33FdsPIC33EdsPIC33CdsPIC33APIC24PIC32SAM E5xSAM E7xSAM C2x
QEI
PDEC
QDEC_SAMx⚠️⚠️
ChangeNotification

Legend: ✅ Full Support | ⚠️ Available but PDEC preferred | ❌ Not Available


See Also

Related Block Categories:

Application Notes:

  • AN1078 - Sensorless Field-Oriented Control with Encoder Option
  • AN1292 - Sensorless FOC with Encoder Backup
  • DS70601B - QEI Module Family Reference Manual

Example Projects:


💡 Quick Selection Guide:

  • Using dsPIC for motor control? → QEI (hardware, zero overhead)
  • Using SAM E5x/E7x for motors? → PDEC (angular output, hardware velocity)
  • Using SAM C2x cost-optimized? → QDEC_SAMx (TC-based)
  • Need encoder on any device? → ChangeNotification (software, universally available)
  • High-speed encoder (> 5000 RPM)? → QEI or PDEC only (hardware required)

CN - Change Notification (Pin Interrupt)

Change Notification - Pin State Change Interrupts and Timing Overview The MCHP_CN block provides an interface to the Change Notification (CN) …

Read more →

PDEC - Position Decoder for SAM Devices

Position Decoder - Hall Sensors and Stepper Motor Interface Overview The MCHP_PDEC block provides an interface to the Position Decoder (PDEC) …

Read more →

QDEC SAMx - Quadrature Decoder for SAM Devices

Quadrature Decoder Using TC Modules - SAM Device Encoder Interface Overview The MCHP_QDEC_SAMx block implements quadrature encoder decoding using the …

Read more →

QEI - Quadrature Encoder Interface

Quadrature Encoder Interface - Position and Velocity Measurement Overview The MCHP_QEI block provides an interface to the Quadrature Encoder …

Read more →

MCHP QDEC Calibration (Non-Index)

Software-based position calibration for QDEC encoders without physical index signal Overview The QDEC Calibration (Non-Index) block provides …

Read more →