Where to place a capacitor to smoothen IR sensor reading?

What you want is a low-pass filter. A low-pass filter will let slow changes in your signal pass, but block faster changing signals. The basic low-pass filter is this RC-filter:

RC-filter

\$V_{IN}\$ is the signal from the sensor. \$V_C\$ is the capacitor's voltage, which you could use to go to the ADC (Analog-to-Digital Converter) of a microcontroller, for example.
The capacitor acts as a voltage reservoir, which is filled or emptied via the resistor. The higher the capacitor's value the longer this will take. A higher resistor's value will cause less current to flow to and from the capacitor and also will deny fast changes.
Now you may think that the higher the values, the better smoothing you get, and that's true, but if you go too high you'll also prevent the normal changes due to the change in distance from the sensor. And those changes are allowed.
The key to finding the right values is the cutoff frequency, that is the frequency starting from which the filter will block a signal. For the RC filter:

\$f_C = \dfrac{1}{2 \pi \times R C}\$

Say you want to allow changes of 1Hz, that means changes which take place in 1 second. Then according to the formula

\$R C = \dfrac{1}{2 \pi \times f_C} = \dfrac{1}{2 \pi \times 1} = 0.16\$

If you choose a value of 1\$\mu\$F for the capacitor you'll need 160k\$\Omega\$ for the resistor.


I added the diagram to C.Zach Martin's answer. I didn't edit his answer as it's disconcerting when the whole sense of an answer is changed by an edit.

  • My understanding of the original post referred to is that they intended the signal to connect to the top of the circuit and for ground to connect to the bottom

  • Which is not what CZR has recommended his answer suggests a series connection - sensor signal in at top, output to eg Arduino at bottom.

  • And I suggest that both are not optimum.

What you want is a "low pass filter" that removes the high frequency variation and noise. Like this - the capacitor is charged and discharged via the resistor. Slower varying signals do this more effectively, faster varying signals have less effect on the capacitor's voltage.

enter image description here

Note that how well this will work depends very very very greatly on the input source and the load. If you provide a sensor part number, or better still a link to a data sheet as well, and specific Arduino connection information (or whatever the load / output is) then we can provide a better answer.

Having R as 1k ohm and C as 100 uF will give a time constant of 1 millisecond. That may not be good enough. Larger capacitors will produce a lower frequency filter. Bigger or much bigger resistor values (10k, 100k) may be usable - but may not, depending on the sensor and load. Help us help you!.

Datasheet - analog sensor GP2Y0A21YK

Datasheet - digital sensor GP2Y0D21YK

Analog & digital in one datasheet


This sensor was referenced by Endolith and may (or may not) be what you are using. If it is then R in the circuit I showed above can't be much more than 1K if the output is (as it appears to be) "open collector". Just adding a capacitor from output to ground would have some effect but the assymetrically driven output could give problems. SO tell us what you are really wanting to use - sensor and load, and we can refine the answer.


Your sensor MAY be one of these. Yes?

enter image description here