Analog Output - Convert PWM to Voltage

About: ProvideYourOwn.com is a website devoted to people who want to make their own things and provide for their own needs. We provide information, encouragement and resources to empower people to become free from ...

Intro: 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.

Share

    Recommendations

    • Electronics Tips & Tricks Challenge

      Electronics Tips & Tricks Challenge
    • Plastics Contest

      Plastics Contest
    • Optics Contest

      Optics Contest

    24 Discussions

    0
    None
    Meek_The_Geek

    8 months ago

    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.

    0
    None
    Joachim Spange

    1 year ago

    Where do I buy your RC-board?

    Or have you soldered it yourself?

    0
    None
    afasias

    2 years ago

    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?

    0
    None
    SamG36AmritS

    Reply 3 years ago

    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.

    0
    None
    PunitJSamG36

    Reply 2 years ago

    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.

    0
    None
    benrosenbachSamG36

    Reply 2 years ago

    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?

    0
    None
    SamG36benrosenbach

    Reply 2 years ago

    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.

    0
    None
    KhaledH6

    2 years ago

    cool,
    Thanks for the article, very helpful

    0
    None
    KhaledH6

    2 years ago

    cool,
    Thanks for the article, very helpful

    0
    None
    PeterR101

    2 years ago

    can someone post some parameters if i want to do it 2nd order RLC low pass filter with extremely low output ripples

    0
    None
    JohnS579

    2 years ago

    I am trying to change a pulse signal from a pulse water metering device to a 4/20mA signal so I can convert that with the use of a 250 ohm resister to a 0 to 5 v dc voltage to use as a input to the little bit cloud bit to monitor water usage on my irrigation system

    0
    None
    clovisfbdraško

    Reply 3 years ago on Introduction

    In this case you would need to increase PWM frequency, meaning you will have to "get your hands dirty" and modify analogWrite() library or even create your own "softPWM". totally doable both ways

    0
    None
    AleixMbdraško

    Reply 3 years ago on Introduction

    Maybe with a coil in series with the resistor and a very small cap....

    Just speculating

    To improve this add a second filter to remove most of the output ripple followed by an OPAmp as a buffer or with a gain if you want to control a larger voltage. The OPAmp will let you draw more current than directly off a pin of a micro-controller.

    A simple microcontroller circuit would also be very easy to use... use the ADC to measure the analog voltage input, and adjust the output PWM duty cycle accordingly.

    0
    None
    Smartfellyalan000

    Reply 3 years ago on Introduction

    I believe a 555 timer would work for that. It's not as simple as this circuit. Here's an example of using it as a PWM light dimmer.

    https://www.instructables.com/id/PWM-Lamp-Dimmer-using-NE555-Timer/

    0
    None
    tomas.ussing

    4 years ago on Introduction

    Do remember that the 'RC-circuit" provided hereover will only provide you with a given voltaic output LEVEL, i.e. for driving a given high-impedance controller. If you want to provide real power to your application via your "analogue" output, you will have to consider the current drawn by your load, and take that into account when chosing resistor and capacitor -values.