# PWM to DAC with operational amplifier

I was following this project/tutorial shared on the website of Arduino to convert PWM signal into DAC output.

From what I can see, it uses an operational amplifier with a low pass filter (22uF capacitor and 3.3 kiloohm resistor.) I ran a quick simulation using Tinkercad Circuits simulation which has also Arduino in it.

The issue is that with the 3.3 kiloohm resistor I can read only 1.94V at 50% duty cycle while if I change the value to 1 kiloohm I can read 2.5V. (The correct value since at 100% duty cycle it should be 5V.)

So, my questions are:

• What does the resistor do?
• What changes with a greater or smaller value?

Background: I need to output a voltage between 1V and 4.5V using an Arduino for sending a signal.

• @JRE, I changed 'KOhm' (kelvin-ohm) to 'kilohm' prior to you changing them to 'kiloohm'. See Guide for the Use of the International System of Units (SI) section 9.3.: Reference [6] points out that there are three cases in which the final vowel of an SI prefix is commonly omitted: megohm (not megaohm), kilohm (not kiloohm), and hectare (not hectoare). In all other cases in which the unit name begins with a vowel, both the final vowel of the prefix and the vowel of the unit name are retained and both are pronounced. Commented Jul 9, 2021 at 18:18
• Good reminder T Commented Jul 9, 2021 at 18:24
• @Transistor Where I am from "megohm" and "kilohm" are definitely not common (though hectare is). it is either "kiloohm"/"megaohm" or simply "kay" and "meg". at least in terms of what people use Commented Jul 9, 2021 at 18:44

Probably you didn't allow sufficient time for your filtered PWM output to settle and that accounts for the difference in results between 1k$$\\Omega\$$ and 3.3k$$\\Omega\$$.

Using the (default, I think) 490Hz PWM frequency and the values of 3.3k$$\\Omega\$$ and 22$$\\mu\$$F (time constant of $$\\tau\$$= RC = 73ms) you get a response like this:

The ripple with this simple RC filter is about 22mVp-p (0.44% of full scale or 0.9% of the output at 50%). As you can see it doesn't get from 0V to 2V all that quickly. A rule of thumb is that you'll get 99% of the change within 5 time constants (365ms in this case).

The larger the RC time constant, the longer it will take to settle, and the lower the ripple will be. So with your 1K resistor you'll be getting more than 3x the ripple (more than 70mV of p-p ripple)

So there is a straightforward trade-off between settling time (we would prefer faster) and ripple (we would prefer less ripple) by altering the time constant. A more complex circuit can give you faster settling and lower ripple. To a first order, and within reasonable limits, it's the product of resistance and capacitance that matters.

Note that 1K is also getting into the range where linearity will be noticeably affected because the AVR chip outputs are not exactly symmetrical and are non-zero resistance (though better than some other MCUs).

Some types of capacitors also have non-ideal characteristics that can affect the output voltage (due to leakage) and the ripple (due to ESR).

And, of course, a simple PWM like this provides an output that is ratiometric to the supply voltage and other pins sinking or sourcing large amounts of current may change the filtered PWM output voltage a bit.

The input bias current of the TLV2451 is less than 7nA so you might well choose to use a much higher value resistor and a small ceramic capacitor. For example, a 1$$\\mu\$$F X7R ceramic capacitor and a 100k$$\\Omega\$$ resistor would give similar (a bit slower/less ripple) results to your 22$$\\mu\$$F electrolytic capacitor and 3.3k$$\\Omega\$$ resistor.

The resistor-capacitor combination sets up a low-pass filter with a certain cut-off frequency. You want this frequency to be no more than your PWM frequency.

See the this Microchip app note "Using PWM to Generate an Analog Output - TB3250" for more details and design guidelines: