PWM generation and motor interfacing
PWM principles and hardware generation, frequency choice, and DC motor, H-bridge, servo and stepper interfacing, with timer-register, average-voltage and motor-speed calculations.
Drafted with Aria, reviewed by the AiCanCode.org team. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
A microcontroller pin can only be fully on or fully off, yet motors, heaters, LEDs and valves need variable power. Pulse width modulation (PWM) solves this efficiently: the switch is either saturated or open, so very little power is wasted, and the load's own inductance or thermal mass averages the pulses. Almost every DC motor drive, servo, BLDC controller and SMPS in mechatronics runs on PWM.
Key ideas
PWM signal. A rectangular wave of fixed period T = 1/f whose high time t_on is varied. The duty cycle D = t_on/T runs from 0 to 1 (0–100 %). The average output voltage is D × V_s.
Why it is efficient. In linear (analog) control a transistor drops the difference between supply and load voltage and dissipates it as heat. In PWM the transistor is either fully on (small voltage drop) or off (no current), so losses are mainly conduction and switching losses — typically a few per cent.
Hardware generation. A timer counts from 0 to a TOP value (period register: ARR on STM32, ICR1/OCR1A or 0xFF on AVR). A compare register (CCR/OCR) sets the switching point: in edge-aligned (fast) PWM the output is set at 0 and cleared at the compare match. The CPU only writes a new compare value; the waveform runs by itself.
- Edge-aligned: f_PWM = f_clk/(N × (TOP + 1)).
- Centre-aligned (phase-correct, up-down counting): half the frequency for the same TOP, with symmetrical pulses that motor drives prefer.
- Duty resolution is TOP + 1 steps — a higher PWM frequency leaves fewer steps.
Choosing the frequency. Above about 20 kHz the motor whine is inaudible and current ripple is small (the motor inductance smooths the current when the period is short compared with L/R). Higher frequency increases switching losses in the MOSFETs. Typical: DC motors 5–25 kHz, heaters a few Hz (or zero-cross switching with SSRs), RC servos 50 Hz.
Motor interfacing.
- Never drive a motor from a GPIO pin. Use a transistor or logic-level MOSFET for one direction, or an H-bridge (L298, DRV8871, discrete MOSFETs) for both directions and braking.
- H-bridge: four switches; turning on diagonal pairs reverses the voltage. Never turn on both switches in one leg (shoot-through) — drivers add dead time.
- Flyback (freewheeling) diodes or the MOSFET body diodes carry the inductive current when the switch turns off; without them the voltage spike destroys the transistor.
- Separate power and logic grounds properly, decouple the supply, and consider opto-isolation in noisy plants.
- DC motor model: back-EMF E = K_e ω, and V = I R_a + K_e ω in steady state, so speed rises nearly linearly with average voltage D × V_s.
- RC servo: 50 Hz frame, pulse width about 1 ms (one end) to 2 ms (other end), 1.5 ms centre; the exact range varies by servo.
- Stepper motor: not PWM-controlled in speed; the step-pulse rate sets the speed (a chopper driver uses PWM internally to limit coil current).
Connections. Timers (topic 4) generate the PWM; relays and drivers (next topic) handle larger loads; in PLCs, high-speed outputs or analog outputs feeding VFDs play the same role.
Formulas
D = t_on / T, T = 1 / f_PWM
- D = duty cycle (dimensionless or %); t_on = on-time (s); T = period (s); f_PWM = PWM frequency (Hz).
V_avg = D × V_s
- V_avg = average load voltage (V); V_s = supply voltage (V). For a resistive load, P = D × V_s²/R.
f_PWM = f_clk / (N × (TOP + 1)) (edge-aligned)
f_PWM = f_clk / (2 × N × TOP) (centre-aligned / phase-correct)
- f_clk = timer clock (Hz); N = prescaler; TOP = period register value.
OCR = D × (TOP + 1)
- Compare value for duty D (edge-aligned, output set at count 0 and cleared on match, as on STM32; on AVR fast PWM the high time is OCR + 1 counts, a 1-count difference).
V_avg = I_a R_a + K_e ω → ω = (V_avg − I_a R_a) / K_e
- I_a = armature current (A); R_a = armature resistance (Ω); K_e = back-EMF constant (V·s/rad); ω = speed (rad/s). N_rpm = 60ω/(2π).
N_rpm = 60 × f_step / steps_per_rev, steps_per_rev = 360° / step angle
Worked examples
Example 1 (standard). A 16 MHz MCU drives a 12 V DC motor through a MOSFET at 20 kHz, prescaler 1, edge-aligned. Find TOP and the compare value for 9 V average.
TOP = f_clk / (N × f_PWM) − 1 = 16 × 10⁶ / (1 × 20 × 10³) − 1 = 799.D = V_avg / V_s = 9 / 12 = 0.75.OCR = D × (TOP + 1) = 0.75 × 800 = 600.- Check: t_on = 600 / 16 MHz = 37.5 µs, T = 50 µs, D = 0.75 → V_avg = 9 V.
Answer: TOP = 799, OCR = 600 (duty resolution 800 steps).
Example 2 (GATE level). (a) A permanent-magnet DC motor (R_a = 2 Ω, K_e = 0.05 V·s/rad) is fed from 24 V through an H-bridge at 50 % duty and draws 1.5 A. Find its speed. (b) An RC servo on the same MCU uses a 16-bit timer with prescaler 8 for a 50 Hz frame; find TOP and the compare value for a 1.5 ms pulse. (c) A 1.8° stepper receives 400 full-step pulses per second; find its speed.
V_avg = 0.5 × 24 = 12 V.ω = (V_avg − I_a R_a) / K_e = (12 − 1.5 × 2) / 0.05 = 180 rad/s.N = 60 × 180 / (2π) = 1719 rpm.- Servo: tick = 8 / 16 MHz = 0.5 µs.
TOP = 16 × 10⁶ / (8 × 50) − 1 = 39 999. - Compare = 1.5 ms / 0.5 µs = 3000 (duty = 1.5/20 = 7.5 %).
- Stepper: steps/rev = 360 / 1.8 = 200;
N = 60 × 400 / 200 = 120 rpm.
Answers: ≈ 1719 rpm (180 rad/s); TOP = 39 999, compare = 3000; 120 rpm.
Common mistakes
- Using f_clk/f_PWM as TOP — the counter has TOP + 1 states, so subtract 1.
- Forgetting the prescaler, or that centre-aligned mode halves the frequency.
- Assuming motor speed is exactly proportional to duty — the I·R_a drop and load torque matter.
- Omitting flyback diodes, or switching both transistors of one H-bridge leg on together.
- Choosing a PWM frequency in the audible range, or so high that MOSFETs overheat from switching loss.
- Treating a servo signal like motor PWM — the servo reads pulse width (1–2 ms), not average voltage.
- Driving a MOSFET whose threshold is above the MCU's 3.3 V output (use a logic-level part or a gate driver).
For GATE ME
Expect numericals on duty cycle, on-time, average voltage and power, PWM frequency and timer register values, DC motor speed from average voltage and back-EMF, and stepper speed from step rate; plus MCQs on why PWM is efficient, H-bridge operation and freewheeling diodes. Practise moving between frequency, period and counts, and keep the TOP + 1 detail straight.
Quick check
- A 50 Hz PWM has 40 % duty. What is the on-time?
- What average voltage does a 24 V supply give at 25 % duty?
- Why is a diode placed across a motor or relay coil?
- 16 MHz clock, prescaler 8, TOP = 99, edge-aligned: what is f_PWM?
- A 0.9° stepper runs at 1200 steps/s. What is its speed in rpm?
Answers: 1. 8 ms 2. 6 V 3. To give the inductive current a path at turn-off and clamp the voltage spike 4. 20 kHz 5. 180 rpm
Interview questions
All Microcontrollers, PLC and Industrial Automation interview questionsTry answering each one aloud before you open it.
1.What is Pulse Width Modulation (PWM) and how is it used in motor control?Concept
Pulse Width Modulation (PWM) is a technique used to control the amount of power delivered to an electrical device by varying the width of the pulses in a pulse train. In motor control, PWM is used to adjust the speed of the motor by changing the duty cycle of the voltage applied to the motor. A higher duty cycle means more power is delivered, resulting in higher speed, while a lower duty cycle reduces the speed.
2.Explain how a microcontroller generates a PWM signal.Concept
A microcontroller generates a PWM signal using its internal timers. The timer is configured to count up to a certain value, and the PWM output is toggled at specific counts to create a pulse. By adjusting the count at which the output toggles, the duty cycle of the PWM signal can be varied. This allows for precise control over the power delivered to a connected device.
3.What are the advantages of using PWM for motor control?Concept
PWM offers several advantages for motor control, including high efficiency, as it reduces power loss in the form of heat. It provides precise control over motor speed and torque, and it allows for smooth acceleration and deceleration. Additionally, PWM can be easily implemented using microcontrollers, making it a cost-effective solution for motor control applications.
4.Why is PWM preferred over analog voltage control in motor interfacing?Application
PWM is preferred over analog voltage control because it is more efficient and generates less heat. Analog control involves varying the voltage, which can lead to significant power loss as heat, especially at lower speeds. PWM, on the other hand, switches the power on and off rapidly, minimizing energy loss and allowing for better thermal management. Additionally, PWM provides more precise control over motor speed.
5.What happens if the PWM frequency is too low when controlling a motor?Application
If the PWM frequency is too low, the motor may experience jerky motion or audible noise. This is because the motor windings have time to respond to the individual pulses, causing vibrations. A higher frequency ensures that the motor windings integrate the pulses into a smooth, continuous motion, reducing noise and improving performance.
6.How does a PLC differ from a microcontroller in terms of PWM generation?Concept
A PLC (Programmable Logic Controller) is typically used for industrial automation and may not have built-in PWM capabilities like a microcontroller. However, PLCs can control PWM signals through external modules or by using high-speed outputs. Microcontrollers, on the other hand, often have built-in PWM modules that allow for direct generation of PWM signals, making them more suitable for applications requiring precise motor control.
7.What are the key considerations when interfacing a motor with a microcontroller using PWM?Application
Key considerations include ensuring the microcontroller can handle the required PWM frequency and duty cycle for the motor. The microcontroller's output current and voltage levels must be compatible with the motor driver circuit. Additionally, proper isolation and protection circuits should be used to prevent damage to the microcontroller from back EMF generated by the motor.
8.What PWM duty cycle delivers 60% of the maximum power to a load?Numerical
For a resistive load the power while the switch is on is V²/R and zero while off, so average power is D × V²/R: a 60% duty cycle gives 60% of full power. The average voltage is likewise 0.6 × V_s. For a motor the relationship is not exactly linear, because the current depends on back-EMF and load torque, so in practice the duty is set by a speed or current control loop rather than a fixed ratio.
9.A motor needs a 1 kHz PWM frequency and the microcontroller's timer is clocked at 8 MHz. What period (TOP) value must be programmed?Numerical
One PWM period must last 8 MHz / 1 kHz = 8000 timer ticks. An up-counting timer that counts from 0 to TOP has TOP + 1 states, so TOP = 8000 − 1 = 7999 (this needs a 16-bit timer, or a prescaler with an 8-bit one). The duty cycle is then set by the compare register, e.g. 4000 for 50%.
10.What is the impact of increasing the PWM frequency on the efficiency of a motor control system?Application
Increasing the PWM frequency can improve the efficiency of a motor control system by reducing the ripple current in the motor windings, leading to smoother operation. However, higher frequencies can also increase switching losses in the power electronics, potentially reducing overall efficiency. Therefore, a balance must be struck between reducing ripple and minimizing switching losses.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?