Resistor/Pulse Width Modulation DAC
By Edward Schlunder / K9spud LLC
A few microcontrollers provide a built-in digital to analog converter (DAC), but usually not of sufficient resolution for full hi-fi stereo audio. Higher end, and more expensive, microcontrollers sometimes provide an I2S interface typically used for connecting to an external DAC chip. The higher price of these microcontrollers, combined with the expense of an external DAC chip, pushes up component count and total cost.
Many low cost microcontrollers today include some kind of pulse width modulation (PWM) feature. While PWM is usually used for controlling the speed of a motor, dimming a light, or controlling a switching power supply, we've found another great use: digital audio! This article will show how to produce excellent quality audio using PWM, at very low cost.
PWM Audio Theory
Audio samples are converted to a train of ON and OFF pulses. The average ON period of each pulse is proportional to the amplitude of the audio sample being played at that point in time. The PWM frequency, i.e. the total pulse period of ON plus OFF time, must be greater than the sampling rate of the audio being played. The output will then consist of a train of pulses where the average output voltage over the period is a function of the amplitude & frequency of the audio being played. This output signal contains unwanted noise from the PWM frequency and its harmonics. This noise can either be ignored because it is outside of the human hearing range, or it can be minimized by use of a low pass filter (integration).
In 1981, IBM released the IBM PC. It had a single, cheap internal speaker for audio, but to keep costs low, there was no DAC. The hardware would allow for applying power to the speaker on or off, but nothing in between.
Programmers found they could program the PC's Programmable Interval Timer (PIT) chip to drive the PC Speaker with varying pulse widths. Unfortunately, the PIT chip was only clocked at 1.2MHz. If the PWM frequency was set to 22KHz, this gave only 54 different output levels (1,193.182KHz / 22KHz = 54). If a PWM frequency of 11KHz is used, 108 possible output levels can be achieved (1,193.182KHz / 11KHz = 108), but this puts the unwanted PWM pulse frequency well into the human ear's audible frequency range, producing an undesirable 11KHz squeal on top of the audio.
Standard 8-bit audio has 256 different output levels (2 ^ 8 = 256), so playing audio on the PC Speaker had to be scaled down to the lower resolution. This made the sound quality fairly poor, but still recognizable.
More information about programming the PC's internal speaker using the PIT chip can be found here: Programming the PC Speaker.
Also during this period, some folks wired together a pile of resistors to their PC's Parallel Port to produce an 8-bit Binary Weighted DAC. This type of DAC used a specially designed resistor voltage divider network to produce the analog output. The resulting audio using this hardware could in theory be significantly better than the PWM PC Speaker method, but in practice it was difficult to get exact resistor values to make this solution work very well. Plus, not all parallel ports behaved properly either (some have internal pull-up/down resistors, etc).
High Speed Microcontrollers
Today's microcontrollers provide significantly better hardware. For example, the PIC24HJ64GP206 from Microchip provides 8 PWM pins, clocked at up to 40MHz. If a PWM frequency of 78KHz is used, each PWM output would be capable of producing 512 different output levels (40,000KHz / 78KHz = 512), or in other words, 9 bits of resolution. 10-bit resolution could be achieved, but this would limit the playback frequency to 39KHz.
What would it take to get to 16-bit resolution? Unfortunately, using the PWM technique by itself would require a clock rate of over 2.89GHz to achieve 16-bits of resolution. 2.89GHz divided by a PWM frequency of 44.1KHz allows for 16-bit resolution (2890137.6KHz / 44.1KHz = 65536). Few devices other than highly advanced microprocessors from Intel or AMD can operate at these frequencies. Obviously, using PWM alone is not going to be a feasible solution with today's equipment.
Extending Resolution by Weighting Pins
Fortunately, there is another way to increase the resolution of our DAC without having to resort to extreme clock rates. If we combine the PWM DAC with the Weighted DAC concept, we can use two or more PWM pins together to create a hybrid DAC design. By weighting each PWM pin with an appropriate resistor value, we can create a resistor voltage divider network that allows each PWM pin control over a particular significant bit range of the input audio sample data.
Refer to the schematic diagram at the right and the calculation table to the left. In this example, PWM pin 1 is connected to a 1K resistor, while PWM pin 2 is connected to a 256K resistor. If the microcontroller is powered at 3.3V, you can see that PWM pin 0 provides far less voltage swing on the final output than PWM pin 1 (roughly +/- 12.8mV), thanks to the 256K resistor on it's output.
This is a much greater difference than the traditional Binary Weighted Resistor DAC. Because we are using PWM pins, not only can we turn each pin on or off (binary), but we can also use PWM to turn each pin on or off for very short periods of time. This is the hybrid Resistor/PWM DAC. PWM pin 1 can produce very large swings in the average output voltage (most significant bits), while PWM pin 0 can produce very small swings in the average output voltage (least significant bits).
PWM allows us to "chop" the average output voltages down into smaller pieces. By applying the correct duty cycles to both PWM pin 1 and PWM pin 0, it should be possible to produce 65536 different average output voltage levels (16-bit), all with only 8 bits worth of different output levels produced on each PWM pin by itself. By combining the two PWM pin outputs through the resistor voltage divider network, we have greatly eased the clock rate requirements of our pure PWM DAC to achieve a 16-bit hybrid DAC.