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

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

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.

1. Introduction

In the first part of this article (Generating Analog Voltage with Digital Circuit (I) ), we 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 (figure 1).
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 in the figure 2 and 3 illustrate the case in which the duty cycle is 50% and 10% respectively. The green represents the PWM signal, while the red represents the continuous (analogue) signal obtained at the output of the filter.

 

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.

2. Features of design

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 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 PWM frequency is 10 kHz (which means 100us period, figure 4), you should choose a time constant of about 10ms – easily obtainable with R=1k and C=10uF (easy to get values). In case the time constant decreases 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.

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 diagram of figure 5, 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%.

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. For that, it is possible to use some additional external components, like for instance a fast and high current transistor (Figure 6).

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 (figure 7).

For More Details: How-to use PWM to Generate Analog (or Analogue) Voltage in Digital Circuits

 


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top