Dusan Ponikvar EDN When you need an analog output from a microcontroller that does not have a digitaltoanalog converter (DAC), you can connect an external DAC chip. But for a cheaper solution, use a pulsewidth modulated (PWM) output and add a lowpass filter (LPF) to extract its average value, which equals the duty cycle of the PWM signal (Figure 1).
The RC filter removes the nonDC components; what remains is the average signal U_{OUT}. If the period T of the PWM signal equals 63 clocks, the signal U_{OUT} can have one of 64 discrete DC values (0 to 63, sixbit resolution).
The time constant t of the lowpass RC filter must be big enough to smooth the output signal U_{OUT}. The ripple, ∆U_{OUT}, should be less than one least significant bit (LSB). The worst case occurs with a duty cycle of 50% (Figure 2). If t is much bigger than the period T then the capacitor charging current IC and the change ∆U_{OUT} can be approximated as: and from it follows For a 6bit DAC, ∆U_{OUT} should be less than V_{CC}/64, requiring a filter of t = R_{C} ≥ 16×T. Some practical numbers: lowpower microprocessors often use a crystal oscillator of 32768 Hz, and this clock signal is used for the PWM block. With 6bit PWM, the period T is 64/32768 ≈ 2 ms, necessitating a time constant of 32 ms. One has to wait 5t (160 ms) for a 6bit converter to settle. Slow. This Design Idea explains how you can speed things up. PWM blocks in microcontrollers can usually generate more than one PWM signal. Consider summing outputs from two PWM based 3bit DACs (DACH and DACL), where the output of DACL is reduced to oneeighth amplitude before adding. The resulting signal acts as a 6bit DAC, with an important advantage over the simple version: the period T is only eight clock periods for the same resolution, and the required time constant t is 1/8 of the previous, speeding up settling time by factor of eight. Such an arrangement is easy to implement with resistors in the RC filter for two PWM signals (PWMH, PWML) (Figure 3).
The output signal U_{OUT} is given by: This technique has been implemented in a TI MSP430F5132 microcontroller (Listing 1): Listing 1. Code to initialize & write to a 6bit (3+3) PWMbased DAC. // configure PWM  32 kHz / 8 = 4 kHz :: 6 bit in two PWMs, done only once on powerup
A 7bit DAC (Figure 5) can be implemented using 1% resistors. This time, two PWM signals are used to make two threebit DACs, totaling six bits, and the MSB is simply set to 0 or 1 at P3.7 (Listing 2).
Listing 2. Code to initialize & write to a 7bit (3+3+1) PWMbased DAC. // configure PWM  32 kHz / 8 = 4 kHz :: 7 bit in two PWMs and one digital pin, done only once on powerup
The speed improvement here is even greater. A simple PWM DAC would have a period of 128 clock periods (128/32768 s1 = 3.9 ms), resulting in t of 32·T = 125 ms and a settling time of 5×125 ms = 625 ms. Figure 6 settles in 40 ms — 16× faster. A higherorder LPF would also help to reduce settling time. References:Materials on the topicPCB Pricing: https://jlcpcb.com/quote


