PWM

From ArduinoInfo
Jump to navigation Jump to search

ARDUINO PWM

The Arduino doesn't have any analogue outputs but it does have the next best thing, PWM outputs. But what are they and how do you use them?

See Here for how-to change PWM frequencies.

How-To Use PWM to make Analog Output for Arduino (click)

But if you want to learn a LOT about PWM and filters, read on!

PWM stands for Pulse Width Modulation, which in itself is not very useful if you don’t know what modulation is. In the electronics world, modulation is the technique of superimposing information on a high speed signal or carrier. The two classic forms of modulation are amplitude modulation, or AM, and frequency modulation, or FM. Those terms have become mixed up in many people’s minds by the radio spectrum, the popular misconception is the AM means medium wave radio, and FM means VHF (Very High Frequency) radio. In fact they simply refer to the method of modulating information, in this case sound signals, onto a higher frequency carrier wave. There is nothing to stop you from broadcasting frequency modulation stations on the medium wave band, apart from the regulatory authorities that is. In FM, the instantaneous frequency of the carrier wave changes as the same rate as the changing, very much lower frequency, sound wave. In AM, it it the amplitude, of size of the carrier wave, that is constantly varied up and down, in time with the variations of the sound wave, or as we say modulating signal.

What is modulation


external image shapeimage_1.png

PWM - lights an LED

In Pulse Width Modulation, PWM, the carrier wave is simply a succession of pulses, or as we say, a pulse train. The width of these pulses changes as the modulating signal changes. Now, you might have come across PWM as a suggested means of controlling the brightness of an LED so what has that got to do with modulating sound waves? Well, it is much the same thing, when we use PWM to control something, the information we are modulating onto the carrier is simply a DC level. You might be able to understand it better if you think of it as simply the average of the waveform taken over the whole of one cycle. A pulse train that consists of manly a low level with the briefest excursion into the high level is going to have, overall, a low average. Contrast this with the opposite situation, a waveform that spends most of the time high and briefly drops low, this will have a high average. That’s what happens when we use the waveform to power an LED. The LED is either full on or full off, it’s the ratio of length of on time to off time that we perceive as being the brightness of the LED. So by altering the width of the wave we can alter the brightness we see. Providing the carrier wave is faster than the persistence of our vision, we don’t see it flickering.

It turns out that there are many other devices whose power we can control simply by turning it on and off rapidly, the trick is that there is some sort of physical mechanism that averages out, or as we say, integrates the rapid switching. With the LED that mechanism was the persistence of vision of the human eye. However, there are other mechanisms we can use. One such allows us to control the output from a heater. With heat there is quite a large thermal inertia, that is there is normally quite a bit of a delay between putting heat in, and that heat spreading out to the surroundings. This is in the order of at least seconds and allows us to use the on / off strategy of PWM.

Another popular use of PWM is to control the speed of a motor, here the mechanical inertia of the rotor acts as the integrating mechanism. However, many people complain that they do not see much in the way of speed control by this method. This is mainly due to the way the motor is switched off, and what mechanical load it is under. A lightly loaded motor will simply coast through the off time, and not show much change in speed for a wide range of PWM values. However, if it is turning some heavy load the motor slows down when it is not being powered and the speed control is more evident. Also it matters how a motor is turned off. Simply removing the power is only one way, the other way is to actually short out the coil to allow flywheel breaking to take place. Here, the collapsing magnetic field generates a voltage that opposes the voltage driving the motor, and tries to power it in the reverse direction. In a motor drive this is done by applying the PWM signal to just one half of the H-drive circuit instead of the enable line. Of course if all you have is a direction and an enable line for your motor controller you can’t take advantage of fly wheel breaking.

All the above examples required the control to be on or off, this is most efficient because the device driving the load is either on, and so has lots of current flowing through it but very little voltage across it. Or it is off, with lots of voltage across it but no current through it. As power is the product of voltage and current on / off control minimises the power burnt off as heat. In the case of the LED it will only work well with on and off control because you need at least a certain voltage across the LED to get any light from it at all.

However, there are situations where you don’t want this all or nothing switching, but to have the actual analogue average voltage, luckily you can still do this with a PWM pulse train. This is normally required when feeding other control devices. The trick here is to have some extra electronics that averages out the waveform instead of relying on some property of the load to act as an integrator. This electronic circuit is known as a filter, and more specifically as we want the low frequency average and not the high frequency pulses this is known as a “Low Pass Filter”. What we want is a filter that removes all the pulses, and leaves us with just the DC content of the waveform. Unfortunately filters are not ideal, and although the high frequencies will be attenuated they will not be removed completely. Also the more you try and remove them the more sluggish the filter will be to respond to any change in the PWM composition. The more complex you make the low pass filter, the better will be both the response to changes, and attenuation of the unwanted signals. So there is a three way compromise going on here.

PWM - other uses

PWM - true analogue output

Filters are characterised by two main features, cut of frequency and slope. There are many other parameters but these are by far the most important ones. As you increase the frequency through a low pass filter the output begins to gradually start to reduce, so it is impossible to tell exactly what frequency it begins to happen.


Therefore we have to define some point we can agree any drop is well underway. The point chosen is where the output has dropped by a half compared to the input, this is called the 3dB point (three dee bee point or 3 deciBels) and we say that the frequency where this happens, is the cut off frequency. Note it is not really cut off it is just reduced by half. The other main parameters is the slope, or how quickly the output drops off after the 3dB point. This is measure in the quaint units of dBs per octave, where an octave is simply a doubling of frequency. This slope is also expressed as the ‘order’ of the filter. A first order filter has a slope of 3dBs per octave. So for a first order filter at a frequency that is double the cutoff frequency the output is down by 6dBs. As a dB is a logarithmic measure, this represents an output that is down by a quarter from the input.Well that is far from ideal, a second order filter drops off at 6dBs per octave, and a fourth order filter at 12dBs per octave. As each 3dBs represents a halving of the signal then 12dBs is actually one sixteenth of the input amplitude.

Filters

Real Filters

Filters are made using components, and the better the filter, the more components they use. The simplest first order filter is simply made from a series resistor and capacitor. They form what I like to think of as, a frequency dependent potential divider. You see, the effective series resistance of a capacitor depends upon it’s capacitance value in Farads and the frequency it is experiencing in Hertz. This is called the ‘Capacitive Reactance’ and given the symbol Xc: Xc = 1 / (2pi FC)

Where F is the frequency and C the capacitance. and pi is pie.

The 3dB point is where Xc is the same as R, in other words the potential divider’s output is exactly half the input signal.

A simple filter consists of just a capacitor and resistor, but what values to chose? First off it is the ratio of the resistor and capacitor value that matters, not the absolute values in defining the frequency response. But more importantly what cut off frequency to design? Well this depends on the application, if the PWM is going to change rapidly, then the cut off frequency needs to be at. or above, the frequency of the maximum rate of change. On the other hand if the PWM is not going to change very rapidly then you can afford the lag or time delay to make the cut off frequency as far away as possible to the PWM carrier wave.

So in practice, one way to design a filter is to decide on how rapidly you want to change the analogue output, then set the cut off frequency to be four times this rate. Four times is a design decision, based on what will really happen, there will be some delay but not so much, you can change this multiplying factor if you like. So, suppose you want to change the analogue output no faster than once per second, the cut off frequency could be set at a quarter of a second or 4Hz. Applying:-

RC = 1/2piF

You get a value for RC of 0.039, so you can substitute actual values of R and C to find what is practical. Start with a resistor value of 1K this needs a capacitor of 39uF, where as 10K would only require 3.9uF. These are not values of capacitors you can get, but 4.7uF and 47uF are. So lets chose them, and put them back into the design to see what our cut off frequency will actually be.

F = 1/2piRC

Gives 3.38Hz, you may consider this hi or low, if so start with a capacitor value you want, or you have, and see what the resistor value comes out at and choose the closest available value. When the real cut off frequency looks acceptable, the final thing to do is to see how much ripple will be on the signal.

Ripple is the vestiges of the PWM signal, and you can never completely remove it, just make it smaller. The default Arduino PWM frequency is 490Hz. so at this frequency the Xc for a 47uF capacitor is 6.9 Ohms. Apply this to the potential divider formula with 1K as R1 and 6.9R as R2. This gives a ratio of 0.00685, remember you started off with a 5V peak to peak signal, so you need to multiply this by five. This reveals you would get about 34mV of ripple. Is that enough or too much? Only you can tell, as you know what you are designing. If it is too much, then you could revisit the design considerations of the cut off frequency. If that is not an option the you can consider altering the PWM frequency of the Arduino to make it higher, alternatively you could use a higher order filter.

Arduino-PWM-Frequency See Here for how-to change PWM frequencies.