Marcio Cunha

How to control brushless DC motors with PWM, PID and encoders

Learn how to drive brushless motors using MOSFET power switches, encoder feedback, and practical PID speed control.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Brushless DC motors require precise electronic commutation of their coils through power switching transistors.
  • Pulse-width modulation varies the average applied voltage to regulate rotor speed efficiently.
  • Incremental encoders provide essential feedback regarding exact shaft position and instantaneous rotation.
  • The PID algorithm dynamically adjusts motor effort to eliminate errors between desired and actual speed.
  • Tuning proportional, integral, and derivative gains prevents abrupt oscillations and ensures mechanical stability.

Why brushless motors transformed modern robotics

Brushless direct current motors, known as BLDC, eliminated traditional carbon brushes that caused friction wear and sparks. Instead of mechanical parts switching polarity, an external electronic circuit commands current flow through the internal coils. In practice, this means the motor spins faster, runs cooler, and lasts much longer than older models. However, this intelligence requires careful planning of the drive hardware.

To make a BLDC motor work, we must alternate energy between stator phases following the rotor position. This process relies on high-speed semiconductor switches and microcontrollers capable of processing real-time signals. If you need to design efficient embedded systems, understanding this cycle is just as important as sizing batteries and solar panels for IoT nodes to guarantee energy autonomy in the field. The secret to operation lies in combining fast electrical pulses with high-power circuits.

Power driving with MOSFET transistors

The microcontroller alone provides only weak control signals, unable to directly power heavy motor coils. This is where MOSFET transistors come in, acting as ultra-fast electronic switches to control high currents with minimal energy loss. In a typical three-phase bridge, six MOSFETs organize alternative paths for current to flow through the three motor phases, creating the rotating magnetic field.

Managing these switches requires careful attention to transition times, because turning on one transistor before turning off another completely creates a catastrophic short circuit in the power supply. We use circuits called gate drivers to raise the control voltage and speed up transistor opening. When the system operates synchronously, pulse-width modulation steps in to control rotor speed smoothly and efficiently.

Controlling speed with PWM and inverters

Pulse-width modulation, or PWM, is the technique used to vary energy delivered to the motor by switching the supply voltage at high frequency. If the signal stays on during half the cycle and off during the other half, the average applied voltage drops by half, reducing rotation. This rapid variation happens thousands of times per second, preventing noticeable motor stuttering and ensuring continuous operation.

However, applying raw open-loop PWM does not guarantee accuracy, because factors like mechanical load or friction alter real speed. To keep the shaft spinning at the programmed rotation regardless of required effort, the system must measure what is happening and automatically correct deviations. This is where position sensor readings and advanced algorithms become indispensable on the workbench.

Speed feedback with incremental encoders

An incremental encoder is a sensor coupled to the motor shaft that generates electrical pulses as it rotates, allowing precise calculation of speed and displacement. By counting these pulses at regular time intervals, the microcontroller finds out if the motor is spinning faster or slower than expected. Without this feedback, the controller would operate blind, unable to compensate for performance drops caused by mechanical resistance.

Reading precision depends on encoder resolution and the microcontroller's ability to capture hardware interrupts without missing pulses. With clear numerical data in memory, software calculates current error by subtracting measured speed from desired speed. This numerical error value serves as direct input for the mathematical correction step that commands system behavior.

Implementing PID speed control

The PID algorithm, standing for Proportional, Integral, and Derivative, is the standard mathematical tool for stabilizing dynamic engineering systems. The proportional term acts on current error, applying immediate correction proportional to the distance to the target. The integral term accumulates errors over time to eliminate persistent steady-state offsets caused by constant loads, while the derivative term anticipates future change trends.

In microcontroller code, we calculate the output by combining these three factors at each sampling cycle before updating the PWM duty cycle. When parameters are properly tuned, the motor reaches desired speed quickly and without unwanted oscillations. The code snippet below illustrates the basic structure of a PID loop applied to pulse-width adjustment:

float compute_pid(float setpoint, float current) {float error = setpoint - current;integral += error;float derivative = error - previous_error;float output = (Kp * error) + (Ki * integral) + (Kd * derivative);previous_error = error;return output;}

Practical challenges and gain tuning

Running a PID system in practice requires patience when tuning Kp, Ki, and Kd gains, because inadequate values cause severe instability. If the proportional gain is too high, the motor begins to vibrate and oscillate violently around the target. Conversely, excessively low gains make response sluggish and inefficient against sudden external disturbances.

Another critical detail is integral saturation, which happens when the motor encounters an unmovable physical obstacle and the accumulated term grows indefinitely. To prevent the system from losing control upon release, we apply maximum and minimum limits known as anti-windup in code. Monitoring these variables in real time ensures that the electrical drive operates with safety, durability, and flawless precision in any industrial or robotic application.

Final considerations

Precise control of brushless DC motors unites power electronics, control theory, and high-performance embedded programming. Mastering PWM modulation, MOSFET switching, and encoder reading opens doors to building robust and highly responsive mechatronic systems. With a well-tuned PID loop, your application gains the stability needed to face real market demands with total reliability.