Sign In / Join Cloud 360
Engineering • Innovation • Learning
Power Electronics & Motion Advanced 20 min

Closed-Loop DC Motor Velocity Control with Optical Quadrature Encoder

Harness hardware microcontroller timer encoder modes to decode high-resolution quadrature signals and regulate motor velocity under varying mechanical loads.

2. Prerequisites & Background Theory:

H-Bridge MOSFET commutation, quadrature phase decoding, back-EMF dynamics, and discrete frequency measurement.

STEP 1

Goal & Engineering Objectives

Maintain constant rotational shaft velocity of a high-torque DC gearmotor under severe dynamic load disturbances using hardware quadrature decoding and high-frequency PWM switching.

STEP 2

Governing Mathematical Formulation & Physics

Quadrature Encoding (4X Decoding Principle): Two optical channels (Channel A and Channel B) are physically offset by 90° electrical degrees (quadrature phase). By counting both the rising and falling edges of both channels, the effective measurement resolution quadruples: $$N_{\text{edges/rev}} = 4 \times \text{PPR}$$ Rotational speed in RPM over a sampling period $\Delta t$ is given by: $$\text{RPM} = \frac{\Delta \text{Edges} \times 60}{4 \times \text{PPR} \times \Delta t}$$ Flyback Inductive Kickback Protection: $$V = L \frac{di}{dt}$$ When PWM turns OFF, winding inductance forces current to continue flowing. Fast-recovery antiparallel Schottky diodes redirect this freewheeling energy safely to the supply rail.
STEP 3

System Architecture & Block Flow Diagram

[12V DC Motor with Optical Encoder] <==== (Shaft Coupling) ====> [Mechanical Load]
       |                 ^
  (Enc A / B)       (High-Side / Low-Side PWM)
       v                 |
[STM32 Timer 3]   [BTS7960 / L298N H-Bridge]
  (Encoder Mode)         ^
       |                 |
       +---> [Discrete PID Task] ---+
STEP 4

Required Hardware Bill of Materials (BOM)

Hardware Component Qty Engineering Specification
12V DC Planetary Gearmotor with Integrated Encoder 1 500 PPR optical disk, 12V DC, 450 RPM nominal
BTS7960 High-Current 43A H-Bridge Motor Driver 1 Integrated low-side current sensing and thermal protection
STM32F401 "Black Pill" Development Board 1 Hardware Timer quadrature encoder input
12V 10A Benchtop DC Power Supply 1 Regulated motor drive voltage source
Schottky Flyback Diodes (1N5822) 4 Ultra-fast reverse recovery time (< 30 ns)
STEP 5

Wiring, Pinout & Schematics Mapping

• Encoder Interface: - Motor Encoder Channel A -> STM32 PA6 (TIM3_CH1) - Motor Encoder Channel B -> STM32 PA7 (TIM3_CH2) - Encoder VCC -> STM32 3.3V / 5.0V - Encoder GND -> STM32 GND • Driver Interface: - STM32 PA8 -> BTS7960 RPWM (Forward PWM) - STM32 PA9 -> BTS7960 LPWM (Reverse PWM) - STM32 3.3V -> BTS7960 R_EN & L_EN (Enable Bridges) • Power Leads: - 12V Supply (+) -> BTS7960 B+ terminal - 12V Supply (-) -> BTS7960 B- terminal & STM32 GND - Motor Winding (+) -> BTS7960 M+ terminal - Motor Winding (-) -> BTS7960 M- terminal.
STEP 6

Firmware Implementation & Algorithms

// Configure STM32 Hardware Timer for 4X Quadrature Decoding
void init_timer_encoder(void) {
    RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;   // Enable TIM3 clock
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;  // Enable GPIOA clock
    
    // Configure PA6 and PA7 as Alternate Function AF2 (TIM3)
    GPIOA->MODER |= (2 << GPIO_MODER_MODER6_Pos) | (2 << GPIO_MODER_MODER7_Pos);
    GPIOA->AFR[0] |= (2 << GPIO_AFRL_AFSEL6_Pos) | (2 << GPIO_AFRL_AFSEL7_Pos);
    
    // Set SMS = 011: Encoder mode 3 (counts both TI1 and TI2 edges for 4X resolution)
    TIM3->SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1;
    TIM3->ARR = 0xFFFF; // 16-bit auto-reload
    TIM3->CR1 |= TIM_CR1_CEN; // Start Timer 3
}

int16_t get_encoder_delta(void) {
    static uint16_t last_count = 0;
    uint16_t current_count = TIM3->CNT;
    int16_t delta = (int16_t)(current_count - last_count);
    last_count = current_count;
    return delta;
}
STEP 7

Bench Testing & Waveform Verification

1. Manually rotate motor shaft exactly 1 full revolution by hand: Confirm the encoder count increases by exactly $2000$ counts ($4 \times 500\text{ PPR}$). 2. Command a setpoint of $200\text{ RPM}$: Apply a mechanical friction brake to the output shaft and confirm the controller increases PWM duty cycle to maintain shaft speed within $\pm 1.5\%$. 3. Verify current shunt feedback voltage with a scope: Confirm absence of large reverse voltage flyback spikes exceeding 16V.
STEP 8

Troubleshooting & Common Failure Modes

• Problem: Motor immediately runs away at 100% full speed in the wrong direction upon engaging the feedback loop. Solution: Positive feedback loop! Swap Encoder Channel A and Channel B wires, or invert the sign of the error in firmware. • Problem: Random missed encoder counts at high RPM. Solution: Long unshielded encoder leads picking up motor PWM EMI. Use shielded twisted pair cable with the shield drain wire tied to earth ground at the controller end only.
STEP 9

Extensions & Undergraduate Research Challenges

1. Implement Field-Oriented Control (FOC) for 3-phase BLDC motors using space-vector PWM (SVPWM) and Clark/Park matrix transformations. 2. Incorporate an active current-sensing control loop (cascaded outer velocity loop + inner current/torque loop) to enforce instant motor torque limiting.
All Engineering Guides
Closed-Loop Discrete PID Control & Inverted Pendulum Balancing
Control Systems & Robotics • 25 min
CAN-Bus 2.0B Telemetry Node with Industrial Sensor Fusion
Embedded Systems & Automotive • 20 min
Precision Dual-Axis Stepper Motion Controller with S-Curve Acceleration
Mechatronics & Motion Control • 22 min
IoT Environmental Monitoring Station with OLED & Secure Telemetry
Internet of Things (IoT) • 18 min
Closed-Loop DC Motor Velocity Control with Optical Quadrature Encoder
Power Electronics & Motion • 20 min
Astable 555 Timer & 74HC595 Shift Register Digital Sequencer
Discrete Electronics & Digital Logic • 16 min
Field-Oriented Control (FOC) & Space Vector Modulation for BLDC Motors
Power Electronics & Motors • 28 min
High-Efficiency Synchronous Buck DC-DC Converter Design
Power Electronics & Energy • 24 min
Discrete Kalman Filter IMU Sensor Fusion for Tilt & Orientation
Control Systems & Robotics • 26 min
Industrial Modbus RTU Slave Node over Galvanically Isolated RS-485
Industrial Automation • 22 min
4th-Order Sallen-Key Active Butterworth Filter for Sensor Anti-Aliasing
Electronics Fundamentals • 22 min
Mobile Robot Differential Odometry with 4X Hardware Quadrature Decoding
Robotics & Automation • 24 min
Discrete N-Channel MOSFET H-Bridge with Bootstrap Gate Drive
Electronics & Motor Control • 25 min
High-Speed SPI NOR Flash Black-Box Flight Data Logger
Embedded Systems & IoT • 22 min
MPU-6050 Digital Motion Processor (DMP) Quaternion Spatial Tracking
Sensors & Embedded Algorithms • 24 min
Deterministic Multitasking Architecture with FreeRTOS Kernel
Embedded Systems & Software • 25 min
Ask LUNA About This Lab

Ask LUNA for circuit calculations, state-space equations, tuning parameters, or component substitutes.

Hardware Catalog

Inspect verified electrical specs, voltage rails, and pinouts across all 10 component categories.

Browse Components →