John L Errington MSc

Generating voltages with the Arduino microcontroller.

PWM as Digital to Analog Converter (DAC)

We saw on the previous page that we can use PWM to provide analog control for an LED or motor. Now lets look at this in more detail, with the aim of generating dc voltages using PWM.

The need for filtering

Simple RC Filter

Frequency response

Step response

 

 

Pulse width modulation: Filtering

A PWM output is not a stable voltage level - its a pulse train. We can look at this as a dc level with a superimposed ac signal.

pwm componentsSo a PWM output with a duty cycle* (see below) of 50% and frequency of 1000Hz coming from the digital output of a 5V arduino as shown here:

has two components:

a dc value corresponding to Vcc * duty cycle;

in this case Vav = Vcc * 50% = 2.5V;

AND

an unwanted ac square wave signal of amplitude Vcc * 50% = 2.5V

which has frequency components at 1kHz, 2kHz etc.

We can remove the AC component (with some reservations) by filtering:
A LOW PASS FILTER will reduce the size of the AC component, but WILL ALSO limit the rate at which the "DC" value can change.

The frequency of the PWM signal on most pins on the UNO and similar boards is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz.

Duty Cycle: (often expressed as a percentage)
The amount of time in one period of the wave that the output is HIGH;
For PWM at 490Hz, the period is 1000/490 = 2.04 milliseconds.

if the wave is high for 0.2 milliseconds the duty cycle is 0.2/2.04 = 0.098 or 9.8%

 

Simple RC filter

rc filterOne way to reduce the size of the unwanted ac signal is to use a simple "low pass" RC filter like this.

The "time constant" of the circuit is just T = R * C

The "center frequency" Fc of the filter is Fc = 1 / ( 2 * pi() * R * C )

so if R = 100k ohm and C = 1uF

the "time constant" is 100,000 microseconds - or 0.1 second;

and the center frequency Fc is 1.59 Hz.

What does that mean?

 

Frequency response of RC low pass filter

rcfilter

A change by a factor of 10 in frequency is called a "decade".

Low freqencies a decade below the "center frequency" are largely unaffected by the filter;

Frequencies a decade above the center frequency Fc are reduced by a factor of 10. Higher frequencies are attenuated even more; for each decade they are attenuated by another factor of 10.

Note in these graphs the high frequencies are at the left of the graph

 

rcfilter

To use PWM effectively there needs to be good separation in frequency between the pwm frequency and the highest frequency you wish to generate.

So if your PWM is at 500Hz (Uno pin 9) and you wish to get rid of the pwm frequency signal

you would need to set the center frequency at about 1 Hz.

 

However (Next) that REALLY limits the rate at which the output from the filter can change.

 

Step response of RC low pass filter

step

A sudden change in the average voltage of the pwm is called a "step".

Here (blue) you see a change from a value of 0 to 1 in the average of the pwm signal.

For example we could write
// set the pwm duty ccle to 0, giving an average voltage of 0
analogWrite(ledPin, 0);
delay(100000); // let it settle for 100 seconds
analogWrite(ledPin, 51); //set the pwm average voltage to 1V

 

The filter output will not change immediately; it will take 5 times the time constant to reach the new value.

So for your RC filter, with R = 100k ohm and C = 1uF and a time constant of 0.1 second, the full change will not be complete for half a second.

 

 

Using PWM to generate a DC voltage

A PWM frequency of only 500 - 1000 per second imposes a major limitation on the rate at which the filtered output can change. We call that the "SLEW RATE".

We can improve on this by

  • increasing the PWM rate, and
  • using a better filter

as shown on the

NEXT PAGE: change the PWM rate and use a better filter