Analog Output - Convert PWM to Voltage





Introduction: Analog Output - Convert PWM to Voltage

Arduino's and other microcontrollers provide analog to digital (ADC) conversion to convert an input voltage to a digital value. You might think that they also provide the converse which is digital to analog (DAC) conversion. This is not the case. Instead they provide pulse-width modulated (PWM) outputs (see second photo). The Arduino library provides this functionality with a function called analogWrite(). The name seems to imply DAC functionality, but it just controls the PWM output. For many applications, such as the case of motor control, PWM is sufficient. For other applications, such as creating a linear voltage or current driver, a real DAC is needed.

Creating a real DAC
Fortunately, it is easy to convert a PWM output to an analog voltage level, producing a true DAC. All that is needed is a simple low-pass filter made from a resistor and a ceramic capacitor. The simple RC low-pass filter shown in the third photo converts the PWM signal to a voltage proportional to the duty cycle. For the Arduino, an R value = 3.9K and a C value = 0.1uF works well for most applications.

For more details on this subject as well as calculating R & C values more suitable for your application, please consult this article.

Was this a useful electronics tip?  If so, please cast your vote for it in the Electronics Tips & Tricks Contest.
How to vote: The voting button can be found at the top of this instructable. You can also visit the contest page and vote for other instructables as well. Multiple votes can be cast in each contest. If you see a tip you like, please show your gratitude with a vote.



    • Epilog Challenge 9

      Epilog Challenge 9
    • Paper Contest 2018

      Paper Contest 2018
    • Science of Cooking

      Science of Cooking

    We have a be nice policy.
    Please be positive and constructive.




    Where a circuit like this starts getting really useful is when you are using 2-wire BLDC/brushless motors (example 2-wire brushless cooling fans and blowers, being used for cooling systems in 3D printers).

    In these brushless fans and motors, the two wires feed into a PCB built into the motor that controls how the motor is commutated (how the coils are energized in sequences to make the motor rotate). Advantage = quieter and longer lived motors and easy to power (you do not need to incorporate an ESC circuit, just power them up, and there are no brushes which can fail, generate dust, and be electrically noisy). Disadvantage: PWM mucks up how the commutator circuit in these brushless motors work (turning power ON & OFF affects the PCB/circuit).

    For brushless motors and fans, they are far better off when powered by continuous DC signals instead of PWM, and controlled by changing the voltage levels instead of using PWM signals.

    Where do I buy your RC-board?

    Or have you soldered it yourself?

    That's great but what's its latency? From the moment I set a pwm value on arduino, after how much time will the output of your circuit be stabilised? If I want to create sin waveform with the arduino and your RC circuit, what's the highest effective frequency that I could reach?

    why do i need the resistor

    The resistor is part of the low pass filter. If you understand how a voltage divider works, the low pass filter works in the same way with signal frequencies, except the second resistance changes (it's actually what's called the reactance of the capacitor). It acts like a big voltage divider for high frequencies, reducing them a bunch, and it acts like a small voltage divider for low frequencies, keeping them the same amplitude. It does this because the capacitors "resistance" (actually the reactance, this is a somewhat complicated subject for beginners I suggest you look it up) changes with frequency. It is a small resistance at high frequencies, and a large resistance at low frequencies. When the second resistor in a voltage divider is bigger than the first the output will approach the original input voltage. When the second resistor is smaller the output approaches 0V. That's whats happening with the capacitor. Without the resistor there, it wouldn't be a voltage divider, and it wouldn't filter things predictably! You can look at the math behind RC low pass filters to understand more about why.

    Awesomely explained. Do you teach?. I'm curious, because my college would have been so much nicer as a learning ground with a faculty like you.
    Putting things simply is an art mostly forgotten here, It seems.
    Anyways, Thanks.

    I just gotta say, this is a fantastic explanation. I've been trying to wrap my head around a lot of these concepts for a long time and this one sunk in right away thanks to the detail you gave!

    Can you reverse the filter (high pass) by putting the capacitor first and the resistor to ground? Also, does the value of the capacitor play into it's reactance?

    You can. Putting a capacitor first and a resistor to ground will make it a high pass filter! You can also exchange the capacitor for an inductor, which will act as a small resistance to low frequencies, but a high resistance to high frequencies. There are the same arrangements (R - L to ground, and L - R to ground) but draw them out and see configuration will be a high pass and which will be a low pass? Always remember that if the second resistance is small, the output voltage is small, and if the second resistance is large, the output voltage is large. Really, I mean that if the second resistance is larger than the first, but for a short hand version thats a good way to remember.

    The reactance of a capacitor is determined by the capacitor's value (and the frequency, to be clear)! As the capacitance increases, the reactance will decrease (note you have to keep frequency in mind, pretend it's the same, say 56kHz, or 1.3MHz, or any other value you want).

    Now, I sort of lied when I said it's just like a voltage divider with two resistors. The equations still apply, but the capacitor has "phase shift" across it, so the output of the filter will change phase (relative to the original input) as you look at different frequencies. The effect this has is that when the reactance of the capacitor is equal to the resistance, the output signal is nearly the same as the original. On the other hand, when a voltage divider has two equal resistances, the output is one-half of the input voltage. This is fairly important when considering these things. Most of these topics should be covered when you look more into reactance, as phase is paired directly with reactance.

    The phase output from the filter, by the way, will follow an arctan (inverse of tangent) curve:

    It starts, though, at 0 degrees, and drops to -90. In other words, it's a

    -arctan(x) curve. Usually this isn't mentioned but it is true, and has meaning. Here's the actual "transfer function" (attenuation and phase over frequency) of an RC filter showing what I mean. Omega (looks like a w) is the point where the reactance of the capacitor is equal to the resistance of the resistor. There are a whole lot of "key terms" that come with filters, for example the transfer function is usually labeled H or H( f ) to indicate it changes with frequency.

    Thanks for the article, very helpful

    Thanks for the article, very helpful