# RC filter

If we want to smooth unwanted noises or annoying fluctuations of an electric signal we read from an analog sensor we have to create a filter.

Now we want to consider different methods to create a filter, both software and hardware, and to do this we first need to prepare a simple circuit to use to test them.

We will use a photoresistor (or light-dependent resistor if you prefer) as the main element for our tests. By the way we’ve found this tutorial by Adafruit very interesting: it contains a lot of information about this electric component, definitely worth reading!

Our siple circuit is composed of a:

• photoresistor;
• $1 K\Omega$ resistor, used in pullup configuration.

Let’s start reading and plotting the voltage across the photoresistor. Here’s the code we are using:

We use the Serial monitor to print the values from the analog input, and the Serial plotter tool to represent them in a more intuitive way.

As we see from the graph above, when we change the light headings towards the photoresistor, the graph registers big variations in ADC values: greater the quantity of light, the lower the resistance value, the same the voltage measured on analog pin 0.

On the other hand, when we let everything untouched, even if the readings from the photoresistor seems to be quite stable, they present a lot of noise.

From the serial monitor we read continuosly changing values (even if they only change a bit), the same we see from the serial plotter graph.

Even though this behaviour seems to be different according to the type of light, we would like to filter out this noise in some way.

## The software way

Let’s try first to use software to smooth our analog readings. The first method we will see is the array method.

### The array method

As we learn from Arduino Smoothing tutorial, we can use an array to store consecutive readings and calculate their mean. This way we will always have an output that will be smoother than the input.

Here’s the code we are using (the same shown in the tutorial):

Below two screenshots for both a light changing and stable environment. As we see noise now seems to be more controlled.

Maybe changing the size of the array could improve the filter power but at the same time, as we read from the tutorial introduction, it will create a delay

The higher the number of samples to keep track of, the more the readings will be smoothed, but the slower the output will respond to the input.

### The bit shift method

Another way could be that of usign a bit shift. In other words using this method we are reducing the bit depth of the ADC signal, loosing some of the least significant bits of the original 10 bit sampled signal.

This is a very rough method that is also responsible of reducing the dynamic range of the sampled signal because of a reduction of the number of quatization levels.

Here’s the code:

And here the plotter images:

The scale of the graph has been reduced; due to the bit shift operation the maximum value that the value variable could assume is lower than 1023 .

We can also test that the more bit we shift the readings to the left, the more squarewav-ish the graph will appear.

However if we take a look at the image above, it seems that the noise has been significantly reduced: this is because our graph now has a much lower resolution than before.

Because our noise is only responsible for little voltage variations, here we are no more able to see them.

### The 1-sample delay method

Recently I’ve read about this method on Andy Farnell’s Designing Sound book. There’s a moment in chapter 10 when he describes this simple PureData patch:

This patch is a simple low pass filter that follows this filter equation:

The strenght of the filter is set by the ratio $A : B$. Both $A$ and $B$ should be between $0.0$ and $1.0$ and add up to $1.0$.

Let’s try this method in code with Arduino:

Here we have $A = 0.1$ and $B = 0.9$, so the filter ratio is $\frac{A}{B} = 0.\overline{1}$ . Here the images from the serial plot for both a light changing environment and a static light one.

We also tried with different values for $A$ and $B$: here $A = 0.01$ and $B = 0.99$, for a filter ratio of $\frac{A}{B} = 0.\overline{01}$

In both cases as we see, noise seems to be little, and we also note that the smaller $A$ is, more this code seems to graph a signal similar to a capacitor charge and discarge cycle.

The second one is much slower than the first one, meaning that a smaller $A$ and a smaller filter ratio creates a more powerful low pass filter behaviour.

If you want to go deeper into the theory behind this patch, please refer to this post!

## The hardware way

But what about doing a filter using hardware components instead of code? We have to realize a simple circuit variation, adding the so called RC filter:

The theory tells us that if we accurately calibrate the value of R and C, we can precisely define $f_{c}$, the cut frequency (where $f_{c} = 2\pi\omega_{c}$) as:

In this way, all variations on the input signal will be attenuated according to their frequency. Voltage variations with a frequency $f > f_{c}$ will be attenuated and will not be transferred to the output signal. On the other hand, signals with a frequency lower than $f_{c}$ will be left untouched.

Let’s say we want to cancel all variations faster that $1Hz$, so we have to calculate the values for $R$ and $C$ accordingly.

For example, if we use $R_{f} = 1.5K \Omega$ and $C_{f} = 100 \mu F$, we will obtain approximately an $f_{c} = 1Hz$, as we wanted. Here’s our new circuit:

And here’s the Fritzing representation:

We highly reccomend you to read the chapter 2 of the “Practical Elctronics for inventors” by Paul Sherz if you want more information about filters.

Here’s the Arduino Serial plotter images for great variations:

and for no variation at all:

Despite our hopes, the noise is still present, as you can see in the last image. Even if this circuit would have completely cancelled the noise it actually seems to add more of it.