Home
Login / Signup
 di 

How-to use PWM to Generate Analog (or Analogue) Voltage in Digital Circuits – Part 2

How-to use PWM to Generate Analog (or Analogue) Voltage in Digital Circuits

The PWM (Pulse Width Modulation) Solution

Many times, designers are faced with the need of generating analogue or analog voltage levels in purely digital circuits. Although the market provides today a very broad range of dedicated digital-to-analogue converters, putting such a device in the schematic has a negative impact on the overall cost of the system.

In the first part of this article (Generating Analog Voltage with Digital Circuit (I) ), I have detailed what is known as the R2R method for obtaining an analogue voltage. The advantage of this method is that it is very much similar to use a real off-the-shelf DAC integrated circuit; the digital values you would need to generate with the code running on the microcontroller are the same in both cases.
The main drawbacks of this method, however, is the high number of pins required on the microcontroller (directly proportional to the resolution you need) and the relatively high number of external components needed (an additional pair of resistors for each additional bit of resolution).
In order to address these problems, another solution is available: the PWM solution. This allows us to obtain analogue voltage using just one pin of the microcontroller, and as little as two external components. The block diagram of such a concept is relatively simple:

 

Block diagram of PWM solution
Figure 1 – Block diagram of PWM

 

The theory behind the solution is that if you have low pass filter good enough and a pulse train signal, you will be able to obtain at the output of the filter the medium value of this particular signal; the medium value of a pulse train signal is always proportional to the duty cycle of the pulse train. If your duty cycle is 0%, then your PWM signal is actually always 0, and the signal at the output of the filter will obviously be also 0. If the duty cycle is 100%, then your PWM signal is actually a continuous voltage signal, equal to VCC, which even filtered, will yield the same continuous value: VCC.
In case the duty cycle is varied between these two limits (0% and 100%) the low-pass filter (provided it is correctly dimensioned) will basically cut the variations in the signal, leaving only the continuous component of the signal (its medium value) pass through. The diagrams below illustrate the case in which the duty cycle is 50%, 90% and 10% respectively. The green represents the PWM signal, while the red represents the continuous (analogue) signal obtained at the output of the filter.

 

Duty cycle 50%
Figure 2 – Duty cycle 50%

 

 

 

Duty cycle 90%
Figure 3 – Duty cycle 90%

 

 

Duty cycle 10%
Figure 4 – Duty cycle 10%

 

This solution is ideal in case you need to generate a continuous voltage, which does not change very much in time (but which is software adjustable), or which does not need to change very fast. Many microcontrollers provide dedicated PWM modules, which can be used for the purpose described here. If PWM modules are not available, timer modules are the next best choice, as they allow you to precisely create the PWM signal you need with a relatively low amount of on/chip resources (a timer, an interrupt routine, and a few RAM locations). But even if you are using the cheapest, smallest and poorest microcontroller, you can still create PWM signal by directly controlling an IO with your code, either in a loop, with routines or with interrupts.
One of the most important aspects of employing this solution is the dimensioning of the low-pass RC filter, which is one of the most simplistic low pass filter configurations available.

The RC filter
Figure 5 – The RC filter

The most important thing is the relation between the time constant of the filter (t=R*C) and the period of the PWM signal. As a rule of thumb, you should dimension your R and C, so that the time constant is at about 100 times higher than the period of the signal. If your PWM frequency is 10 kHz (which means 100us period), you should choose a time constant of about 10ms – easily obtainable with R=1k and C=10uF (easy to get values).
In case you decrease the time constant with respect to the period of the PWM (say from 10ms down to 1ms), the filter will not be able to dampen the oscillations of the PWM signal, and you might end up with something like this (again, red is the output of the filter):

PWM period: 100us; Filter time constant: 1ms
Figure 6 – PWM period: 100us; Filter time constant: 1ms

Increasing the time constant too much is also not recommended. This method can be used to obtain a non-continuous analogue voltage, in case you actually vary the duty cycle of the PWM signal in time (which is what PWM really means), but if the time constant of your filter is too high with respect to the period of the PWM signal, then the filter will have a slow response, basically limiting the variation speed of your output signal. In the following diagram, you may see the effect of a too high time constant over a PWM with the duty cycle which varies from 0% to 90% to 50% and finally to 10%.

“Real” PWM: variable duty cycle 0%->90%->50%->10%
Figure 7 – “Real” PWM: variable duty cycle 0%->90%->50%->10%

As you may see, the response of the filter is slow, rendering the used time constant unsuitable for generating “fast” changing signals. The method is however, more than adequate for creating either continuous or slow changing analogue signals, like for instance a 50 Hz sinewave. In order to create a sinewave, the duty cycle of the PWM signal would have to vary in time by a sinusoidal formula (easy to achieve in high end microcontrollers, a little more difficult with simpler ones, but not impossible – you can always use tables stored in program memory).
A warning is needed with respect to the error sources which might affect the output signal (apart from the dimensioning of the filter). In many cases the minimum high output voltage on a pin of the microcontroller is specified to let’s say VCC-0.2V. This means that the pin will never be able to reach the VCC value in its high state and this will affect all the calculations that you make. If you make your computations for a nominal case, you would draw the conclusion that you need a 50% duty cycle to obtain a continuous output signal of 2.5V from a 5V supply. But this is ONLY in case your pin actually reaches 5V in a high state. If it will only reach 4.8V, you will only get 2.4V at the output for a 50% duty cycle. You may work around this by using some additional external components, like for instance a fast and high current transistor:

External transistor
Figure 8 – External transistor

Bear in mind though, that even if you make the signal at the input of the filter to vary between the rails (0 and 5V) the additional resistor in the collector of the transistor will affect the response of the filter (in calculations, it should be considered in series to the filter resistor).
One of the more difficult error sources to correct though is the variation of the power supply itself. You might count on it being 5V but the truth is that most voltage regulators have a tolerance (commercial ones go to +/-4%). You can surely get more expensive ones, which can bet to +/-1%), or you can also get a voltage reference, to which you can connect the resistor in the collector of the transistor. If you want to prevent cost increases though, a solution to account for this error might be the use of an on-chip ADC (in case your micro has one).

PWM period: 100us; Filter time constant: 1ms
Figure 9 – PWM period: 100us; Filter time constant: 1ms

You may use this ADC to monitor the output voltage and make the necessary adjustment in the duty cycle. This only works if the micro has an internal band gap reference for the ADC module, otherwise, the ADC itself would be affected by the variation of the power supply, thus not being useful for the intended purpose.
So in order to summarize the characteristics of this solution, a short parallel to the R2R method described in the first part of this article is appropriate:
a) the PWM solution is even cheaper in its basic form, resolution independent of the number of pins used (however, dependent on the duration of the frequency of the micro and period of the PWM)
b) The PWM solution is very tolerant to the deviation in the external components used (the RC filter can have 5% tolerance resistor and even +/-20% tolerance capacitor)
c) The frequency of the output signal is not dependent on the resolution, but it is mainly (and critically) dependent upon the period of the PWM signal. The PWM solution should be mainly used for low varying signals or continuous (yet software adjustable) analogue signals.
In case further reading is required on RC or low-pass filters, the web offers a myriad of resources (these are just top google results):
http://en.wikipedia.org/wiki/RC_circuit
http://en.wikipedia.org/wiki/Low-pass_filter
http://www.muzique.com/schem/filter.htm

PWM - RC Filter

I found the following application note quite interesting :
http://ww1.microchip.com/downloads/en/AppNotes/00538c.pdf

Who's online

There are currently 0 users and 19 guests online.

Recent comments