Really straightforward question, not related to any projects, but its been bugging me for a while.
Say, I set up an arduino pin with 50% PWM frequency, which basically means keeping the input on half the time and off the other half. which in turn turns an LED at 50% brightness.
pretty basic stuff.
BUT.
add a parallel capacitor with the led, it averages the voltage?! 5v, 50% , 2.5V?! As i understand capacitors, wouldnt it be charged with each rising edge of the pwm frequency? which also means the capacitor is charged with the peak voltage. Yes, the voltage does fall on the other half of the pwm. But still, the capacitor is charged with 5V, so basically it would give a rough sawtooth voltage. The larger the capacitor, the lesser(word?) the voltage falls between each peak.
The capacitor and the internal resistance of the port form a low pass filter, with output ripple that depends on the load. There are any number of discussions on the web, so check them out.
Your charged capacitor has to discharge back through the high impedance Arduino pin, so very little current can flow that way. Experiment with various resistor values across the capacitor to increase the discharge current.
kaseftamjid:
an arduino pin with 50% PWM frequency, which basically means keeping the input on half the time and off the other hal.
Here you go wrong.
The pin toggles between high and low signals, not on and off. Or to say it simple: half the time the output is connected to Vcc, half the time to GND. That this makes the LED turn on and off is a whole different matter, the pin does not turn on or off, it just changes its potential.
The pin indeed has a third state where it's an input, which for the outside world is a "high impedance" state, effectively not allowing any significant current to flow in or out of the pin. This state is not used in PWM. This is what you could call "off" but it's a stretch.
I suggest you give this a read to better understand things. You will see similar to the sawtooth you mention but there is much, much more to it.
When looking at a square or rectangular waveform like PWM for example E peak = R average = E rms or actually today change my E designations to V designations.
There needs to be consideration for the frequency used for the pwm (the link uses 100 KHz) and note the associated formulas. If I have a DC source or pulse DC source and I place it across a capacitor what happens? What will, for example the instantaneous current be across the cap? How long will it take the capacitor to charge?
The link does fairly well at explaining things. If you want the average value of a PWM signal.
This is quite a bit over-simplified but when I was first learning electronics it was taught that:
Capacitors in series "block DC" but allow AC through.
Capacitors in parallel "resist changes in voltage". (You can think of that as "averaging" or "smoothing".)
In this, the yellow dots represent the current flow and the green shading represents voltage. Faster yellow dots is more current, greater green saturation is higher voltage.
kaseftamjid:
Really straightforward question, not related to any projects, but its been bugging me for a while.
Say, I set up an arduino pin with 50% PWM frequency, which basically means keeping the input on half the time and off the other half. which in turn turns an LED at 50% brightness.
pretty basic stuff.
BUT.
add a parallel capacitor with the led, it averages the voltage?! 5v, 50% , 2.5V?! As i understand capacitors, wouldnt it be charged with each rising edge of the pwm frequency? which also means the capacitor is charged with the peak voltage. Yes, the voltage does fall on the other half of the pwm. But still, the capacitor is charged with 5V, so basically it would give a rough sawtooth voltage. The larger the capacitor, the lesser(word?) the voltage falls between each peak.
so what is happening in here?
Firstly you need a series resistor before the capacitor to avoid overloading the pin.
Secondly output pin driver transistors on the chip have about 40 ohms on-resistance, so there is
already resistance.
The capacitor is charged, then discharged, then charged, etc etc, but not instantly. If the time-constant is
much more than the period of the PWM then the output voltage is roughly constant. The time-constant
is simply RC as ohms x farads = seconds (yes, really).
We say that it takes 5 RC time constant seconds to (very nearly) fully charge a capacitor.
Now let's say that during the charging portion of the 50% PWM the cap ends up with
1.0 volt (of the 5 volts applied) terminal voltage. When the discharge starts, the current
flows from 1.0 volt not 5 volts as before. So the charging current is more than the
discharge current. As time goes on, the cap continues to gain charge until the current
in equals the current out.
Herb
Actually something like 63% during the first RC time constant, then the remaining 4 get us to about 99%. So with a 5 volt PWM of 50% after 1 RC time constant we would be about 63% of 2.5 volts or 1.45 volts.
Ron_Blain:
I suggest you give this a read to better understand things. You will see similar to the sawtooth you mention but there is much, much more to it.
When looking at a square or rectangular waveform like PWM for example E peak = R average = E rms or actually today change my E designations to V designations.
There needs to be consideration for the frequency used for the pwm (the link uses 100 KHz) and note the associated formulas. If I have a DC source or pulse DC source and I place it across a capacitor what happens? What will, for example the instantaneous current be across the cap? How long will it take the capacitor to charge?
The link does fairly well at explaining things. If you want the average value of a PWM signal.
DVDdoug:
This is quite a bit over-simplified but when I was first learning electronics it was taught that:
Capacitors in series "block DC" but allow AC through.
Capacitors in parallel "resist changes in voltage". (You can think of that as "averaging" or "smoothing".)
In this, the yellow dots represent the current flow and the green shading represents voltage. Faster yellow dots is more current, greater green saturation is higher voltage.
Why isnt there an option to select the voltage of the pwm frequency? Only an option for the frequency?
herbschwarz:
We say that it takes 5 RC time constant seconds to (very nearly) fully charge a capacitor.
Now let's say that during the charging portion of the 50% PWM the cap ends up with
1.0 volt (of the 5 volts applied) terminal voltage. When the discharge starts, the current
flows from 1.0 volt not 5 volts as before. So the charging current is more than the
discharge current. As time goes on, the cap continues to gain charge until the current
in equals the current out.
Herb
What happens when the capacitor is fully charged? Does it stay in constant state?,ie. Fixed charge and discharges?
Ron_Blain:
Actually something like 63% during the first RC time constant, then the remaining 4 get us to about 99%. So with a 5 volt PWM of 50% after 1 RC time constant we would be about 63% of 2.5 volts or 1.45 volts.
kaseftamjid:
Why isnt there an option to select the voltage of the pwm frequency? Only an option for the frequency?
Because there is no voltage to select, and you normally won't do anything about the frequency (default on Arduino is 480 or 960 Hz depending on the pin used). PWM is a square wave switching between Vcc and GND. You normally just select the duty cycle from 0 (always low) to 255 (always high).
kaseftamjid:
What happens when the capacitor is fully charged? Does it stay in constant state?,ie. Fixed charge and discharges?
Depends on how the cap is wired, whether there's any discharge path.
An ideal cap when fully charged & disconnected keeps its charge, but that's where the real world comes in play: all caps have self discharge, how much depends on the cap (make, technology, voltage, temperature, age, etc).
There are many more real life things to caps. Especially ceramic caps see a stark reduction in capacitance as the voltage across it rises. Electrolytic caps tend to act more as an inductor than a capacitor at high frequencies. That's why in many circuits you see large electrolytic caps with one or more small ceramic caps in parallel.