Phipps Electronics

Order within the next 

FREE SHIPPING OVER $199

50,000+ ORDERS

WORLDWIDE SHIPPING

SSL SECURED

Using a PWM Module as a DAC

Contents

Don’t have a DAC module on your microcontroller but instead have a PWM? Want to know how to use a PWM module as a DAC? Read the rest of the article to find out.

Introduction

A DAC (Digital-to-Analog) module can have many uses for your projects. You can use it as a control variable for various analog functions. Say, for example, controlling the brightness of a flicker-free LED or as an input to a Voltage-to-Frequency Converter. Previously, a blog about a Wireless Voltage Source used a DAC to output its values.

Investigating the PWM Output

When you average out a PWM signal, it has a corresponding DC value. This DC value is directly proportional to the duty cycle that you used in your PWM. For example, below is a 50% duty cycle PWM with a peak voltage of 5V. Effectively, the equivalent average DC voltage is half the peak value which is 2.5V. Similarly, a PWM with a 75% Duty cycle has an equivalent average DC voltage  of 3.75V 

Using a Low Pass Filter to get the Average

A Low Pass Filter can be used at the output of a PWM so that fast-changing variations of the waveform can be filtered or attenuated. What should ideally be left are low-frequency components.  Hopefully, you’ll end up with an almost DC value.

An important parameter of this low-pass filter is its cut-off frequency, Fc. This cut-off frequency will determine the starting point at which frequency (and all frequencies above it, hence its name, low pass) to start attenuating or filtering signals. A Bode Plot or a Frequency Response curve can illustrate the working of a low-pass filter.

An Arduino UNO outputs different kinds of PWM frequencies on its Digital PWM pins. The table below gives some examples of these PWM frequencies as well as in other Arduino boards.

As an example, we can choose pin 5 of the UNO to get a PWM waveform with a constant frequency of 980 Hz. The Vpeak of this waveform is about 4.6V. The time per division is set at 1ms per division. Without the filter circuit, the PWM waveform is seen below.

The operating frequency of your PWM gives you a rough estimate of the starting cut-off frequency you need to have an effective PWM to DAC converter. On the example circuit above, we can start with Fc as:

Fc = 1/ [(2 pi) * 1k * 1uF]

Fc = 159 Hz

Hence, here, anything above 159 Hz will be attenuated to some degree.

Above, we see attenuation at the peak values of the PWM waveform. The average value of the waveform is about half the previous peak value, now at 2.3V, just right for a 50% Duty Cycle. However, a large ripple is unacceptable as a DAC value.

Above, we change the filter capacitor value and increase it to 10uF that effectively decrease the value of Fc to 15.91Hz. The resulting waveform is seen, and we get more attenuation for the filtered signal. However, this is still not acceptable.

Finally, we further increase the capacitor to lessen the ripple voltage. We now place a 100uF value instead, resulting in a lower cut-off frequency of 1.59 Hz. The result is a cleaner output enough as a DAC value.

The Bode Plot or a Frequency Response Curve may help you shorten the time to pick the correct resistor-capacitor value for your low-pass filter. You can also use MatLab or other simulation software to automate your results.

Operating your PWM-to-DAC circuit

Below is a code to operate your PWM-to-DAC circuit, as well as a video demonstration.

				
					void setup() {
  // put your setup code here, to run once:
  analogWrite(5, 255);
}

void loop() {
  // put your main code here, to run repeatedly:
  byte i;
  for(i=0;i<255;i++)
  {
    analogWrite(5, i);
    delay(50);
  }
   
}

				
			

SUBSCRIBE FOR NEW POST ALERTS

Subscribe to be the first to know when we publish a new article!
List Subscriptions(Required)

POPULAR POSTS

Scroll to Top