Calculations for RC filtering PWM to create an analogue voltage

Thread Starter

Travm

Joined Aug 16, 2016
363
I'm building a simulator for a current monitoring device with PID control. I haven't gotten into any fancy calculations beyond the basic ohms law stuff for resistor sizing. I used LTspice to simulate and size my RC circuit.
I would like to figure out a way to calculate what my output voltage will be based on my PWM output.
Its somewhat confusing to me because LTspice is showing a non-linear relationship between vout and the pwm ratio.

forgive me because so much of this is new to me, and i have in the last 3 months learned so much that my head is somewhat scrambled. I have probably read (multiple times) the calculations for this, but I'm fighting with comprehension a bit still.

I have built the simulator and it works, but I need to properly understand how the PWM --> RC filter thing works.
2016-12-04.png IMG_20161205_194751757.jpg
 

MrAl

Joined Jun 17, 2014
11,496
I'm building a simulator for a current monitoring device with PID control. I haven't gotten into any fancy calculations beyond the basic ohms law stuff for resistor sizing. I used LTspice to simulate and size my RC circuit.
I would like to figure out a way to calculate what my output voltage will be based on my PWM output.
Its somewhat confusing to me because LTspice is showing a non-linear relationship between vout and the pwm ratio.

forgive me because so much of this is new to me, and i have in the last 3 months learned so much that my head is somewhat scrambled. I have probably read (multiple times) the calculations for this, but I'm fighting with comprehension a bit still.

I have built the simulator and it works, but I need to properly understand how the PWM --> RC filter thing works.
View attachment 116492 View attachment 116493

Hello there,

Let me see if i can give you a little insight into this mystery.

First, a regular calculation for the filtered output voltage of a PWM is very simple. It is related directly to the PWM high and the PWM period. So for example if we have a 10 second period and a 1 second 10v high pulse (that means 9 seconds low at 0v) then we would have a filtered output voltage of 1 volt because 1/10 is equal to one tenth of the high state pulse voltage.

There is a slight catch there though, and that is that this calculation depends on the filter being able to properly average the PWM signal. That means that only certain circuits will provide this input to output relationship.

To give an example of when this works, if we had a perfect driver that provided a pulse of exactly 10 volts when high and exactly 0v when low and fed a resistor and a capacitor and the RC time constant was large enough to filter out the ripples, then we would see the actual average voltage of 1 volt DC with a little ripple on it.
The things to note here are that:
1. The RC time constant is large enough to filter out the ripples.
2. There is only ONE resistor, not two, and that means the SAME resistor is used to charge and discharge the capacitor. That means the capacitor is never left open circuited, and the capacitor is never discharged with another resistor or a different resistor value.

If you use a discharge resistor in addition to the charge resistor, then the outcome is a little different.
In fact, if the circuit is any different than one resistor and one capacitor then there could be many other things to take into account, so each circuit could be different. If you can explain your circuit a little better that would help here.

For example, which cap and resistor(s) are you using to filter the PWM?
What it looks like is you are driving some LEDs with the filtered output, and that equates to more than one resistor so this circuit will be different.
I'll try to get back here a little later today with some calculations if someone else doesnt do it first.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
For example, which cap and resistor(s) are you using to filter the PWM?
What it looks like is you are driving some LEDs with the filtered output, and that equates to more than one resistor so this circuit will be different.
I'll try to get back here a little later today with some calculations if someone else doesnt do it first.
So I have 2 transistors set up to switch 12v and produce an analog voltage output. This output is to drive a load, today its 3 LED's in parallel. There is a resistor and op amp for current sensing. The circuit is extremely similar to what is shown in the LTspice image. It functions about the same too.

The filter resistor is 27 ohm (not 25 as in the image) and the cap is a 27 uF aluminum. Behind that i have a 2ohm current sense resistor (wire wound, i know that is bad, but hey this is just a simulator for learning), then a bunch of resistors to feed the opamp that will feed back to my MC, then 3 parralel LED's with each their own current limiting resistors.
 
Last edited:

MrAl

Joined Jun 17, 2014
11,496
Hello again,

Ok, one such formula looks like this:
VoutPeak=(R2*(e^(tc/(C1*R2)+tc/(C1*R1))-1)*(e^((2*td)/(C1*R2)+tc/(C1*R2)+tc/(C1*R1))+1))/((R2+R1)*(e^(td/(C1*R2)+tc/(C1*R2)+tc/(C1*R1))-1)*(e^(td/(C1*R2)+tc/(C1*R2)+tc/(C1*R1))+1))

and a secondary formula is like this:
VoutValley=VoutPeak*e^(-td/(C1*R2))

These two give the highest point and the lowest point on the output wave, so the average of these two would be the average output voltage. This is after the circuit has been switched on and is running constantly for at least 10 time constants of the slowest time constant.

In the formulas we have:
R1 the resistor connected to the transistor,
R2 the LED's equivalent resistance,
tc the charge time,
td the discharge time,
C1 the filter capacitor value.

Note this formula still has to be checked and i will do that a little later today hopefully or tomorrow morning sometime.

There may be a simplification we can apply also which would lead to a simpler form, but those two formulas are the result of a direct calculation in time.

Note if the capacitor value is not large enough it will lead to a big difference between VoutPeak and VoutValley, which may be unacceptable so you may have to increase cap size.

What frequency do you intend to use with the PWM?
 

Thread Starter

Travm

Joined Aug 16, 2016
363
In the formulas we have:
R1 the resistor connected to the transistor,
R2 the LED's equivalent resistance,
tc the charge time,
td the discharge time,
C1 the filter capacitor value.

Note this formula still has to be checked and i will do that a little later today hopefully or tomorrow morning sometime.

There may be a simplification we can apply also which would lead to a simpler form, but those two formulas are the result of a direct calculation in time.

Note if the capacitor value is not large enough it will lead to a big difference between VoutPeak and VoutValley, which may be unacceptable so you may have to increase cap size.

What frequency do you intend to use with the PWM?
Frequency i'm looking for is 100khz, although for testing purposes it can be flexible. In the final adaptation it must be 100khz.

I don't see how the resistors connected to the transistor affect the RC network. mind you i do see how the LED's and LED limit resistors might.
I'm looking more for the, how does it all work answer, than the specific formula. Although i appreciate the help.

In your equation i see a repetitive group of values. What is this and where might read about it?

I have lots of experience simplifying complicated weld strength equations so this sounds almost fun, but I need to understand exactly what is going on in the RC network and why the other resistances and loads affect it.
 

MrAl

Joined Jun 17, 2014
11,496
Frequency i'm looking for is 100khz, although for testing purposes it can be flexible. In the final adaptation it must be 100khz.

I don't see how the resistors connected to the transistor affect the RC network. mind you i do see how the LED's and LED limit resistors might.
I'm looking more for the, how does it all work answer, than the specific formula. Although i appreciate the help.

In your equation i see a repetitive group of values. What is this and where might read about it?

I have lots of experience simplifying complicated weld strength equations so this sounds almost fun, but I need to understand exactly what is going on in the RC network and why the other resistances and loads affect it.
Hi,

Ok, well then here we go...

Referring to your schematic, the 25 ohm resistor causes the charging of the 27uf cap, and the 2 ohm resistor is in series with the LEDs and the LEDs are emulated with a resistor who's value is the normal run voltage divided by the average current which allows a great simplification that allows us to create a closed form solution. Without that simplification we would have to do a complete non linear analysis which would lead to a numerical only solution which is certainly not as convenient and probably not much more accurate assuming the cap can filter the PWM well enough to keep the ripple small.

What this means is that we have a circuit that is made up of:
1. A controlled switch that is either on or off. We may include a small voltage drop when the switch is on but for now let's call it a short when it's on.
2. An 'upper' resistor (the 25 ohm resistor) we can call "R1".
3. A 'lower' resistor (the LEDs) we can call "R2".
4. A voltage source (V1).

When the transistor turns on (via signal) the 25 ohm conducts and that charges the cap and also after a short time starts to allow current to flow through the LED's and they light up. The transistor stays on for the pulse high time (tc in the formulas). During that time the cap charges up and the LEDs light up.
When the transistor turns off, the 25 ohm disappears from the circuit (a distinct change in topology) and we only have R2 (the LEDs) and the capacitor. The capacitor supplies current to the LEDs now, and so they stay lit up. The voltage across the cap drops a little as the LEDs use the energy from that cap.
That's about it, and then the cycle repeats as the transistor turns on again via signal.

An analysis of the above requires looking at the charge equation and the discharge equation with included initial conditions. They are then combined in such a way so that the final value of one equation leads to the initial conditions of the next equation, and then that next equations final value becomes the initial value of the former equation, and then the former equation final value again becomes the initial vaue for the second equation again, etc., etc. At some point we reach a sort of equilibrium where the average voltage is about in the middle of the transition from a slightly lower voltage to a slightly higher voltage and that is the ripple. Once that point is reached the signal just repeats over and over, and that is actually reached once the cap charges up to the average value which could be around 5 to 10 time constants.

The repeated values such as R1*C1 and R2*C1 are called the time constants, and are often represented with the Greek letter tau. So using upper case T for tau for now, we would have:
T1=R1*C1
T2=R2*C1

and you could substitute those into the equation to make it slightly simpler to write out.

The charging of a capacitor with time constant RC=R*C is as:
Vc=Vs*(1-e^(-t/RC))

and the discharge is as:
Vc=Vc0*e^(-t/RC)

and that is the basis of the formula although there are also initial conditions for the charge form:
Vc=(Vs-Vc0)*(1-e^(-t/RC))+Vc0

The long way is to start with the charge form and start with Vc0=0 (cap discharged) and then allow t to equal the pulse high time at the voltage Vs of the pulse, then use that result in the discharge formula to calculate the new value of Vc, then use that result back into the charge equation again, then use that result back into the discharge equation again, etc., etc., and eventually reach a repetitive point and that would be the normal running result. If you change the duty cycle (to change the LED current) then you have to run the formulas again. The time between the change and equilibrium again will be roughly about 10 time constants but you could check for that too by observing the results as they are calculated.

The closed form solution comes from combining them in a recursive manner to come up with a single formula which provides a direct calculation. You might want to try this yourself using a series which might be reducible, or you may find another way.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
Thats more or less how I thought it worked. But thanks for the indepth answer.

I guess i'll have to plot the formulas in a spreadsheet to better understand them. I still dont understand why i need to make the duty cycle so low to get a decent drop in current. its like the difference between 90% and 25% is more like 1% and the difference between 25% and 1% (duty cycle i'm talking about) is 99% (current).

Need to let my brain recharge, its still smoking from learning how to solder.
 

MrAl

Joined Jun 17, 2014
11,496
Hi,

I must not have been notified about your reply here as i am just seeing it now.

I am not sure what you are asking here or what you dont understand. Perhaps rephrase your reply and that might help.
If you are talking about the charge and discharge times, when the voltage DIFFERENCE is greatest we see the fastest change in capacitor voltage level. The difference is the difference between the source voltage and cap voltage when charging, and the difference between the cap voltage and ground when discharging (so it's the cap voltage when discharging).
 
Top