# Can I adjust the volume of a DAC using PWM?

Here is a circuit that allegedly converts PWM into a precision voltage reference:

I am thinking about feeding that reference into the reference line on this MCP4921 DAC. My goal is to generate waveforms that use the entire range I need (0-2v) and be able to digitally adjust the volume or max output level without losing the 12-bit precision. An option I hope to avoid is using 1 or 2 digipots on the output. For example, I could generate a 1v sinewave (instead of the maximum 2v), but the wave would not have as high a resolution as if I generated a full sine wave, but with respect to a 1v reference instead of 2.

I'm also hoping this circuit will help me avoid using 1 DAC as the reference to a 2nd DAC.

Question #1:

Is it even acceptable to vary the reference on the DAC, or is it meant to be constant? From what I can see, it seems doable.

Question #2:

Am I totally off track? Is there a better way to solve this problem?

Question #3:

Assuming that this is a possible solution and I'm not totally off track, how would you go about converting the +-2495mV range to a 0-2495mV single supply range? I'd like to be able to adjust this reference as high-res as possible within that range using the PWM from the uC.

• The LM431 has a reference voltage of 2495mV. This is going to throw a wrench in any attempt to get it down to 2V. – Ignacio Vazquez-Abrams Mar 9 '15 at 3:41
• Ah good catch. Is is possible to at least get it between 0-2495mV? I will probably still have enough resolution and will just ignore the extra. – scubadude22 Mar 9 '15 at 3:43
• This seems absurdly Heath Robinson; why not just build a proper "one-bit DAC" (giving ~16 bits effective resolution)? The slightest bit of ripple on the reference voltage will swamp the lower bits of the DAC's output resolution. – pjc50 Mar 9 '15 at 14:12
• @pjc50 Do you mean use a 1-bit DAC to supply the reference to the other DAC? or just the 1-bit DAC only? I'm just worried that I won't have a decent waveform at 100mV peak-to-peak. Maybe I should just try it. – scubadude22 Mar 9 '15 at 15:46
• You need to define a noise budget and usable dynamic range through the entire system in order to say what "decent" is. 12 bits implies roughly 70dB. What are you driving and what are its noise characteristics? Also, why not use a 16-bit or 24-bit DAC and do volume adjustment in the digital domain? – pjc50 Mar 9 '15 at 15:53

I think you've missed some of the limitations of the circuit. In effect, it takes a 5-volt PWM signal, low-pass filters it, then subtracts 2.495 volts to get the output voltage. Since the PWM signal can have a DC component in the range 0 to 5 volts, the output range is -2.5 to 2.5 volts. Its advantage over a simple RC filter on the PWM output is that a) it allows bipolar output, and b) with a 2-pole response it allows a better tradeoff of PWM switching frequency rejection vs. response time for changes in PWM duty cycle. Oh yes, and it's also cheap.

Note, though, that the accuracy of the output is only as good as the accuracy of both the 5-volt and 0-volt levels of the PWM signal. If you want the output to be accurate to 1 mV, the PWM high and low levels must both be accurate to within 1 mV.

There's no free lunch here. For what you're doing, I'd guess you'd be better off using the LM431 as a simple 2.495 reference, then use an analog switch driven by the PWM followed by a multi-pole filter. The more poles in the filter the sharper the cutoff frequency and the faster the response to duty cycle changes for a given level of switching frequency ripple. More poles means more complexity in the filter, but that's the price you pay. The filter could also incorporate the 0.8 gain you want to produce a 0 to 2 volt swing.

If your goal is to be able to have one digital signal source generate a PWM which would represent a fixed amplitude, and then scale that signal by some amount, there are a variety of techniques which can be used. If the signal sources have a nice integer frequency ratio like 3:2 (it's imperative that neither be a multiple of the other), and their least common multiple is significantly more than twice the roll-off frequency of one's output filter, one can multiply using a simple XOR or XNOR gate: simulate this circuit – Schematic created using CircuitLab

It's important to note that the output of the XOR/XNOR gate may include, as unwanted frequency content, every possible sum and difference of every possible multiple of the two PWM frequencies. If the frequencies are multiples of a common frequency, but are not multiples of each other, then the unwanted frequency content will consist of multiples of that common frequency. Thus, to make this technique work, one must use PWM rates that are much faster than would otherwise be necessary. If synthesizing such PWM rates isn't a problem, however, this approach has the advantage of extreme simplicity.

An alternative approach is to use a PWM signal, an op amp, and some pass gates to switch between an inverted and non-inverted version of a reference signal. When using that approach, it's imperative that both the input and output of the modulator be well-filtered, since any content remotely near the PWM frequency which is present at the input will generate aliasing at the output. Still, in cases where one only wants to modulate by a small number of discrete values (e.g. a 16-level volume control) it may be easy to use a PWM frequency that's high enough for that not to be a problem.