# PWM to Analog conversion.

Hi all

I need to prepare a circuit to get 0-10v real analog DC output by using PWM outputs of Arduino.

I am aware that this issue has been discussed at many topics in this forum and be sure that I have studied all of them carefully. As long as I read the related topics I figure out somethings better but confused on new things.

I have found many advices and the attached circuit in this forum seemed me more friendly and simpler. But I have to ask a few point to let me sure this is what I realy want.

This circuit switches the 12V supply according to the PWM frequency (490hz for Arduino) and then the output is being applied to RC filter for analog version of PWM signals.

My questions is that;

1. When a load (motor, bulb, speed adjustment input of a industrial servo driver, etc) is connected and the gate of transistor is zero (logic “0” from Arduino) then the current will go from ground to load and then through to the 680ohm resistor and then 1K resistor and finally +12V of supply. In this case, an amount of voltage will drop on resitors (according to current consumption of the load) and so the load will be activated with reduced power even the arduino output to the base is zero. Is this true?

2. When the base is trigged by outputing the logic 1 from arduino, then the ground will be connected to the +12 volt thourgh the 1K resistor. In this case the load will be like parallel to this 1K resistor and so on.

If I am correct, please help me by posting a new shematic for my purpose. By the way, I am not interested in opamp solutions. I need transistor+RC filter solution.

Basically its a low pass filter and is not designed to have a significant load on it, so forget motors and the like - the output voltage will go into some sort of amplifier before being used. If you drive a motor the PWM passes all the way to the motor and its inductance and inertia are utilised to do the low-pass filtering.

The capacitor stores charge and acts to smooth the voltage fluctuations that are present at the collector of the transistor. The law for a capacitor is that the rate of change of voltage is proportional to current into the capacitor divided by the capacitance. The resistor(s) sets the current for a given voltage input.

That circuit has the wrong component values for a 490Hz PWM (suspect its for more like 10kHz or more)

The simplest circuit that does the job is a single R connected from a PWM output pin and to a C that is grounded at the other side. For 490Hz something like 10k and 10uF would be reasonable giving a ripple of 50mV or so and a bandwidth of half a second or so. For more bandwidth you need to up the PWM frequency and use RC values more like that circuit.

Incidentally that circuit has one big issue - its response is not linear since it drives up through 1.47k and down through 470 ohms...

Thank yo very much indeed MarkT

yes I know that this circuit is not for big load at output. But I was thinking that this is beacuse of transistors (BC series). I was expecting to change the transistor with Tip120 (and resistors accordingly of course) and so it can drive higher loads (DC motor, bulb, relay etc) at output.

According to your explanations I see that this expectation is invalid. Correct?

Regarding your basic solution (with only RC components), I am certainly agree with you but it would be valid if I need analog output 0-5VDC. My purpose is to get analog values higher then logic levels.

Thanks again.

so it can drive higher loads (DC motor, bulb, relay etc) at output.

You don't need PWM to drive a relay, it fact it would be bad to do so. DC motors and bulbs do not need PWM to be smoothed. Smoothed PWM is simply a DC voltage.

So I am not sure what you want to do.

If you want other voltages then replace the +12V supply with the voltage you want. If you want to drive a heavy load with smoothed PWM then use that circuit and follow it with an emitter follower or source follower circuit to drive your load. You will then be operating in the linear part of the device's characteristics so it will dissipate some power.

Many thanks Grumpy_Mike...

The fact that, I have high power industrial Servo motor (400W-24VDC). It has a built in driver. The driver has many inputs to manage the motor. The main inputs which I am related are direction and speed pins. The direction selection is the two pin (?nput 1 and input 2). These two pins are for four options. Hold with no torque, Hold with torque, clockwise direction and counterclockwise direction. All those pins are not basic logic levels. They accept 0V as low and 24V as high. So I can easily manage this input via simple transistor+relay combination connected to any digital port of arduino.

On the other hand, The speed pin needs 0-10V (analog) 120mA supply. 0V means 0rpm and 10V means 4080rpm. So I need a circuit to feed this input. I thought that PWM+smooting with RC filter would serve my goal. And your circuit seemed me good on my goal. But I could not explain myself how it works principally. What happens at cut-off and at fully saturated mode? How does it act? How does it output smoothed output between 0-10V levels?

Finally I thought to build a better circuit for general purpose of PWM to analog conversion which also helps for higher loads in case of necessity to avoid redesigne of PCB, components, etc. I simply thought to change the BC series transistors with TIP120. But now I see that, general purpose achivement is not possible or not so easy.

Best,

http://www.thebox.myzen.co.uk/Tutorial/PWM.html

What happens at cut-off and at fully saturated mode?

The transistor looks just like a switch it is either on (collector connected to ground) or off (collector connected to nothing).
So the signal at the collector is either 0V or +Vcc (supply volts which is 12V in that diagram)

How does it output smoothed output between 0-10V levels?

The next two components are a low pass filter, this smothers out the rapid change to give a DC value.

Thanks for the link. I have read it carefully, printed and stored in a good place in my library.

I am just trying to understand the behaviour of the circuit instead of being a blind applier. Sorry to ask you same question again.

When the transistor is off state and the output is connected to speed control input pin of built-in driver of the servo motor then the current has an only way to follow as below.

gnd -> motor driver -> 680R -> 1K -> Vcc

In this case a voltage will drop on motor driver but this will be neither +Vcc nor 0V. Correct?

When tha transistor is saturated (fully on) then a new parallel path for current will be open (gnd->transistor->1K->+Vcc)

In this case Vce will be almost zero but above situation is still going on.

I am not confident that this story is true but it it is not then I need to know where I am wrong.

Best

It is easiest to think of measuring the output voltage with a high impedance (no load) multimeter The output voltage will vary from (almost) Vcc to (almost) zero

I say almost because a) when it should be Vcc there is a potential divider between Vcc and the meter so as long as your meter is high impedance, the difference is very small

b) when the transistor saturates it will hold the output at Vce(sat) which can be as low as 0.1 volts

you would normally use a high input impedance driver after this circuit connecting a motor directly will probably not work as it is very low impedance

you might just get away with an LED

leastways, that's how I see it :)

mmcp42:
It is easiest to think of measuring the output voltage with a high impedance (no load) multimeter
The output voltage will vary from (almost) Vcc to (almost) zero

That is what I have done just now. I have on my desk with a multimeter+osscilascope+arduino+power supply 12v+components+cables+notebook

I will share the results as soon as I get
Thanks

b) when the transistor saturates it will hold the output at Vce(sat) which can be as low as 0.1 volts

Wow where can I get transistors like that, normally it is between 0.7V and 2V.

but above situation is still going on.

Yes but the bulk of the current will flow through the transistor because at that point the voltage driving into your load is only the Vsat of the transistor, so any current into your motor driver will only be being pushed at less than a volt. This is too small to activate it.

Ok guys,

Here are the first results.

1) The circuit works. 2) Analogwrite(9,255) gives zero volt output 3) Analogwrite(9,1) gives +12V (meaning Vss) 4) The range is not lineer. I mean that AnalogWrite(9,127) gives 4V (not half of Vss=6V) Please note that I have used BC639 instead of BC183. Because I had in my hand at the moment. I did not change the resistors. Is this behaviour coming for this change of transistor?

These are values from meter without any load at output Now, I will take a look at the scope results. Comming soon.

hello again,

The scope result says that the circuit is not doing its job.

The waveform at output is like sawtooth. When going to both end (I mean analogwrite(9,1) and analogwrite(9,255)) the waveform is going better (very near to real DC)

But after going far from these two cases it is becoming sawtooth and at the middle (analogwrite(9,127) it become like a triangle.

I will play with the values of RC filters now.

hartoksi: Ok guys,

Here are the first results.

1) The circuit works. 2) Analogwrite(9,255) gives zero volt output 3) Analogwrite(9,1) gives +12V (meaning Vss) 4) The range is not lineer. I mean that AnalogWrite(9,127) gives 4V (not half of Vss=6V) Please note that I have used BC639 instead of BC183. Because I had in my hand at the moment. I did not change the resistors. Is this behaviour coming for this change of transistor?

These are values from meter without any load at output Now, I will take a look at the scope results. Comming soon.

that's not too surprising There will be a value of AnalogWrite() that saturates the transisitor any values higher than that will still only saturate it

it should be fairly linear below that value

so i would guesstimate that AnalogWrite(190) or thereabouts will give close to zero?

Grumpy_Mike:

b) when the transistor saturates it will hold the output at Vce(sat) which can be as low as 0.1 volts

Wow where can I get transistors like that, normally it is between 0.7V and 2V.

when i were a lad the old geranium (sic) transistors had very low Vsat mind you they also had very high leakage current, too!

hartoksi: hello again,

The scope result says that the circuit is not doing its job.

The waveform at output is like sawtooth. When going to both end (I mean analogwrite(9,1) and analogwrite(9,255)) the waveform is going better (very near to real DC)

But after going far from these two cases it is becoming sawtooth and at the middle (analogwrite(9,127) it become like a triangle.

I will play with the values of RC filters now.

I'd be interested to know what frequency sawtooth you are getting the values of C and R should give a Tcr of about 0.5 mS

I am unable to measure correctly the frequency because my scope in an old analog scope so the shapes are not stable on the screen.

I have changed the RC values 10mf+6.8K and the output is now almost real DC

P.S: If it is important for you to know aboput the frequency I can try to measure it more or less.

There will be a value of AnalogWrite() that saturates the transisitor

Any value of analogWrite() will saturate the transistor, it still gives a full voltage pulse, not a DC level.

the old geranium (sic) transistors had very low Vsat

True but you can't get them easily anymore and they never handled that much current anyway.

I have changed the RC values 10mf+6.8K and the output is now almost real DC

I think you might mean 10uF not 10mF.

Have you tried some of the other PWM pins? I seem to remember that some are a low frequency and others are high. The higher frequency ones need smaller RC values to look smooth.

what's important is you fixed the problem Trc is now 100 times bigger, so you can't see the sawtooth any more :)

(an approximate frequency value is good enough, just for interest; don't need 2 decimal places :) )

Grumpy_Mike:

There will be a value of AnalogWrite() that saturates the transisitor

Any value of analogWrite() will saturate the transistor, it still gives a full voltage pulse, not a DC level.

oops my mistake so what makes it non-linear?

so what makes it non-linear?

Any number of things, the first to look at would be the turn off time of the transistor, this can happen due to the base charge effect. You can try reducing the base current (making the base resistor higher) so it is less saturated and so turns off quicker. It could also be the characteristics of the transistor.

Circuit has a “by design” non-linearity, due asymmetry output impedance for “high” and “low” state. High state will include sum resistance of R1 and R2, the same time only R2 for Low state. It easy to derive equation:
Vout = Vp * R2 / (R2 +D(R1 + R2)) , where D = analogWrite / ( 256 - analogWrite).
Substitute R2 = 680, R1 = 1000, Vp = 12, D = 1 gives Vout = 3.4 V.
The diode connected in parallel with R2 will “straighten out” output characteristic,
Vout = Vp *(255 - analogWrite) / 255, when R1 = R2.
It obvious, that circuit “invert” signal, analogWrite has a minus sign. To make it works “proportional”, value that send to analogWrite has to be inverted as well.