Tech
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
Homepage About Us Contact Us Store<br />
Food Energy Health <strong>Tech</strong> Living More…<br />
<strong>Tech</strong><br />
Arduino’s AnalogWrite – Converting PWM to a<br />
Voltage<br />
By SCOTT DANIELS | Published: JUNE 15, 2011<br />
DA_converter<br />
When I first started working with the<br />
Arduino platform (it was also my first<br />
experience with microcontrollers), I was<br />
a little surprised that analogWrite didn’t<br />
actually output a voltage, but a PWM<br />
(pulsewidth modulated) signal. After all,<br />
the ATmega had a AD (analog to<br />
digital) converter along with Arduino’s<br />
analogRead. The complementary<br />
analogWrite function was there, but no<br />
DA (digital to analog) converter on the<br />
AVR chip itself. Fortunately, there is an<br />
easy way to convert a PWM signal to an<br />
analog voltage. To do so you only need<br />
to implement a simple singlepole low<br />
pass filter. Does it sound complicated? It<br />
isn’t. There are some great online tools<br />
to help. Once you learn how to make one, you can quickly and easily output analog<br />
voltages from not only the Arduino, but PICs as well as any other microcontroller<br />
that has PWM output.<br />
PWM Primer<br />
Pulse width modulation (or PWM as it is most commonly known), is a way of<br />
encoding a voltage onto a fixed frequency carrier wave. Commonly used for radio<br />
controlled devices, it is similar to FM (frequency modulation) or AM (amplitude<br />
modulation) in what it accomplishes. Each type of modulation scheme has its own<br />
advantages and disadvantages. AM modulation was the first type of modulation<br />
used for radio transmissions. It is the most simple modulation scheme to<br />
implement, requiring only a single transistor or vacuum tube amplifier as was done<br />
in the early days of radio. However, it suffers from excessive noise and therefore,<br />
FM modulation was invented. In this modulation technique, the voltage signal is no<br />
longer related to the strength of the signal. That is why FM radio has superior noise<br />
and fidelity qualities over AM radio, though it is not as simple to implement in<br />
circuitry.<br />
With the need for digital communication, a new modulation technique was<br />
invented – PWM. This technique shares the same noise immunity as FM, to which<br />
it is very similar. The biggest difference is the simplicity and digital nature of the<br />
modulation. Instead of varying the modulation frequency with voltage, an output is<br />
merely switched on and off at a fixed frequency. The percentage of the ontime is<br />
in proportion to the signal voltage. To see better what this means, let’s examine<br />
what a PWM signal looks like for various levels. In the following image, the duty<br />
cycle is the output value from the PWM pin of an Arduino divided by 255:<br />
Follow me...<br />
Visit Our Store:<br />
Search for Products:<br />
Categories:<br />
Energy<br />
Food<br />
Health<br />
LEDs & Lighting<br />
Supplies<br />
<strong>Tech</strong><br />
Tools<br />
Latest Tweets<br />
New article Can LED Lighting<br />
Cause Blindness? Get the real<br />
scoop here <br />
http://t.co/wS4zmEd82p<br />
09:26:19 PM May 23, 2013<br />
Search<br />
New article No Your Dishwasher<br />
is Not Broken (or how to get your<br />
clothes/dishes clean again) <br />
http://t.co/WDaQIetnQd<br />
09:08:44 AM April 25, 2013<br />
How to add temperature control to<br />
an electric stove <br />
http://t.co/b5LQnEOfGf<br />
09:06:35 AM April 25, 2013<br />
Interesting new type of silicone<br />
rubber http://t.co/HWV7sWBl2r<br />
03:06:26 PM March 05, 2013<br />
Great article if you want to<br />
understanding modern banking <br />
http://t.co/iBqbY2uO<br />
03:54:10 PM February 02, 2013<br />
Follow @provideyourown<br />
24 followers<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 1/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
Recommended<br />
News & Views<br />
A Maker’s Microwave<br />
Get Accredited Degree Online<br />
Nanotechnology at Work – A<br />
Wonder Waterproof Coating Now<br />
Available<br />
List<br />
Why You Should Homeschool Your<br />
Child<br />
DIY Desklamp – from a Pizza Box!<br />
PWM outputs (curtesy arduino.cc)<br />
For the Arduino, you write a value from 0 to 255 on a PWM pin, and the Arduino<br />
library will cause the pin to output a PWM signal whose on time is in proportion to<br />
the value written.<br />
When it comes time for us to actually write an output voltage, the 0255 value<br />
lacks meaning. What we want is many cases is a voltage. For our purposes, we will<br />
assume the Arduino is running at Vcc = 5 volts. In that case, a value of 255 will<br />
also be 5 volts. We can then easily convert the desired voltage to the digital value<br />
needed using simple division. We first divide the voltage we want by the 5 volts<br />
maximum. That gives us the percentage of our PWM signal. We then multiply this<br />
percentage by 255 to give us our pin value. Here is the formula:<br />
Pin Value (0255) = 255 * (AnalogVolts / 5);<br />
Articles<br />
List<br />
Using ATtiny Chips for ArduinoLike<br />
Projects<br />
Making Hay<br />
Can LED Lighting Cause Blindess?<br />
No, Your Dishwasher is Not Broken<br />
Making a Color Television Set<br />
How to Get a Good Cheap 10 inch<br />
Android Tablet<br />
Statistics on the Arduino (also Pic<br />
or any microcontroller)<br />
Getting Started with Arduino<br />
Want to Die From Cancer? – Get<br />
Chemotherapy<br />
Recovering YouTube Channel<br />
Account<br />
Subscribe To Feeds<br />
All Articles<br />
All Comments<br />
All <strong>Tech</strong> Articles<br />
This Post's Comments<br />
Help! What are Feeds? <br />
Modulating a Signal<br />
In addition to just setting the output voltage, you may need to actually modulate a<br />
signal. To modulate a signal, we simply call analogWrite with the value<br />
corresponding to our signal voltage. One way to do this would be to read the<br />
voltage at an analog pin, and then write it back out. For example:<br />
int pwmPin = 9; // output pin supporting PWM<br />
int inPin = 3; // voltage connected to analog pin 3, e.g. a potentiometer<br />
int val = 0; // variable to store the read value<br />
float volt = 0; // variable to hold the voltage read<br />
void setup()<br />
{<br />
pinMode(pwmPin, OUTPUT); // sets the pin as output<br />
}<br />
void loop()<br />
{<br />
val = analogRead(inPin); // read the input pin<br />
volt =(5.0 * val) / 1023;<br />
val = 255 * (volt / 5);<br />
analogWrite(pwmPin, val);<br />
}<br />
Now in this example, we obviously won’t be need to convert our output voltage<br />
back to a voltage, but will instead transmit our modulated signal as it is. If you<br />
have an oscilloscope, you can attach it to the output, and a potentiometer to the<br />
input and watch your PWM signal change with the input value. There are many<br />
applications for PWM modulation, the most commonly being control of servos –<br />
either directly by wire or by radiocontrol. The Arduino has a nice library that<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 2/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
handles creating the correct PWM signal for servos. For more information, see the<br />
Arduino Servo Library.<br />
Changing the Modulation Frequency<br />
Most microprocessors permit you to change the modulation frequency for PWM<br />
pins. The Arduino has its own set default values. For pins 3,9,10,11 it is<br />
approximately 488 Hz. For pins 5 and 6, it is about 977 Hz. These values are for a<br />
stock Arduino running at 16MHz. You can change these frequencies easily by<br />
writing new values to the appropriate timer register. For example, to change the<br />
frequency of timer 2, which controls pins 9 and 10, to 3,906 Hz, you would set its<br />
register like so:<br />
TCCR1B = TCCR1B & 0b11111000 | 0x02;<br />
On the Arduino website, there is a nice tutorial on setting timer frequencies and<br />
their ramifications.<br />
Low Pass Filtering<br />
Now that you understand how PWM works and can even change the frequency, it is<br />
time to take a look at how to implement a simple low pass filter. This simple piece<br />
of circuitry will convert your PWM output into a voltage corresponding to the<br />
percentage of the PWM waveform. You will then have a complete DA converter for<br />
your Arduino or other microcontroller.<br />
If we examine the circuit on the left, when a voltage<br />
is applied to the input of R, the capacitor C will<br />
begin to charge. When it is charged, it will cease to<br />
conduct current and the voltage at the output of<br />
this circuit will match the input (assuming a high<br />
impedance load). If you remember that capacitors<br />
block DC currents, but pass AC currents, you can<br />
RC Low Pass Filter<br />
see that any DC voltage input will also be output,<br />
but high frequency AC voltages will be shorted to<br />
ground. For anything in between, i.e. lower frequency AC voltages, they will be<br />
filtered according to the R/C time constant formed by the resistorcapacitor<br />
network.<br />
While this circuit is very simple, choosing the appropriate values for R & C<br />
encompass some design decisions – namely, how much ripple can we tolerate and<br />
how fast does the filter need to respond? These two parameters are mutually<br />
exclusive. In most filters, we would like to have the perfect filter – one that passes<br />
all frequencies below the cutoff frequency, with no voltage ripple. While no such<br />
ideal filter exists, we can achieve close to it by using a multiple pole filter. Such a<br />
filter would incorporate many components in a ladder configuration. While such a<br />
filter has wonderful performance characteristics, its complexity and cost is<br />
unnecessary for simple DA conversion.<br />
In such cases, we only need a simple single pole filter as shown above. We can<br />
achieve a reasonable voltage ripple for a single price – a low cutoff frequency. A low<br />
cutoff frequency has two ramifications. First, it limits the speed by which we can<br />
vary our output voltage. Second, there is a response delay when changing the<br />
voltage until the steadystate voltage is reached. For many of the more common<br />
applications, this tradeoff is perfectly acceptable. Let’s now look at an example.<br />
First, let’s choose our maximum ripple voltage. When we filter this high frequency<br />
PWM signal, a small component of it will always make it through the filter. That<br />
happens because our capacitor is too small to filter it out entirely. We could choose<br />
a very large capacitor /resistor combination that would get a very high proportion<br />
of it, but then it would take a long time to reach the proper output voltage as the<br />
capacitor charges. That would greatly limit how fast our signal can change and be<br />
seen at the output. Therefore, we need to choose a reasonable value for the ripple<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 3/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
voltage. A popular application would be to change the voltage of a MOSFET. Since<br />
MOSFETs are voltage controlled devices, we can easily drive them with our<br />
microcontroller with PWM and a lowpass filter. Any ripple voltage present at the<br />
input would also be present at the output. For this example, assume the MOSFET<br />
will be driving a noncritical load such as a high power LED. In this instance, we<br />
merely need to stay within reasonable limits so the peak current in the LED will not<br />
be exceeded. In this case a 0.1 volt ripple would be more than adequate.<br />
Next we choose a capacitor value. While it would seem the next step would be<br />
choosing a cutoff frequency (and it normally would be), there are additional<br />
considerations such as output load and capacitor cost. If we were only driving the<br />
gate of a MOSFET, there would be no output load to speak of. In such case, we<br />
could choose a cheap ceramic cap such as 0.1uF and then choose the resistor we<br />
need to achieve the cutoff frequency desired. If, on the other hand, we need some<br />
current from our output, then we will need a smaller resistor and a correspondingly<br />
larger capacitor. For a recent circuit, I found I needed a 2.2uF capacitor to prevent<br />
my modest load from altering the output voltage too significantly. Designing this<br />
circuit for nontrivial loads is beyond the scope of this article. If you find yourself in<br />
such a need, the best approach would be to start with at least a 1uF capacitor and<br />
then test how your output voltage changes with load. Increase your capacitor until<br />
the load has a low enough effect to be acceptable. Another way to look at this<br />
circuit would be to think of it as a poorly regulated power supply. It only meant to<br />
convert digital signals to an output voltage; not to drive a load as well. Buffer the<br />
output with an opamp or a FET first. Then drive your load.<br />
For our example, let’s choose a capacitor value of 1.0uF. For driving a MOSFET, you<br />
can use something even smaller, but this size will let us have a small load. Next,<br />
we need to choose a cutoff frequency or response time. These two parameters are<br />
related but not the same. For simple things like driving LEDs, we are more<br />
concerned with a response time. Our response time can be pretty generous. Let’s<br />
choose a settling time (to reach 90% of the final value) of 0.1 seconds, which<br />
would require a resistor of 15K ohms.<br />
You may be wondering how to calculate these values, or others of your own. Rather<br />
than delve into a lot of equations, I have found something better. This excellent<br />
online calculator does all the hard math for you, calculating cutoff frequency,<br />
response times, voltage ripple and other values. It even draws a transient analysis<br />
graph for you – displaying your ripple and how the voltage ramps up over time.<br />
Here is the output graph for this example:<br />
RC Low Pass Filter Time Response (curtesy http://sim.okawadenshi.jp/en/PWMtool.php)<br />
Conclusion<br />
You are now armed with the knowledge you need for creating and using your own<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 4/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
digital to analog circuit. Such circuits are incredibly useful. My favorite is driving<br />
MOSFETs and opamps. By sampling a current or voltage somewhere, you can then<br />
determine what voltage you need to output to create the level of current or voltage<br />
you need. By means of such a simple system, you can make your own voltage<br />
regulators, current regulators, LED drivers, etc. The possibilities are endless.<br />
If you have any questions about this article, please drop me a note in the<br />
comments. If you have any improvements, corrections or additions, please let me<br />
hear about them as well.<br />
Post to Facebook<br />
57<br />
57<br />
Like 57 Send<br />
3 12<br />
57<br />
This entry was 57posted in <strong>Tech</strong> and tagged analogWrite, arduino, digital to analog<br />
converter, pic. 57Section: Article. Bookmark the permalink. Post a comment or leave a<br />
trackback: Trackback 57 URL.<br />
57<br />
57<br />
« Makers & Submarines 57<br />
Food Independence – First Steps »<br />
Post to<br />
Twitter<br />
Add to LinkedIn<br />
12<br />
2<br />
96 Comments<br />
12<br />
2<br />
2<br />
12<br />
2<br />
12<br />
John 12<br />
2<br />
Posted<br />
12<br />
2 June 16, 2011 at 2:09 pm | Permalink<br />
Simple, 12<br />
2<br />
clear, concise…an excellent tutorial. Thank you.<br />
12<br />
2<br />
Reply<br />
2<br />
12<br />
Post to Google+<br />
Add to Google Bookmarks<br />
Post Roger to StumbleUpon<br />
G Lewis<br />
Posted April 7, 2013 at 3:58 pm | Permalink<br />
Send via Shareaholic Mail<br />
A great Print Article with PrintFriendly<br />
very usefull indeed.<br />
Im on the Highway to Pedhell with ADC/DAC<br />
http://www.youtube.com/watch?v=Trb3UAXWNn8<br />
Reply<br />
Ian Crane<br />
Posted November 10, 2013 at 1:23 am | Permalink<br />
Scott, where do you get the little DA Conv PCBs?<br />
Reply<br />
High Energy Rob<br />
Posted June 16, 2011 at 6:08 pm | Permalink<br />
Nice article. Great job.<br />
Reply<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 5/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
wilcimar<br />
Posted June 16, 2011 at 7:53 pm | Permalink<br />
Hi, I´d like to know if I could use this circuit to convert a frequency to<br />
voltage signal from a ignition cable of my car ( by capacitive coupling), for a<br />
reliable RPM measuring. thanks.<br />
Reply<br />
High Energy Rob<br />
Posted June 16, 2011 at 8:29 pm | Permalink<br />
I think you can buy frequencytodigital converters that would probably work<br />
better for a tach. Im also fairly sure that I have seen guages that do this<br />
automatically and just require a frequency input. The problem with this<br />
circuit is that the signal from the coil on your car is not pulse width<br />
modulated, the pulses are always the same length so it wont really work.<br />
You could pretty easily turn an arduino into a tach using this circuit on the<br />
output though.<br />
Reply<br />
Mike<br />
Posted June 21, 2011 at 4:10 pm | Permalink<br />
What is the point to this? Most motors handle the PWM just fine. And for<br />
applications that need an analog voltage, well…this is only capable of<br />
sourcing a small amount of current.<br />
Reply<br />
Scott Daniels<br />
Posted June 21, 2011 at 7:49 pm | Permalink<br />
That’s a good question. For some applications such as the ones you<br />
describe, PWM may be just fine. For others, PWM is not acceptable. In<br />
these cases you need a lowripple voltage. When you buffer this<br />
voltage with a highimpedance amplifier such as a FET or an opamp,<br />
you can drive any load you like. This application is intended to drive<br />
such high impedance loads only.<br />
One example for this type of circuit is driving high intensity LEDs. For<br />
these devices, you can’t just apply a full voltage to them without some<br />
sort of current regulation, no matter how short the pulse is. They will<br />
burn up. You must create some kind of constant current driver. One<br />
way is to use the circuit described and apply this voltage to the gate of<br />
the FET. By monitoring the current going through the FET, you can set<br />
it to whatever value you like by varying the pulse width.<br />
Another example would be creating a programmable linear voltage or<br />
current regulator, again by using a single FET. You can even create a<br />
constant power regulator using this technique.<br />
Do these examples make more sense of this article now?<br />
Reply<br />
Hello..<br />
Eddie<br />
Posted May 3, 2013 at 1:03 pm | Permalink<br />
by modulating a signal,<br />
int pwmPin = 9; // output pin supporting PWM<br />
int inPin = 3; // voltage connected to analog pin 3, e.g. a<br />
potentiometer<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 6/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
if i use accelorometer.. how about the coding? i dont use<br />
potentiometer.<br />
Thanks<br />
Reply<br />
Navid<br />
Posted July 1, 2011 at 10:52 pm | Permalink<br />
Thanks alot – your answer solved all my problems after several days<br />
struggling<br />
Reply<br />
Art k<br />
Posted November 3, 2011 at 12:08 am | Permalink<br />
Great article. Do you mind a question.<br />
I am using the arduino to take two input signals to output a 05 volt analog<br />
control signal. No real load. Respons time is probably the most critical. What<br />
cap and resistor do you recommend?<br />
Thanks again for a great lesson!!!!<br />
Reply<br />
Scott Daniels<br />
Posted December 6, 2011 at 4:29 pm | Permalink<br />
That all depends on what timing you need. Go to the RC calculator<br />
referred to determine them.<br />
Reply<br />
Magnus<br />
Posted November 11, 2011 at 3:25 pm | Permalink<br />
Could you use this to convert an audio signal to pwn to control a laser which<br />
will send the impluse to a photo diode?<br />
great tutorial<br />
Reply<br />
Scott Daniels<br />
Posted December 6, 2011 at 4:31 pm | Permalink<br />
Yes, although you probably just want to send the PWM directly to the<br />
laser. Usually lasers are pulsed, not attenuated.<br />
Reply<br />
Tomas<br />
Posted August 29, 2014 at 4:28 am | Permalink<br />
NO! Lasers are delicate components that can not be driven by<br />
PWM. They will – as oposed to LEDs, transistors etc. – suffer<br />
from “COD” (Catastrophic Optical Damage) in nanoseconds; that<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 7/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
is – long before they die from thermal overheating in for<br />
instance a critical junction.<br />
Reply<br />
Hi,<br />
RodP<br />
Posted November 27, 2011 at 12:50 pm | Permalink<br />
I’m looking for a circuit that will interpret a signal that changes in pulse<br />
frequency and amplitude to drive a small peristaltic pump to deliver a very<br />
small amount of liquid. The pulses come from an lpg injector system, namely<br />
Prinns. I have a picture of a scope readout showing an example of the pulse<br />
but it doesn’t look like I can post it here. Please could you let me know if this<br />
might help me with what I’m looking for?<br />
Many thanks in advance<br />
RodP<br />
Reply<br />
Scott Daniels<br />
Posted December 6, 2011 at 4:34 pm | Permalink<br />
It sounds like you will need to read your input via an analog pin, do<br />
you signal processing on it via code, and then output straight PWM. You<br />
won’t need the filter specified in this article at all. Pumps like other<br />
motors are usually controlled by being turned on and off, not by<br />
sending a reduced voltage.<br />
Reply<br />
JStoski<br />
Posted January 17, 2012 at 6:56 pm | Permalink<br />
Perfect. Knew I had to buffer the pulses and smooth them out in order to<br />
measure the current going through the FETs. Have to control the current<br />
_smoothly_, not just max value and off using PWM. Thanks for the link to the<br />
single pole filter calculator.<br />
Reply<br />
Hannah Herbert<br />
Posted January 27, 2012 at 10:09 am | Permalink<br />
Thank you for an excellent tutorial!<br />
Reply<br />
Hannah Herbert<br />
Posted January 27, 2012 at 10:14 am | Permalink<br />
Scott, do you think I could sum digital audio samples using an Arduino<br />
and output this value using PWM and the filter you have suggested. Do<br />
you think this would suffice to generate a percussion sound like a bass<br />
drum or a snare drum?<br />
Please let me know your thoughts or if you have any better<br />
suggestions.<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 8/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
Reply<br />
Scott Daniels<br />
Posted January 29, 2012 at 7:21 pm | Permalink<br />
If you are fast enough . You must be able to do your signal<br />
processing in time to output your value above the Nyquist rate<br />
which is usually considered to be 40kHz for high fidelity music.<br />
The top frequency for the Timer2 of an ATmega 168 series is<br />
62.5k according to the spec sheet, so as long as you can do your<br />
signal processing fast enough to keep up. That would be the<br />
trick. Of course you want to make your output filter to be just as<br />
fast as well. Lastly you’ll want a high impedance load such as an<br />
opamp or MOSFET in order to make a fast filter.<br />
I hope that answer makes sense and isn’t too technical. I would<br />
begin by designing a filter you think would be fast enough and<br />
try doing some tests – both with an oscilloscope and your ears.<br />
— Best wishes on your project.<br />
Reply<br />
Ajay<br />
Posted February 20, 2012 at 2:29 pm | Permalink<br />
Looking for circuit or suitable instrument which can convert vehicle tach/rpm<br />
impulses to analog voltage, i.e. output value of tach/rpm needs to get<br />
converted to analog voltage as our hardware takes input from 0 to 5v or<br />
even up to 20v.<br />
Reply<br />
Scott Daniels<br />
Posted February 20, 2012 at 4:10 pm | Permalink<br />
What you need is a frequency to voltage converter. Something like this<br />
one or similar. If you are using a micro already, you can also count the<br />
pulses and then output a voltage as my article describes.<br />
Reply<br />
Ajay<br />
Posted February 24, 2012 at 6:44 pm | Permalink<br />
Thanks for the info Scott…..is any ready made Micro board<br />
available for PWM to Analog converter?<br />
Reply<br />
Asaph<br />
Posted February 24, 2012 at 5:34 am | Permalink<br />
Thanks for the PWM insight.<br />
I would wish to use it for triggering mosfets in a variable frequency inverter.<br />
Could you shed some more light on the same. you could forward some<br />
similar work on the same.<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 9/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
I will appreciate our help<br />
Reply<br />
gusti<br />
Posted March 7, 2012 at 4:03 pm | Permalink<br />
Thanks for that great tutorial. Especially for the link to the japanese site.<br />
Reply<br />
Scott<br />
Hello Scott,<br />
Posted March 25, 2012 at 10:32 am | Permalink<br />
I have 8bit digital signal coming in from photodiode (the song that is being<br />
transmitted from LED and captured by photodiode). Now I want to convert<br />
that 8bit signal back to analog and recover the song. Would your circuit<br />
work for it? Or should I just use the PWM signal as an analog input to the<br />
muencoder?<br />
Reply<br />
Scott Daniels<br />
Posted March 28, 2012 at 6:54 pm | Permalink<br />
This circuit will work, provided your digital signal is using PWM<br />
modulation. Also, make sure response time of the filter is fast enough.<br />
For audio, 24K sampling is fine for moderate quality (think cassette<br />
tape) audio. For high fidelity, sample at 40K. Also, note for the 8bit<br />
output, your dynamic range will be about 48 decibels. Give it a try and<br />
see how you like it (be sure to buffer the filter’s output with an opamp).<br />
Reply<br />
vipul<br />
Posted April 11, 2012 at 3:16 pm | Permalink<br />
Hi Scott,<br />
I am using the arduino and xbee radios with current transducers to monitor<br />
realtime power.<br />
The voltage output of the current sensor is wirelessly transmitted as adc<br />
value((01024) to a xbee receiver which is connected to arduino uno board.<br />
This board basically maps the adc value received via xbee(01024) into a 0<br />
255 range pwm signal. But this mapping causes loss in resolution, which<br />
affects the system, because i am not actually generating a steady voltage<br />
equal to the one i read using the current sensor.<br />
Do you think a low pass filter circuit will solve this issue by generating<br />
accurate voltage from pwm signals coming out of arduino analoque write().<br />
Thanks in advance<br />
Reply<br />
varsha<br />
Posted April 24, 2012 at 9:40 am | Permalink<br />
Hi Scott,<br />
we are making project with the help of arduino kit ATmega8, so we need<br />
difference between 8051 micro controller and arduino.<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 10/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
Thank you!<br />
Reply<br />
Mark Murray<br />
Posted May 27, 2012 at 4:47 am | Permalink<br />
Nice article, i would like to drive a model train with PWM [0 to 100%] at<br />
about 4V up to 1/3 throttle then linearly run up to 12V. I have an arduino and<br />
was wondering how to use PWM and then control the output voltage.<br />
Reply<br />
Scott Daniels<br />
Posted May 29, 2012 at 3:01 pm | Permalink<br />
For driving motors, you can use PWM directly – you don’t have to filter<br />
it to a voltage. Basically, use the PWM output to turn a MOSFET on and<br />
off, switching the full voltage. The motor will coast during the off<br />
phase, and the result will be a reduction in torque and speed that is<br />
correlated to your PWM percentage.<br />
Reply<br />
Tobi Broger<br />
Posted August 2, 2012 at 3:32 am | Permalink<br />
Hi Scott.<br />
Thank you for this excellent tutorial; for me as a Arduino/Electronic beginner<br />
really helpful!<br />
Probably you can help me with some hints on this:<br />
I would like to drive a tunable lens<br />
(http://www.optotune.com/images/products/Optotune%20EL1030.pdf) with<br />
my Arduino ADK. For this purpose I need DC: 0…300 mA. The manufacturer<br />
of the lens proposes some LED Driver Modules. Would the following one<br />
(http://www.thorlabs.com/Thorcat/0000/LD1255RManual.pdf) work together<br />
with the Arduino PWM output? Does that work straight away or do I need the<br />
low pass filter your described? Do you know cheaper possibilities (this one is<br />
123 Euros) to do something like that?<br />
Thank you for your valuable advice!<br />
Tobi<br />
Reply<br />
Tobi Broger<br />
Posted August 2, 2012 at 4:49 am | Permalink<br />
Found a cheaper one and decided to just try it<br />
(http://www.ebay.com/itm/WavelengthElectronicsWLD3343Laser<br />
DiodeDriver22AFastAnalogModulation/271026157095?<br />
pt=LH_DefaultDomain_0&hash=item3f1a6af627#ht_554wt_1093)<br />
Reply<br />
sri<br />
Posted October 6, 2012 at 1:33 am | Permalink<br />
thanks for the article besides that let me ask you one thing, i don’t<br />
understand the use of potentiometer to vary the duty cycle .my point is that<br />
by varying the bit sequence in our program with specified delay we can<br />
change the duty cycle of our pwm just like any other pin isn’t it?<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 11/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
Reply<br />
Scott Daniels<br />
Posted October 24, 2012 at 1:55 pm | Permalink<br />
The use of a pot is just an example of how to change the duty cycle.<br />
You can change the duty cycle of any PWM capable pin using<br />
analogWrite getting the value for that function anywhere. You don’t<br />
have to read it from a pot.<br />
Reply<br />
repairs laptop<br />
Posted October 12, 2012 at 1:52 pm | Permalink<br />
Interesting site I’m very glad I stumbled here through google, Gonna have to<br />
add this one to the old bookmark list<br />
Reply<br />
sam<br />
Posted October 20, 2012 at 1:00 am | Permalink<br />
Nice article, is it possible to drive peltier element from the output of low<br />
pass filter<br />
Reply<br />
Scott Daniels<br />
Posted October 24, 2012 at 1:58 pm | Permalink<br />
Since you would need to have a driver of some sort to drive any high<br />
power device, I don’t see why you would want to change the voltage<br />
into the driver. Just using PWM would suffice. The main exception<br />
would be in making your own constant current source using a MOSFET.<br />
In that case, you drive the FET with the low pass filter.<br />
Reply<br />
sam<br />
Posted November 9, 2012 at 12:24 am | Permalink<br />
Hi Scott,<br />
Thanks for the reply ,am a beginner in electronics ,am using a<br />
opamp h bridge circuit to drive the peltier . Can i give PWM<br />
signal from a atmega32 to a driver circuit for peltier element?<br />
Reply<br />
Josh<br />
Posted October 24, 2012 at 9:21 pm | Permalink<br />
Hey Scott,<br />
I am trying to decide if the Arduino is right for me. I am 100% a beginner to<br />
this kind of stuff but I have a few things I would like to make that I can not<br />
just go out and buy, or so it seems. I have a 12v sensor that outputs a<br />
frequency. I want to take that frequency and convert it to a linear 05v<br />
output that will not be driving anything, its just for data accusation. I also<br />
have a pwm output that has a constant frequency and I would like to take the<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 12/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
duty cycle and convert that to a 05v output for the same use. My question is<br />
the Arduino the right thing to use? Maybe just a low pass filter and a<br />
frequency to voltage converter would be simpler, I’m just not sure if that will<br />
fit my needs of getting both signals to be 05v.<br />
Thanks for your time and your help.<br />
Reply<br />
Josh,<br />
Scott Daniels<br />
Posted November 8, 2012 at 12:17 am | Permalink<br />
I like your spirit regarding making the stuff you need and want to<br />
encourage you in that direction. You can do what you want without an<br />
Arduino or microcontroller. It really depends on what your needs are.<br />
As far as cost, an ATtiny chip costing $1 will do what you want for less<br />
than the freqvolt chip, but you will need to program it. If you actually<br />
use an Arduino in a permanent placement, that would of course be<br />
fairly expensive. As an alternative you can use the Arduino for<br />
prototyping (which is what it is best at), and then switch to just an<br />
ATmega328 chip or even program an ATtiny as I said.<br />
The only real disadvantage to using a microcontroller is the need to<br />
program it. Are you a beginner in electronics or microcontrollers? If<br />
you are new to electronics, then microcontrollers can actually be<br />
easier for you since they greatly simplify the electronics involved.<br />
They kind of convert the messy analog world into the tidy digital<br />
realm. If you are new to microcontrollers (and not electronics), then I<br />
recommend adding this category to your repertoire. It opens a new<br />
world of possibilities.<br />
In either case, I recommend getting your application running using the<br />
Arduino – it is beginner friendly and makes electronics beginner<br />
friendly as well. When you need to deploy your application, you can<br />
burn an ATmega or ATtiny chip using your Arduino and pop it into your<br />
final project for only a few dollars.<br />
Lastly, the other advantage the Arduino offers is intelligence. Your<br />
circuit does not need to be ‘dumb’, meaning it can only do simple<br />
things like an analog circuit can. You can program in extra features,<br />
complex decision making, etc. Smart electronics is the wave of the<br />
future.<br />
Reply<br />
Jason White<br />
Posted November 19, 2012 at 10:40 am | Permalink<br />
Hi Scott,<br />
Thank you for the article, I’m sure when I’m farther along in my education, I<br />
will understand it better. I am 2 to 3 month novice to electronics and<br />
Arduino’s, but I Love challenges, so I may have bitten off more than I can<br />
chew with my latest project. I guess it’s silly to go from lighting some RGB<br />
LEDs to trying to control a model train with a TV remote, but pardon the pun,<br />
that’s how I roll. I actually am doing quite well with it. I have managed to get<br />
all manner of speed and direction control with DPDT relays and a Darlington<br />
transistor, or two types of motor shield. The problem lies in all cases with<br />
the annoying hum generated by PWM. I am currently using Lady Ada’s motor<br />
shield and I have set e frequency to 64khz which I am told will make the<br />
Pwm in audible, but I still hear it. Do you think your low pass filter could<br />
help with this? Can It be placed after the motor shield, before the track?<br />
Testing has moved from an nscale running 12v dc at 300mA to a big Gscale<br />
pulling .5.75A at 16V. If this would help with the hum, could you lead me in<br />
some direction as to the values of resistor and Capacitor I might need to<br />
make my train function more silently? I have considered going back to the<br />
Darlington hbridge and using a tip120 to provide speed control. Would a low<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 13/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
pass filter before the tip120 help with the PWM hum?<br />
Thank a million, Jason<br />
Reply<br />
Scott Daniels<br />
Posted December 6, 2012 at 10:42 am | Permalink<br />
Jason,<br />
I am not sure why you are hearing a hum. I suspect it is in the motor<br />
itself and not related to the actual PWM frequency. I would suggest<br />
trying a different frequency and see if the pitch changes.<br />
If you want to control the motor without PWM by filtering, you will<br />
need to use Nchannel MOSFETs to drive your motor. The low currents<br />
you mention should not be a problem with proper heat sinking. You<br />
would probably also need to monitor the current or voltage in order to<br />
determine what voltage to drive the FET with. The technical aspects of<br />
doing so is too much for this comment, but I plan a future article on<br />
the subject.<br />
You definitely cannot use a low pass filter either before or after<br />
Darlington transistors. They are for switching applications only.<br />
It is nice to hear about your willingness to tackle such an ambitious<br />
project. When facing a big learning curve it can sometimes be quite<br />
daunting, but with persistence the pieces eventually fall into place. My<br />
best to you.<br />
Reply<br />
Michael Hayman<br />
Posted December 3, 2012 at 6:59 pm | Permalink<br />
As an 87 years0ld dinosaur who started life with crystal sets, I have dabbled<br />
with electronics ever since, without ever really learning the theory; my most<br />
recent venture has been a poorman’s electrocardiograph (described on the<br />
above site). It seems to be time to update this effort, and I see the<br />
possibility of using one of the Burr Brown analog amps, and – I hope – a<br />
sampler employing an arduino device. I taught myself sufficient of several<br />
versions of Basic to be able to draw and printout the necessary waveforms,<br />
and am now battling with C and its variants. I am told that microcontrollers<br />
make electronics easier to grasp, but so far this blissful state eludes me. I<br />
am most happy to see your kindly and tolerant responses to us uneducated<br />
people, and just wonder if you could point me in the right direction with<br />
arduino? ( My field is Medicine, and my aim has been to make it possible for<br />
electrocardiography to be available to poor parts of the world).<br />
Reply<br />
Michael,<br />
Scott Daniels<br />
Posted December 6, 2012 at 10:50 am | Permalink<br />
I applaud your efforts with your noble project. The best I can do to<br />
help you get started with the Arduino is this article – Getting Started<br />
with Arduino. It gives a brief overview and links to some books and<br />
other resources.<br />
I do recommend the Arduino as the best microcontroller to start with.<br />
It has more resources, libraries and tutorials than any other platform.<br />
The best way to start is to buy an Arduino and start playing around<br />
with the sample “sketches” (programs) and go from there. The Arduino<br />
website has libraries for everything you will need to do. You just need<br />
to use them and modify the examples as needed.<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 14/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
Best wishes and keep up the outstanding effort.<br />
Reply<br />
John Ford<br />
Posted December 23, 2012 at 10:05 pm | Permalink<br />
Very informative.<br />
I need to replace a defunct HPLC gradient controller with absolute minimal<br />
cost (since I’m paying out of my pocket). Basically, I need to generate<br />
complementary sawtooth waveforms, one changing from 0 volts to 10 volts<br />
over 10120 minutes while the other changes from 10 to 0 volts over the<br />
same time interval.<br />
As I (mis?)understand this method, you effectively get about 8 bits of<br />
resolution on the conversion. Is that approximately correct?<br />
Thanks<br />
Reply<br />
Scott Daniels<br />
Posted December 26, 2012 at 6:16 pm | Permalink<br />
That is correct. On the ATmega328, Timer0 & Timer2 are 8bit, while<br />
Timer1 is 16bit. Therefore, it is possible to get 16bit resolution by<br />
using Timer1. The Arduino library does not support 16bit timer values,<br />
so you will need to write your own analogWrite equivalent in order to<br />
use all 16bits.<br />
Reply<br />
John Ford<br />
Posted January 15, 2013 at 4:48 pm | Permalink<br />
Thanks, Scott. I appreciate the advice. I’m thinking that an<br />
Arduino Due (with two 12bit DACs) would be an easier<br />
implementation for me. I’ve put my name on the waiting list at<br />
Adafruit.<br />
Reply<br />
GP<br />
Posted January 23, 2013 at 8:44 pm | Permalink<br />
Do you trust this method enough to use in lieu of a current limiting resistor<br />
for driving LEDs? The Arduino can source up to 40mA of current which is<br />
more than sufficient to burn out even most high mcd value LEDs; I am<br />
considering using this method to exactly deliver the maximum forward<br />
voltage to an LED based on its specifications. Good idea or high likelihood of<br />
magic smoke escaping the LEDs?<br />
Reply<br />
Scott Daniels<br />
Posted January 24, 2013 at 2:41 pm | Permalink<br />
You cannot reliably drive an LED using its nonimal forward voltage. If<br />
you look at an LED’s IV curve, the current takes off vertically at this<br />
bias point. Since the exact voltage various from device to device and<br />
over temperature, there is no way to do it. There are two alternatives:<br />
1) You can drive an LED with much less than its Vf by this method. If<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 15/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
the spec says the Vf=3.2 volts and you use 2.8 volts, it will probably<br />
work. However, what have you gained? You must use an added<br />
capacitor (not very cheap) and it is still an iffy approach.<br />
2) Use the method to make a constant current source. LEDs need to be<br />
driven by a controlled current source as opposed to a voltage source.<br />
To make a current source (technically a current sink), use an N<br />
channel FET as a voltage controlled resistor. Put a small resistor<br />
between the source and ground and measure the current by means of<br />
the voltage across that resistor. The LED goes between Vcc and the<br />
FET’s drain. Set your analog voltage to the FET’s gate to a nominal<br />
value (such as 1.0 volts) and measure your current flowing through the<br />
resistor. Increase your analog voltage driving the FET’s gate until you<br />
get the current you want. You need to test and adjust your current<br />
constantly. This method is a fair bit crude, but it will work. A better<br />
approach is a strictly analog one such as using a bipolar’s constant VBE<br />
to provide the needed feedback or else an comparator.<br />
Obviously, this approach is overkill for low current LEDs, but is a<br />
legitimate way to drive high current ones.<br />
Reply<br />
dan<br />
Posted January 24, 2013 at 6:44 pm | Permalink<br />
ok im using arduino mega 2560 to control the speed of a dc motor. i want to<br />
vary the speed of the dc motor according to a load from a load cell off a<br />
digital scale. i know i have to use the pwm, to control this. however im<br />
completely new to this and have no ideal where to go from here. i have the<br />
arduino, dc fan, power supply, and very little time to do this any help will be<br />
greatly appreciated<br />
Reply<br />
Scott Daniels<br />
Posted January 28, 2013 at 3:59 pm | Permalink<br />
It sounds like you just need to use the analogRead & analogWrite<br />
functions as well as processing the data. Read your data from your<br />
scale using analogRead(), and then power your motor with an N<br />
channel FET, giving the PWM to its gate with analogWrite(). For small<br />
FETs, you can drive their gates directly or with a small series resistor.<br />
For larger ones, you may have to use a driver circuit.<br />
Reply<br />
Hi Scott,<br />
Tom<br />
Posted February 9, 2013 at 9:52 am | Permalink<br />
First off, thanks very much for the great article!<br />
I came across this while looking for a way to convert an analogOut to 05v in<br />
order to control a mains dimmer<br />
(http://www.amazon.co.uk/dp/B001IROCTO/?tag=provideyourown120). I<br />
had enough of an exciting time soldering that whole assembly together! I’m<br />
quite a noob when it comes to electronics, so I’m a bit nervous about making<br />
a lowpass filter myself, for fear of getting lost in what exactly I need… I’ve<br />
poked around on Amazon and eBay but can’t seem to find any for sale, or it’s<br />
not clear whether or not they’d be suitable for my application…<br />
If you’ve got a spare moment, would you mind pointing me in the right<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 16/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
direction? I believe I simply need a 05v signal to control the power of the<br />
light through the dimmer, but of course I could be way off!<br />
Reply<br />
Scott Daniels<br />
Posted February 14, 2013 at 12:39 am | Permalink<br />
Just use the example given. Prototype it and measure the voltage<br />
output with a voltmeter. It should correspond to your PWM duty cycle.<br />
Provided the dimmer circuit you cited takes 0 to 5 volts, you should be<br />
good to go.<br />
Reply<br />
Hi Scott,<br />
Brian<br />
Posted February 9, 2013 at 11:46 pm | Permalink<br />
Wow, I love what your doing and how much your helping others. I often<br />
search for information for myself and very rarely share solutions. I’m glad<br />
someone out there does. Very inspiring.<br />
Keep up the good work.<br />
Reply<br />
andy<br />
Posted February 11, 2013 at 12:57 pm | Permalink<br />
this could be exactly what i’m looking for. i want to take a pwm signal from<br />
my computer’s motherboard and drive an analogue dial gauge. what<br />
components would i need for this?<br />
thanks in advance<br />
Reply<br />
Scott Daniels<br />
Posted February 14, 2013 at 12:42 am | Permalink<br />
If your dial gauge measures up to 5 volts, just the resistor and<br />
capacitor given should work fine. If your gauge requires a higher<br />
voltage, you will need to drive the gate/base of a transistor hooked up<br />
to a voltage source matching the gauge, and then filter that PWM<br />
signal as before.<br />
Reply<br />
Holger<br />
Posted February 18, 2013 at 10:00 am | Permalink<br />
Hi Scott, thanks for all these great thoughts, its fun reading through this<br />
thread!<br />
I have to drive a Photomultiplier (PMT) control circuitry with a 05V DC<br />
control voltage (CV) to generate the respective high voltage (HV) that<br />
regulates the PMT’s sensitivity.<br />
As the HV follows the CV according to the equation<br />
HV = CV*250<br />
any ripples are scaled up by a factor of 250 as well which could cause some<br />
additional noise on the PMT output due to sensitivity fluctuations.<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 17/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
As the Arduino’s PWM output is 5V (UNO in my case), I thought of a 2 pole<br />
active low pass filter with an OpAmp (e.g. a LM358 at 14Vcc) to get the full<br />
5V back after the low pass.<br />
Do you think that this will produce a DC stable enough or can you think of a<br />
better circuitry to be driven from a PWM pin for my purpose?<br />
Thanks<br />
Holger<br />
Reply<br />
Hi Holger,<br />
Scott Daniels<br />
Posted March 6, 2013 at 11:45 pm | Permalink<br />
You may have to do some testing to see what is acceptable. Multiplepole,<br />
lowripple filtering is a good bet. An alternative would be to use<br />
an R2R resistor ladder. It would take a lot of pins, but you would have<br />
no ripple.<br />
That’s the best I can come up with. Have fun.<br />
Scott<br />
Reply<br />
Enrique<br />
Posted March 17, 2013 at 10:30 pm | Permalink<br />
Hi everyone,<br />
I´ve been working in DAC to drive a directional proporcional valve (0 10V),<br />
but I´ve got de DAC (PWM to Analog) with a AOP LM741. will I need another<br />
step to drive the coil of the valve?<br />
Reply<br />
Scott Daniels<br />
Posted April 24, 2013 at 12:30 pm | Permalink<br />
An opamp should drive a small coil, but don’t forget to add the bypass<br />
diode to protect against the inductive transients.<br />
Reply<br />
Jason<br />
Posted April 21, 2013 at 4:05 pm | Permalink<br />
I have a gear reduction motor that has its own motor controller. It has one<br />
analog input controlling direction and speed. +5vdc thru 5vdc, Ovdc being<br />
the stopped position. +5vdc full speed counter clockwise, 5vdc full speed<br />
clockwise. Is there a way to make the pwm output from the arduino output<br />
+5vdc? Thanks<br />
Reply<br />
Jason,<br />
Scott Daniels<br />
Posted April 24, 2013 at 12:28 pm | Permalink<br />
You’ll need to use an opamp configured as a noninverting amp with a<br />
gain of 2 and supply it with 10 volts (assuming rail to rail opamp), to<br />
give you the required 10 volt peakpeak voltage swing. Then use<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 18/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
another opamp configured as a voltagefollower with a 5v input to<br />
create a ‘virtual ground circuit’ (google it for details). Use the output of<br />
this opamp for your ground and the first opamp for your signal.<br />
Reply<br />
Hello..<br />
Eddie<br />
Posted May 3, 2013 at 1:07 pm | Permalink<br />
by modulating a signal,<br />
int pwmPin = 9; // output pin supporting PWM<br />
int inPin = 3; // voltage connected to analog pin 3, e.g. a<br />
potentiometer<br />
if i use accelorometer.. how about the coding? i dont use<br />
potentiometer.<br />
i use http://fr.hobbytronics.co.uk/imu5dofadxl335idg500<br />
thanks<br />
Reply<br />
NAcho<br />
Posted April 20, 2014 at 5:52 pm | Permalink<br />
I’m sorry, I didn’t read all the comments. If my question<br />
has been asked before, please point me to the answer and<br />
I’ll be grateful.<br />
I want to convert: PWM to Analog 05Vdc.<br />
Is it possible? Easily.<br />
Thank you.<br />
Reply<br />
sab<br />
Posted May 9, 2013 at 5:19 am | Permalink<br />
Hi Scott,<br />
i am wandering on how to operate and to make just a simple program for my<br />
6A Hbridge motor controller, here it is,http://www.egizmo.com/KIT/hbd6.htm,<br />
and the manual is<br />
https://docs.google.com/file/d/0BxdLxDCD6HidMTFJVFdfeC1pQ3c/edit?pli=1, i<br />
really dont understand what does the PWM means but because of this tutorial<br />
i think i have an idea on how to this , advance thanks =)<br />
Reply<br />
Scott,<br />
Chris<br />
Posted June 13, 2013 at 12:58 pm | Permalink<br />
A very late comment but just getting into this stuff and came across your<br />
excellent article.<br />
I wonder if you can help me.<br />
I am driving a PC fan through an H bridge motor controller. I send a PWM<br />
signal from the arduino, and can vary the motor speed. Problem is that I get<br />
an annoying hum, which only occurs with PWM. If I use a lower voltage (non<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 19/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
PWM) on the fan I don’t have a problem.<br />
So I imagine that I should use a low pass filter to change PWM into<br />
something more like straight DC. I used the calculator to size R and C<br />
values, but find that there doesn’t seem to be enough voltage left after the<br />
resistor to drive the fan, is the fan barely runs (low speed).<br />
Perhaps I have misunderstood but I would assume that the resistor in the<br />
filter will cause a volt drop as current passes through it?<br />
I’m a complete beginner at this so any suggestion welcome.<br />
Chris<br />
Reply<br />
Chris,<br />
Scott Daniels<br />
Posted June 16, 2013 at 1:15 am | Permalink<br />
The problem you are seeing is the fact that the motor is putting a load<br />
on the filter. Such a filter needs to have a buffer between it and a<br />
load. Yes, the resistor is a huge voltage drop at all but the smallest<br />
currents, and the capacitor can store only the smallest amount of<br />
charge. Any kind of load at all with cause the filter to not function<br />
properly.<br />
What you need is an amplifier with high input impedance and low<br />
output impedance. Either an opamp or a MOSFET will serve this<br />
purpose nicely. Both have high input impedance (they won’t load the<br />
filter at all), and they both have low output impedance (they can drive<br />
a large load without bogging down).<br />
In the case of using a Nchannel FET, put your load between your<br />
supply and the FET. Your duty cycle will not be follow a linear<br />
relationship to motor speed however. If you want such a relationship,<br />
insert a small current sense resistor and feed the voltage across it<br />
back into your Arduino and scale your PWM to produce the current<br />
level desired.<br />
For a choice of FET, this one in a TO220 package will handle a couple<br />
of amps. If it gets too hot, just add a small heat sink.<br />
Reply<br />
Michael<br />
Posted June 29, 2013 at 11:41 am | Permalink<br />
hey scott<br />
i was hoping someone else would ask this with all the comments…<br />
but no one did so it may be a bad question, but could clear this up for me.<br />
in your schematic of the filter what connects to the other end of the<br />
capacitor? the schematic shows nodes with no further indication of what they<br />
are connected to. my assumption is common ground or nothing as dc cant<br />
pass through the capacitor.<br />
thanks. ill be messing around with this circut to fade in and out a large LED<br />
array with my arduino.<br />
Reply<br />
Scott Daniels<br />
Posted July 12, 2013 at 10:06 pm | Permalink<br />
Yes – the lower wire with its connections is the common ground for<br />
both input and output. I apologize that the drawing is not more clear on<br />
that.<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 20/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
Reply<br />
Greetings,<br />
Asterios Stamatis<br />
Posted July 4, 2013 at 12:29 pm | Permalink<br />
I would like your advice because i am a little bit confused. I have a project in<br />
which i have to dim a 10V lamp according to a digital value. I just ordered<br />
the arduino starter kit. I have an LPC2468 board but seems to be too<br />
compicated for that and the community is not that big.<br />
I started to read some things and it seems easy to dim an LED. But how can<br />
I produce voltage from an Arduino board? Is there an easy way? My project<br />
is supposed to read a value (luminance) from a file and dim the lights<br />
accordingly. In addition arduino produces PWM equal to 5 V. is there a way<br />
to amplify it to 10V?<br />
Its been a long time since the last time that i read about electronics, so plz<br />
make it as easy as you can..!!!<br />
Best Regards,<br />
Asterios Stamatis<br />
Reply<br />
LewTwo<br />
Posted August 5, 2013 at 10:09 pm | Permalink<br />
First: Thank you for the well written article.<br />
Second : There may be an excellent application for this technique …<br />
supplying and analog reference voltage to the Arduino’s “AREF” pin.<br />
This would allow the user to adjust the reference voltage through software to<br />
meet the needs of the signal that they were intending to read (temperature<br />
sensors come to mind). There is already a 32K Ohn resistor on the Ref Pin<br />
but that would be on the wrong side of the capacitor. Is there a convenient<br />
way to modify the calculation to take this into account?<br />
Reply<br />
Scott Daniels<br />
Posted August 15, 2013 at 12:34 am | Permalink<br />
Since the input impedance to the AREF pin is probably quite high, you<br />
don’t have to worry about the effects of an additional resistor. I am not<br />
sure this is the best way to go about what you want to do though.<br />
Using a precision 2.5v reference IC is what I would suggest. It would<br />
be very accurate and independent of supply voltage.<br />
Reply<br />
gargoor<br />
Posted August 6, 2013 at 3:46 pm | Permalink<br />
great article . Dear i have a question . now i got a mic+amp+pot i want to<br />
feed it into arduino and get an output digitally and send it via a TRx RFM22 .<br />
do i need a buffer for it ?<br />
second thing , what if i use my mic+amp+pot as input , then the output i<br />
want to hear it via speaker . should i still use a buffer ? ….<br />
Do i need to divide 1023 from ADC by 4 so it can be heard properly in the<br />
output ? because i know the ADC reads from 0 to 1023 and the PWM from 0<br />
to 255 . wouldnt there be loss in data ?<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 21/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
Reply<br />
Scott Daniels<br />
Posted August 15, 2013 at 12:43 am | Permalink<br />
On the transmitter, I would have to study the spec sheet more to be<br />
sure, but you should just give it a try – if it doesn’t work, add a buffer.<br />
For output to a speaker, you definitely need a buffer. On the 01023<br />
input, no you don’t need to divide, but rather scale both the input and<br />
output. For example, if you want to do internal calculations in terms of<br />
percent, then scale your input from 1023 to 100, and your output from<br />
100 to 255. The Arduino supplies a ‘map’ function for this very purpose.<br />
See – http://arduino.cc/en/Reference/map<br />
Reply<br />
Manikandan Selvam<br />
Posted August 15, 2013 at 8:32 am | Permalink<br />
I’m doing PWM to analog voltage conversion in my project. We are using<br />
second order RC low filter to convert from digital to converter.<br />
It would be helpful if you share the calculation to find out value of the step<br />
response and peak to peak ripple voltage and settling time.<br />
and how to make the relation between pwm input signal and rc filter in<br />
equation to get the output waveform.<br />
Reply<br />
tenCents<br />
Posted August 20, 2013 at 7:33 am | Permalink<br />
Adequate and well explained, got me up to speed quickly. Thanks for your<br />
effort. I’m just getting into this Arduino stuff and loving it. Salutations!<br />
Reply<br />
David Pan<br />
Posted October 11, 2013 at 8:29 pm | Permalink<br />
How can I type numbers from 0255 from seiral USB keyboard to control<br />
PWM and analog output?<br />
Reply<br />
Hey Scott<br />
Ferdous Ahmed<br />
Posted November 12, 2013 at 3:17 pm | Permalink<br />
I’m just trying to get two Analog output from Arduino. can you give some<br />
guidance to do so. I have a duo device. Thanks a lot<br />
Ferdous<br />
Reply<br />
SJPeng<br />
Posted December 24, 2013 at 1:18 am | Permalink<br />
Hi this article is fantastic!!<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 22/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
And one question I would like to understand. How to convert PWM > RC<br />
filter to + voltage.<br />
ex. Here I have a PWM channel where the voltage level is 5volt. so if I use<br />
your suggestion, only 5 volt is available for filtered ouput. So how can I get<br />
negative 5 volt by using the similar manner? Many thanks for your kindly<br />
help.<br />
SJPeng<br />
Reply<br />
banz<br />
hi Mr. Scott,<br />
Posted January 16, 2014 at 10:10 am | Permalink<br />
i wonder if our group is using the correct value for resistor and capacitor for<br />
the low pass filter. 10k ohm and 3.2 nF respectively for a 5khz cutoff<br />
frequency. this application is for our project wireless communication based<br />
laser with certain distance to cover.. we are using a PIC 877A for a PWM<br />
output, then we decided to low pass filter the PWM output to generate analog<br />
waveform.<br />
does it make sense sir?<br />
and one thing also i have read some forums online is that in order to acquire<br />
the right value for resistor we have to refer first on what input impedance<br />
we have in our audio amplifier, say if input impedance is 10k ohm then the<br />
resistor will also then be 10k ohm..BUT as far as what i have searched,<br />
typical input impedance say for LM386 is 50k ohm. seemed to be too large.<br />
what could be the right thing for this sir?.<br />
alternatively, we planed to use IC 555 timer to generate PWM and wire up<br />
the low pass filter for analog output. i wonder if what could be other<br />
consideration for this?. or say is this possible and applicable for our project?.<br />
i hope u can help us with our project. THANKS LOT and thanks for your info.<br />
GOD BLESS..<br />
Reply<br />
Aydin Gulgun<br />
Posted March 14, 2014 at 9:43 am | Permalink<br />
hi, i find your project when i was searching for an audio to pwm<br />
transformation.<br />
i m trying to drive some pomps to create an equalizer graphic with water.<br />
i understand that what i want is kind of opposite of what you did here but i m<br />
wondering if you can do this, can the oposite be done? what i mean is can we<br />
input an audio signal and convert it into a pwm signal? if yes can you help<br />
me to understand the prosses. and i m sorry for my bad english.<br />
Reply<br />
Allan<br />
Posted March 26, 2014 at 10:33 pm | Permalink<br />
Thank you Scott. I believe you have described what I am needing which is a<br />
method for an Arduino to mimic the controller for a submersible Speed Wave<br />
DC aquarium pump. I think these pumps must have their own PWM controls<br />
potted into the pump. The external controller provides a steady 24 volt<br />
source to the pump and a 0 to 4.9 volt control signal for on/off and 6 speed<br />
levels. I have assumed at this point this 0 to 4.9 control signal is not PWM<br />
from the controller to the pump. My questions are: One, why would I care<br />
about cutoff frequency? I think I need to pay attention to the ripple and how<br />
long it takes to go from one voltage to another. Second, the OKWA calculator<br />
looks at 0% to X% as duty step. How can I figure the time going from X% to<br />
Y%? I assume this is the ramp time going from one speed to the next for the<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 23/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
pump application. Thanks again.<br />
Reply<br />
Greg<br />
Posted April 18, 2014 at 6:22 pm | Permalink<br />
I’m sorry, I didn’t read all the comments. If my question has been asked<br />
before, please point me to the answer and I’ll be grateful.<br />
My question is: Would I be able to control sound volume (from an external<br />
source) through a transistor with this analog output? If not, do you have any<br />
suggestions? In my project space is verry limited so as few components as<br />
possible would be nice.<br />
Thank you!<br />
Reply<br />
NAcho<br />
Posted April 20, 2014 at 5:51 pm | Permalink<br />
I’m sorry, I didn’t read all the comments. If my question has been<br />
asked before, please point me to the answer and I’ll be grateful.<br />
I want to convert: PWM to Analog 05Vdc.<br />
Is it possible? Easily.<br />
Thank you.<br />
Reply<br />
Logan<br />
Posted June 7, 2014 at 11:24 am | Permalink<br />
I’m curious of a DAC like this would work for driving galvanometers, which<br />
don’t like raw PWM input (At all…), for a UNOprogrammable laser light<br />
show. Assuming it would, any idea what changes might need to be made?<br />
Sorry if this is a noob question, I’m kinda new to both Arduino’s and Galvos.<br />
Reply<br />
Alannah<br />
Posted June 13, 2014 at 7:31 pm | Permalink<br />
Pretty! This was an extremely wonderful article.<br />
Thanks for providing this info.<br />
Reply<br />
Vin<br />
Posted June 19, 2014 at 1:38 am | Permalink<br />
Hello Sir. I need an analog control signal ranging grom 5V to +5V. Can I use<br />
this approach to get 0 to 5V, and then use an amplifier with gain 2 to get 0 to<br />
10V, and clamp it negative by 5V to obtain 5V to +5V?<br />
Reply<br />
Bojan<br />
Posted July 30, 2014 at 12:08 pm | Permalink<br />
Can you make it quicker? I mean can you make time necessary for capacitor<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 24/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
to charge smaller?<br />
Reply<br />
Mike Eisen<br />
Posted October 21, 2014 at 2:30 pm | Permalink<br />
Help Please!<br />
I have PWM out put from a Radio Control Receiver that i need to connect to<br />
an RS485 input to control the zoom on a camera any pointers you have<br />
would be much appreciated as i have no idea how to do it.<br />
Reply<br />
amit singh<br />
Posted December 1, 2014 at 5:25 am | Permalink<br />
I am trying to build a quadcopter but i am facing problem in calibration of<br />
ESC. I have 4in 1 EMAX ESC. I am using Arduino UNO to calibrate the ESC.<br />
My code is:<br />
#include<br />
Servo esc;<br />
int potpin = 0;// Pot pin<br />
int val=0;<br />
void setup()<br />
{<br />
esc.attach(9);// ESC attach to pin no 9<br />
}<br />
void loop()<br />
{<br />
val = analogRead(potpin);<br />
val = map(val, 0, 1023, 0, 179);<br />
esc.write(val);<br />
}<br />
by using this code i have calibrated 2 ESCs (esc1 and esc2) out of 4 and they<br />
are working fine. but other 2 ESCs(esc 3 and esc4) are not calibrating<br />
properly although the ESC is giving the calibration confirmation tone but<br />
motors are not rotating.<br />
my procedure is<br />
1 throttle the pot at full.<br />
2 connect the li po and turn on the esc<br />
3 after exact beep sending the minimum signal to ESC (by throttling pot to<br />
lowest position).<br />
4 certain beeps come and finally calibration confirmation beep comes and<br />
ESC has calibrated. but motors are not rotating<br />
Plz help me .<br />
Reply<br />
Dennis<br />
Posted December 3, 2014 at 5:23 pm | Permalink<br />
Trying to eliminate the noise from a Micro when Pulse With Modulating<br />
“PWM” the Analog out with LED’s attached! The LED’s are mounted inside of<br />
an electric guitar and the pickups of the guitar are amplifying the sound of<br />
the Arduino Micro. I’ve tried to change the Timers 0,1,&2 frequency but<br />
unfortunately I was getting an error when trying to change the frequency of<br />
Timer #2. Are there only 2 timers on the Micro being #0 & 1?<br />
Reply<br />
Hubert Tchio<br />
Posted January 13, 2015 at 3:59 am | Permalink<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 25/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
what is the easiest way to control the PWM pulse width?<br />
Hi Scott, thanks for all these great thoughts. I am a beginner in Electronics. I<br />
have designed a switch mode power supper made up of a MOSFET to driver a<br />
high current LED (1.2A). My switching frequency is about 1.5Mhz. I have also<br />
built an analogue PWM consisting of op amps for switching the Mosfet. What<br />
is the easiest way to control the PWM pulse width? is it possible to use a<br />
common controller (P, PI, PID) to change the pulse width? if yes how?<br />
I will be grateful to get some hints.<br />
Reply<br />
Hello..<br />
Eddie<br />
Posted May 3, 2013 at 1:05 pm | Permalink<br />
by modulating a signal,<br />
int pwmPin = 9; // output pin supporting PWM<br />
int inPin = 3; // voltage connected to analog pin 3, e.g. a potentiometer<br />
if i use accelorometer.. how about the coding? i dont use potentiometer.<br />
thanks<br />
Reply<br />
Scott Daniels<br />
Posted June 16, 2013 at 1:19 am | Permalink<br />
You’ll have to figure out how to interpret your accelerometer data by<br />
studying the specs/tutorials you already have. Modulating the signal is simply<br />
changing your analogWrite value based on how you want to interpret your<br />
measured data. Does that make more sense now?<br />
Reply<br />
21 Trackbacks<br />
By Beginner Concepts: Using a lowpass filter to smooth PWM output Hack a Day on<br />
June 16, 2011 at 1:02 pm<br />
By Beginner Concepts: Using a lowpass filter to smooth PWM output | You've been<br />
blogged! on June 16, 2011 at 1:24 pm<br />
By hackaholicballa Beginner Concepts: Using a lowpass filter to smooth PWM output<br />
on June 17, 2011 at 12:28 am<br />
By ElectronicsLab.com Blog » Blog Archive » Arduino’s AnalogWrite – Converting PWM<br />
to a Voltage on June 17, 2011 at 2:15 pm<br />
By ArduinoProjects.INFO » Arduino’s AnalogWrite – Converting PWM to a Voltage on<br />
July 24, 2011 at 1:38 am<br />
By Arduino Controls 30,000 Volts « Prometheus Fusion Perfection on September 10,<br />
2011 at 8:10 pm<br />
By Newbie: Need to convert a pulse to flat on January 14, 2012 at 10:30 pm<br />
By новинки высоких технологий on January 26, 2012 at 10:34 am<br />
By What's the easiest way to turn the Arduino's PWM "pseudoanalogue" into real<br />
analogue output? Quora on March 31, 2012 at 9:40 pm<br />
By Converting PWM to a Voltage — Arduino Passion on August 17, 2012 at 8:08 am<br />
By EECS Sr Design » Blog Archive » Out of Time Progress Report I for Prototype II:<br />
Hot/Cold Pad on November 5, 2012 at 9:05 pm<br />
By EECS Sr Design » Blog Archive » Out of Time – Prototype II Final Report: Cool TEC<br />
on December 10, 2012 at 9:03 pm<br />
By Any interest in starting new open source automated brewing project...for propane? <br />
Page 2 Home Brew Forums on January 16, 2013 at 1:05 pm<br />
By electronics | Annotary on January 17, 2013 at 9:08 pm<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 26/27
25/2/2015 Arduino’s AnalogWrite – Converting PWM to a Voltage<br />
By RGB RF Touch Wheel Controller with DIGITAL LED strip via AnalogRead on Arduino |<br />
Nerdcore YAFG on February 6, 2013 at 4:46 am<br />
By MIDI control for the Monotron using a Teensy 3.0 | Marc Nostromo on March 21,<br />
2013 at 4:33 am<br />
By Tonefreqhz DIY pedal. Update on build Les Paul Forums on April 7, 2013 at 4:08<br />
pm<br />
By Mobile Apps | Verisage | Blog on June 18, 2013 at 11:33 am<br />
By My red and cheap interior illumination kit. Page 3 Subaru Outback Subaru<br />
Outback Forums on October 2, 2013 at 12:13 am<br />
By Low Pass Filter for PWM to DC | Austin's Imaging Blog on March 24, 2014 at 11:02<br />
pm<br />
By How to emulate an analog potentiometer with PWM | CLUAT on December 24, 2014<br />
at 6:35 pm<br />
Post a Comment<br />
Your email is never published nor shared. Required fields are marked *<br />
Name *<br />
Email *<br />
Website<br />
Comment<br />
You may use these HTML tags and attributes <br />
<br />
Post Comment<br />
About Us Contact Us Privacy Terms & Conditions<br />
The content of Provide Your Own is licensed under the Creative Commons AttributionShareAlike 3.0 Unported License. See Terms for more information. All content<br />
posted on this site is commentary or opinion and is protected under Free Speech. ProvideYourOwn is not responsible for content written by contributing authors. The information on this<br />
site is provided for educational and entertainment purposes only. It is not intended as a substitute for professional advice of any kind. ProvideYourOwn assumes no responsibility for the<br />
use or misuse of this material. Your use of this website indicates your agreement to these terms and those published here. All trademarks, registered trademarks and servicemarks<br />
mentioned on this site are the property of their respective owners.<br />
This website is created and maintained by Purple Cow Solutions<br />
http://provideyourown.com/2011/analogwriteconvertpwmtovoltage/ 27/27