# Analog output via PWM and low pass filter

In this previous post we saw how to use PWM outputs to simulate an analog voltage output.

However, we made it clear that a PWM output is not an analog voltage signal, but a pulsed digital signal whose average value is the desired value. Not knowing this can cause unexpected behavior in the components we connect to the PWM signal, or even damage them.

To serve as an analogy, for a component to expect an analog voltage signal and receive a PWM is similar to asking for a gentle head massage and getting a hammer blow every 20 seconds. The average will be the same but… It’s not the same.

There are several mechanisms that we can use to improve the response of our PWM, so that it better approximates an authentic analog signal. One of the simplest is to use a low pass filter to “smooth” the signal.

In this post we will see how to incorporate a low pass filter to a PWM output to improve its behavior and make the obtained output better approximate an authentic analog signal.

## Incorporating a low pass filter

A PWM output is a mechanism frequently incorporated by automation systems to emulate an analog signal. It consists of providing a pulsed signal at a certain frequency, whose average value is the desired analog value. But the applied voltage value is actually, at all times, a digital signal between -Vcc and +Vcc.

For example, if with a voltage Vcc of 5V we want a PWM signal of 1V, a signal will be generated that will be 5V 20% of the time and 0V the remaining 80%.

If the system’s response is slow compared to the PWM frequency, the PWM output may be sufficient. However, in other cases it will not be, and we may even damage the powered component if the Vcc voltage is higher than the maximum voltage permissible by the component.

To improve the response of the analog output we have several options, and the simplest is to incorporate a passive low pass filter using an RC network.

A low pass filter is a component that eliminates the high frequencies of a signal, allowing the low frequencies to pass.

The electrical schematic is as follows, where we see that we have superimposed the RC filter network between the PWM output and the filtered signal, which is the one received by the load.

As we had anticipated, the system’s response is a damped signal of the PWM signal, but it still is not a perfect analog signal. An example of the response is the following:

We must note that the produced signal has two relevant parameters.

• Response time: The time it takes for the signal to reach the steady state (a percentage of the desired signal)
• Ripple: The small oscillation that remains after the filter.

These two effects are “opposite” to each other. That is to say:

• If we try to reduce the ripple, we will need very long response times.
• If we try to reduce the response time, we must accept a greater ripple.

Therefore, there is no single optimal filter and signal. We will have to make a design decision and accept a compromise between a certain ripple value and a response time.

## Adjusting R and C in the low pass filter

The response characteristics will depend on:

• The PWM frequency
• The voltages of the filtered signal and the desired signal
• The chosen values of R and C

To calculate these parameters, and visually display the obtained response, we can use the RC network calculator available at this link.

## Limitations of a passive filter

The proposed low pass filter is useful for providing an almost analog voltage value, and can even power small loads. However, it is not suitable for powering large loads.

Loads with higher power have lower impedances. When this impedance is lower than that of the RC network, the load consumption causes the obtained signal to deviate from the theoretical.

A simple way to avoid this is to use an operational amplifier to create a voltage follower, which isolates the influence of the load from the operation of the RC network.

However, since we are going to use an operational amplifier, we could unify both components by using an active filter.

Finally, another highly recommended option is to directly use one of the many commercial ADC converters available. We will see these solutions in future posts.