# Simulating a transmissive photo interrupter sensor with Arduino

First of all hello. I find myself in trouble while trying to simulate the behaviour of a photo interrupter sensor by using Arduino Uno R3. The idea is to get rid of the sensor and use Arduino to send the signals instead, in order to be able to control over device by my own demand.

According to the datasheet - the sensor is powered by 3.3V and has the following outputs. HIGH - if voltage is >2.4V LOW - if voltage is < 0.4V

I have connected my Arduino's PWM pin10 to sensor's output pin and GND to GND and wrote a simple test code that does the state change inside a loop like this:

analogWrite(PinNr, 170);// send High signal 170 = 3.3v PWM output
delay(120); // wait 120ms
analogWrite(PinNr,0); // send Low signal
delay(120); // wait 120ms


Problem is that it does not work this way.

If I connect a simple 3V battery to sensor's output Pin and GND to GND through a simple push button and quickly push and release the button this method works perfect. I need to be able to do this by using Arduino and not by hand so I think the problem relates to the type of signal (Arduino PWM vs analog).

I looked into a way of converting the Arduino PWM signal to analog by using a Low pass filter, yet I have trouble understanding what is the cufoff frequency and the response time of the RC filter that I should take into consideration. For example I know Arduino PWM pin 10 runs at a 500hz frequency and I would like that the filter should respond to the state change HIGH- delay(120)-LOW-delay(120) as fast as possible. Sensor datasheet specifies that any voltage > 2.4v will be interpreted as a HIGH signal and any voltage < 0.4v will be interpreted as a LOW voltage, so I think that ripple can have a greater marje.

Basically, you need to pick a filter with a cutoff frequency lower than your source frequency in order to get the PWM to look like DC. To be clear, no first-order filter (or any filter) will turn a signal into pure DC (there will always be a noise component), but you can get close enough that your receiving circuitry won't care.

In order to calculate cutoff frequency, we utilize this formula, which is for first-order RC filters:

$f_c = \dfrac{1}{2\pi R C}$

Now we must use a little bit of engineers' intuition to figure out what would be an appropriate cutoff frequency. I would say start at a cutoff frequency of 10hz and go from there. Through a simple re-arrangement of the formula, we can calculate $R$ by selecting $f_c$ and $C$.

$R = \dfrac{1}{2\pi f_c C}$

The reason I chose to "select" the value of the capacitor is because usually it's a lot easier to find an appropriate resistor (or resistor network) than it is to find an odd sized capacitor. So if you have a bunch of capacitors laying around, pick one and go from there. If your resistance is really wacky (like really high or really low), pick a new C and try again. For this situation, you could get away with using a 10uF cap and a 1.5K resistor.

If you find that there is too much ripple, increase the value of your capacitor. If you find that you need faster response to changing PWM frequencies, lower the value of your capacitor. You can accomplish similar effects by changing the value of R, but that will limit your drive capability on that pin as R gets higher and higher. Additionally, if the value of R approaches the input impedance of the load you are driving, it will begin to act like a voltage divider which is not desirable.

There are also many filter calculators on the internet that can help you design this thing. Usually for this type of situation you do not need to apply a whole lot of engineering rigor; if you can estimate what you need, and that works for you, then there really isn't a need to do any deeper analysis.

• Thank you very much for you fast response, I will try the suggested values of 10uF capacitor and 1.5K resistor to see how it goes. Will post my answer here. – n00b007 Feb 29 '16 at 14:54
• Thank you for 1000 times and God bless you, this worked perfect for me. I'm really greatfull to you for helping me. I red all sort of information related to low pass filter yet I did not understood how to pick the cutoff frequency. Lot's of information related to audio signals so I got even more confused. I'm still intrigued by how you determined and picked that 10HZ :) so any further explanation is always welcomed and appreciated, as I have struggled the last few days in order to understand how this PWM to analog really works and how to achieve my goal. – n00b007 Feb 29 '16 at 16:07
• I basically picked 10Hz based on the fact that 10Hz is a) at least an order of magnitude lower than the frequency of the PWM and b) pretty close to DC without going so low that the transient response to a duty cycle change would be very slow. That is to say, if you quickly change duty cycle, it takes a long time for the "DC" output of the filter to change as well. – Brendan Simpson Feb 29 '16 at 18:49