# Where to place a capacitor to smoothen IR sensor reading?

I know this question has already been asked, but the answer I'm looking for here is different.

I found out about this forum post explaining the best way to smooth out the reading of a IR sharp sensor (it seems really great based on facts and measurements).

The thing is I don't know much in electronics, I'm wondering where should I put the capacitor and resistor described in this post: 10uf ceramic capacitor and 530 ohm resistor parallel to the capacitor.

"Where should I put it" means where do I connect it. Thanks

-
@endolith this: sparkfun.com/products/242. There are 3 pins: VCC, Ground and signal output. –  Matthieu Napoli Aug 3 '11 at 20:02

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:

$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.

-
Wow great answer! I have a 0.1uf ceramic cap, so I'm right I need a 16kOhms for R in order to get a cutoff frequency of 100 Hz (for a IR distance sensor for a robot, it seems about right?). Am I correct? –  Matthieu Napoli Aug 4 '11 at 6:36
@Matthieu - Glad you like it. Your values are right for 100Hz (though 100Hz are probably more than you need for a robot). Also the values aren't very critical; 15k is a more common value for a resistor than 16k. –  stevenvh Aug 4 '11 at 6:41
It worked really good! I used 22K for the resistor, which is around 70 Hz. Thanks –  Matthieu Napoli Aug 6 '11 at 13:12

Make sure you are using a ceramic cap, such that it isn't polarized. Attach the sides of the capacitor and the sides of the resistor together. Attach one side of this resistor/capacitor combination to the signal output on the sensor. attach other side of resistor/capacitor combination to whatever you want to use to get the data.

Like This

EDIT: I think I understand what you are asking now. I believe you are unsure which point is the Vo (data out) line? My suggestion would be to just buy this connector for \$1.50. You can then just solder/breadboard connect the resistor/cap in parallel I explained above to the yellow line. Otherwise you have to solder it to the back of the sensor.

-
Thanks (I have the connector though, so I know the yellow one is the signal). Though I don't understand what you mean by "whatever you want to use to get the data". I connect the signal output of the sensor to an Arduino pin. So with 1 side of the resistor/capacitor "parallel combo" connected to the yellow wire (signal), where do I connect the other side? :p thanks (and I have a ceramic cap). –  Matthieu Napoli Aug 3 '11 at 20:54

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.

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?

-
It seems great! The link to my sensor is already as a comment to my question ;), it is indeed in the list. I actually only have a 0.1uf ceramic cap, but I have loads of resistors so that should be good! –  Matthieu Napoli Aug 4 '11 at 6:31
Tell us the exact part number of the sensor you are using. Having people have to dig through comments by other people is not ideal and may produce errors. There are two versions of that sensor (A & D versions) - which one is yours. What is your circuit? Are you using a pullup resistor on the output? (as per data sheet in one case). What are you driving with this ? (as already asked). Using a 15K resistor may affect your accuracy depending on what you are driving (as I noted ). And it may result in much reduced output, depending on other factors. –  Russell McMahon Aug 4 '11 at 7:03
If you just take the answers you "like" and ignore other technical input (as you are doing) and don't provide more details, there is a reasonable chance that the results will not be what you expect. You need to try to answer all questions from people who seem to be technically competent. Sometimes doing so produces quite unexpected bonuses - or a quite different answer. –  Russell McMahon Aug 4 '11 at 7:06
Russell, are you being jalous that I said "wow great answer" on another answer? Yours is great too, and I'm not neglecting it, I was just working today. Now here are the answers you wanted: the exact part I'm using is (as I said in the only comment I made) GP2Y0A21YK (so version A). My circuit right now is that I connected the signal output to an Arduino analog pin. So to answer the next question: no pull up resistor. With this, I'm driving a robot: i.e. when an obstacle is detected at a threshold distance, I stop the robot to avoid collision. –  Matthieu Napoli Aug 4 '11 at 15:24
So I'm not looking especially for distance measurement here, but rather having a critical distance to which to stop. What I noticed, and you may find it interesting, is that the signal is quite correct when using just the sensor. But when I start the vacuum cleaner motor on the robot (yes the robot is a vacuum cleaner), I get a lot of noise on the sensor. FYI the Arduino takes its power from USB, but the motor battery and USB ground are connected. Maybe this is the source of the problem? Anyway, thanks a lot for the time you take for me, I appreciate it. –  Matthieu Napoli Aug 4 '11 at 15:29