Topics / Electronics & Hardware Basics / Q11
What is a DAC, and how can you generate an analog voltage with PWM?
Short answer: A DAC (digital-to-analog converter) produces an output voltage proportional to a digital code: V_out = code / (2^N − 1) × V_ref. Many MCUs have 12-bit DACs for waveform generation, audio, biasing and setpoints. Without a DAC, a PWM output followed by a low-pass filter gives an average voltage V_out = duty × V_high; resolution comes from the PWM timer's counts and the ripple from the filter.
PWM-DAC numbers
- Resolution: a timer clocked at 84 MHz with a period of 4096 counts gives 12-bit resolution at 84 MHz / 4096 ≈ 20.5 kHz PWM frequency.
- Filter: an RC filter with f_c ≈ 200 Hz attenuates the 20 kHz ripple by about 40 dB (100×); a two-stage filter gives more.
- Trade-off: higher resolution means a lower PWM frequency (for a fixed timer clock), which needs more filtering and slows the response.
Generating waveforms: use a timer to trigger DAC updates and DMA to feed a sine table (e.g. 64 samples per period) for exact frequencies without CPU load.
Other DAC concepts: settling time, glitch energy at major code transitions, output buffer drive capability, and R-2R ladder or resistor-string architectures.