PWM - Duty Cycle, Resolution and Demodulation
In questa pagina 6
Pulse-width modulation (PWM) transmits the information of a signal, the modulating signal, using as carrier a square wave of fixed frequency and amplitude in which the duration of the pulses is proportional to the level of the modulating signal. The modulation often implies a strong power amplification: the pulses may be hundreds of volts while the comparator works at logic levels. It is a fundamental technique in switching converters (SMPS, switch-mode power supplies), in motor drives, in class D audio amplifiers (switching output driven by PWM) and in D/A converters. On Arduino the same idea gives the analog output obtained with analogWrite and the LED fading of Lab 8 - Analog Output and Sensors with ArduinoLaboratory 8 covers digital input modes, PWM analog output with analogWrite (LED fading), the 10-bit analogRead input, the map and constrain functions, trimmer and photoresistor exercises, the LM35 temperature sensor and two additional exercises (hysteresis and potentiometer-controlled data logger).Lab 8 - Analog Output and Sensors with Arduino →.
Principle
An analog PWM modulator compares with a comparator the modulating signal with a triangular carrier (amplitude , period ). The output drives the switches of a half-bridge, and the voltage is for and for . The fraction is the duty cycle. The mean value over the period is As varies between 0 and 1, varies proportionally between and : the information of has moved into the mean value, and the power associated with can be much larger than that of . With a signal at levels and (instead of ) . If varies slowly with respect to , still .
Other modulations (PFM, random, pulse density, delta) also vary the distance between pulses: they are less used.
Spectrum and demodulation
PWM is a non-linear transformation and distorts the signal strongly; but the spectrum has a characteristic form: the mean value (or the modulating signal) plus lines around the integer multiples of the carrier frequency. A low-pass filter that removes the components at the carrier frequency and multiples is therefore enough to recover the modulating signal (demodulation). It works the better, with less residual distortion, the larger the ratio between the carrier frequency and the maximum frequency of the modulating signal: this is why PWM frequencies are relatively high and simple filters suffice. The demodulated signal keeps a residual ripple at the PWM frequency and a phase shift due to the filter. In many systems (SMPS, audio) the demodulation is done by the system itself, which is low-pass.
Attenuation of a first-order filter. With pole at and a component at frequency the modulus is . Example: PWM at 100 kHz () with a filter of pole : , attenuation .
Ripple on an RC filter. For a wave between and (peak-to-peak ) with duty , period and time constant , the peak-to-peak ripple is (the capacitor voltage rises for with slope and the mean value is ). For , , , : (the exact calculation with exponentials gives ). With the mean value is (zero for ).
Digital PWM modulator
It is built with digital circuits: the carrier is a binary counter and the analog comparator is a binary comparator. The counter counts up to (modulation period) and the output is high while the count is below the value of the compare register. The compare register is : it has the meaning of duty cycle.
- Counts per period: (with prescaler: ).
- Duty-cycle resolution: the step of is ; the effective bits are . The maximum modulation frequency for bits is . It is hard to have high resolution and high modulation frequency together.
- Asymmetric modulation (counter that counts up and restarts from 0: pulses aligned to an edge, "leading/trailing edge") and symmetric (up-down: the pulse is centred in the period; needed for many converters). With up-down counting the counts per period are (resolution halves at equal frequency).
- Dead time: delay between the turn-off of one switch and the turn-on of the other, to avoid short circuits between the supplies.
Numerical examples
- Maximum frequency for 10 bits. Clock 10 MHz, duty on 10 bits: . At the counts are : at least 6 bits are needed to represent the duty cycle () and the effective resolution is bits. With theoretical duty (modulation between and ) the register is by truncation: . Ideal mean value , real : error .
- Clock 170 MHz, square wave at 100 kHz, resolution bits. counts with prescaler 1: . Period register ; for the duty register is .
- 16-bit timer, 170 MHz, carrier 20 kHz, at least 10 bits. With clock 170 MHz: ( bits). Increasing the counter clock frequency improves the time resolution, but : . Among the maximum is 4 (): , effective bits; for a duty of : .
- Clock 200 MHz, 16 bits, bits of resolution: ; for a carrier near 15 kHz: ( bits); : (or 6666). Leading edge modulation: the pin is set at the timer reset (start of the period) and reset at the match on the duty cycle; the match on the period coincides with the counter reset.
- PWM as a DAC: 16-bit data (0.16 unsigned) generated at 1 kHz, PWM between 0 and 5 V at 20 kHz with counter clock 16 MHz: , equivalent bits ; for the datum : (41.4%), mean voltage ; for a resolution of 10 bits: .
Resolution and limit cycles
With too high the resolution drops below 8 bits and limit cycles can appear in a closed-loop control. For typical resolutions of 12 bits the modulation frequency does not usually exceed 500 kHz. If the frequency is not fixed by the specifications it is best to choose it as high as possible (simpler demodulation) compatibly with the resolution.
Applications
Control of power converters (SMPS, inverters): the converter acts as a voltage buffer with large power gain and the passive part (inductor and capacitor) is the demodulator (second-order low-pass), while the microcontroller computes the duty cycle values with a PID. Class D audio amplifiers. D/A converters. Example: an LED driven in PWM above 100 Hz is not perceived by the human eye as flicker; with 16-bit PWM, 10 MHz clock, frequency 200 Hz and at least 10 bits: counts (15.6 bits): and match .
Common mistakes
- Computing the effective bits with the processor clock frequency instead of the counter clock (after the prescaler).
- Applying to a bipolar signal: for levels the mean is .
- Swapping the meaning of the period register and the compare/duty-cycle register.
- Demanding both high frequency and high resolution with a slow clock.
Versione ripasso
Key statements and formulas
- Principle: the information goes into the pulse width; duty cycle . Levels : ; levels and : .
- Demodulation: a low-pass filter removes the carrier harmonics, better when the carrier frequency is much larger than the signal bandwidth; residual ripple and phase shift remain. First-order attenuation .
- RC ripple (): ; mean .
- Digital modulator: counter plus comparator; (with prescaler ); compare register ; effective bits ; . Up-down counting halves the resolution; dead time avoids shoot-through.
- Limit cycles: below about 8 bits in closed loop; with 12 bits ; choose the frequency as high as the resolution allows.
Main steps of the examples
- PWM at 100 kHz with : attenuation . RC ripple , , , : .
- 10 MHz clock, 10 bits: . At : , bits, register , mean instead of .
- 170 MHz, 100 kHz: , gives .
- 16-bit timer, 170 MHz, 20 kHz: clock (), , gives .
- 200 MHz, 15 kHz: ; leading edge: set at reset, reset at the duty match.
- PWM as DAC, 16 MHz clock, 20 kHz: ( bits); datum gives , .
Typical mistakes
- Bits computed with the processor clock instead of the counter clock.
- used for a bipolar signal.
- Period and compare registers swapped.
- High frequency and high resolution demanded together with a slow clock.