Sie sind auf Seite 1von 8

11/11/2017 Arduino PWM to DC Voltage Op Amp Converter | Henry's Bench

H enr ' Benc

Arduino LM358 Op Amp PWM to Voltage Converter

Contents [show]

Creating a DC Voltage using Your Arduino


The PWM output of your Arduino will work ne in many cases. Particularly with
motors and LEDs. However, what if you must have an actual DC voltage? This
tutorial will show you how to create that analog voltage using an LM358.

You will need that Op Amp or one similar, along with a resistor and a capacitor.

Now, Ive attempted to give you a functional description of the circuit, not only to a
you in understanding, but also so that you get a sense for how you may wish to
tweak it.

That said, do feel free to jump right to the tutorial.

Get an LM358
eBay Amazon Bang Good IC Station

The LM358 Op Amp PWM to Voltage Converter


The circuit presented uses an LM358 Op Amp in a Non-Inverting Voltage Follower con guration. If youre
unfamiliar with Op Amps, you can read an easy to follow description HERE.

It also uses resistor and capacitor in series to form an RC time constant that charges to the desired voltage.

You could omit the op amp, but it has some advantages. For one, it has a nice high output impedance.
Second, it offers a degree of protection for your Arduino.

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/ 1/8
11/11/2017 Arduino PWM to DC Voltage Op Amp Converter | Henry's Bench

PWM to Voltage Converter Functional Description


The image and text below serve to give you a sense of how this circuit works.

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/ 2/8
11/11/2017 Arduino PWM to DC Voltage Op Amp Converter | Henry's Bench

1) A PWM signal is applied to the input of the circuit. Pulse width modulation has few characteristics that a
important to us.

The rst is the period of the signal. That is two say, how frequently the PWM signal repeats itself
Next were interested in the amplitude, or how high the signal goes in volts.
Finally, we want to know the percentage of time the signal is on.

Sections
2) Next, the
capacitor C1 charges through R1 to the average value of the capacitor. The time it takes to
charge to this value is determined by the RC time constant. The values for the capacitor and resistor are
largely driven by the time period of the PWM. Another consideration in resistor selection has to do with
maximum current handling capability of the Arduino Output. The capacitor should also have a voltage rating
that is higher than the voltage it is expected to charge to.

One time constant in seconds = Value of Resistor in Ohms x Value of Capacitor in Farads.
It takes ve time constants for the capacitor to charge very close to 100 percent.

3) Finally the Op Amp responds by raising or lowering its output until the value at pin 2 ( the inverting input) is
equal to the value at pin 3 ( the non-inverting input).

Circuit Low Pass Filter in More Depth


C1 and R1 in our circuit are in a con guration that is know as a low pass lter. That means it is designed to
pass lower frequencies while blocking higher frequencies. More it establishes a time constant that allows us
to charge to the value of our Arduino PWM output.

The PWM to Analog Converter Charge Time Constant


Ive selected 4700 ohms for R1 and a 10uF cap for C1. I did it because it will work and (more importantly)
because its what I have around.

Thus my time constant is 4700 * 0.000010 or 0.047 seconds.

When super imposed on a classical RC time constant curve (with a value of 5 volts), it will look something lik
this:

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/ 3/8
11/11/2017 Arduino PWM to DC Voltage Op Amp Converter | Henry's Bench

The PWM to Analog Converter Discharge Time Constant


To get a sense for how the low pass lter interacts with the PWM, it also useful to look at the discharge time
constant. The curve is the inverse of the charge curve.

The PWM Signal Characteristics


There are two default frequencies in the Arduino Uno PWM. One is 490 Hz and the other 980 Hz. Im using
digital PWM pin with a 490 HZ output. The time it takes for it to complete one cycle is determined by
calculating the inverse of the frequency:

Time in Seconds = 1 / Frequency in Hertz

Therefore the period is 1/490 or approximately 2 mSec.

If we select a 50% duty cycle, the timing will look something like this:

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/ 4/8
11/11/2017 Arduino PWM to DC Voltage Op Amp Converter | Henry's Bench

Notice how the output of the PWM falls low after 1 mSec. At this point in time, the capacitor begins
discharging.

How the the Low Pass Filter is Charged


So we have a capacitor that takes 235 mSec to charge and PWM that is only on for about 1 mSec. One the
surface, you might conclude that it will never charge up. However, if you examine the charge and discharge
curves a little closer, you will see that it does charge (albeit slowly) in a pattern that looks like a warped
stairway.

Start with the rst charge cycle

In that millisecond of time, lets estimate what the capacitor will charge to.

Lets cheat. Go to the online calculator found HERE. Insert the resistance and capacitor values, enter 5 for th
supply voltage and enter 1 mSec for the time.

You will see that after this rst millisecond the the capacitor will have charged to 0.1053 volts. (about 2.1
percent of).

Examine the rst discharge cycle

Now when we start the discharge cycle, we have 0.1053 volts across it; not ve volts.

Lets go to another online calculator found HERE. Enter the 0.1053 volts and the other values. Be sure to en
the time.

You will see that in that the capacitor has only lost about two percent and is sitting at about 0.103 volts.

Second Charge Cycle

In the second charge cycle the voltage across the cap starts where the rst discharge cycle left of or about
0.103 volts.

Go back to the online calculator for charging caps. Set your supply voltage to 5.0 0.103 volts ( or 4.897
volts). You will see a result of 0.103 volts.

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/ 5/8
11/11/2017 Arduino PWM to DC Voltage Op Amp Converter | Henry's Bench

This value is what is added to the 0.103 volts that we already have. Thus at the end of this charge cycle we w
have about 0.206 volts.

Second Discharge Cycle

Go back to your discharge calculator and you will see that you lost another 2 percent.. putting the value acros
the capacitor at about 0.201 volts.

Completing the Low Pass Filter Charge

This step process continues until the capacitor charges to about 2.5 volts. It will take about ten RC time
constants to get there ( about half a second).

When it gets there, the small charge and discharge cycles will continue. Using an oscilloscope, you will see
them riding across the top of the DC value. My set up yielded about 50 mV of ripple. Ill show you how to cle
that up in another tutorial.

Arduino LM358 Op Amp PWM to Voltage Out Tutorial


LM358 Pin Outs
Youll be using an LM358. If you use an eight pin DIP, the pins are as show below.

Connect You LM358 Circuit to Your Arduino


You will want to use a common external supply that is greater than 5V. A 9 volt supply would work. If you tr
to power the Op Amp with 5 Volts will cap your output voltage to about 3.6 volts.

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/ 6/8
11/11/2017 Arduino PWM to DC Voltage Op Amp Converter | Henry's Bench

Copy Paste and Up Load the Arduino PWM to Voltage Out Converter
Sketch
The sketch is really basic. Youre going to cycle through various voltages in three second intervals.

// Henry's Bench LM358 Arduino PWM to Voltage Converter

int pwmOut = 3; // The output to the transistor that drives the motor

void setup()
{
pinMode(pwmOut, OUTPUT);
}

void loop()
{
analogWrite(pwmOut, 0); // about zero volts
delay(3000);
analogWrite(pwmOut, 51); // about 1 volt
delay(3000);
analogWrite(pwmOut, 102); // about 2 volts
delay(3000);
analogWrite(pwmOut, 128); // about 2.5 volts
delay(3000);
http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/ 7/8
11/11/2017 Arduino PWM to DC Voltage Op Amp Converter | Henry's Bench

analogWrite(pwmOut, 153); // about 3.0 volts


delay(3000);
analogWrite(pwmOut, 204); // about 4.0 volts
delay(3000);
analogWrite(pwmOut, 255); // about 5.0 volts
delay(3000);
}

Test Your Output


Connect a meter to your pin one on your LM358. If you observe it long enough, you will see it march throug
the programmed values.

Share this:

Share 18 203 submit Tweet More

Proudly powered by WordPress

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/ 8/8

Das könnte Ihnen auch gefallen