Photo Interrupter Problems

The best method for reading the signal would depend on your application and some specific details like:
What's the maximum rate the posts pass through the slot?
Are all the posts positioned the same way, same color, is it reflective, translucent, same size?
Can the posts completely block the LED and photo transistor?

Note there will be a time interval where the posts gradually block or unblock the LED. This is where the signal would be most susceptible to noise as it gradually transitions from low to high or high to low.

You could get a picture (capture) of what the signal looks like as the posts pass through the slot. Connect the signal to A0 and run this code. Open Serial Plotter. To stop the trace, just select an invalid baud rate. What does the signal look like?

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A0));
}

Try at higher baud rates to get more samples, faster plotting and higher resolution.

Hi,
Spec sheet says ON current for the phototransistor is typically 2.5mA

So RL
5V/2.5mA= 5/ 0.0025 = 2000 Ohms, 2K you are using 56K.

So your ON current is
5V/56000 Ohms = 0.000058 A = 0.058mA, or 58uA that is nowhere near enough by the spec sheet, no wonder you have noise.

Replace the 56K with 4K7 or 2K2 Ohms resistor.

Tom.... :slight_smile:

TomGeorge:
Replace the 56K with 4K7 or 2K2 Ohms resistor.

I have a 10k resistor on my QRE1113 optical sensor.

...R

OK, moving on from my earlier OMG post.
FFS! We are talking about what is effectively a switch on an input pin.
What's so hard?

Yes, you could use an analog input if you (really) need to, but in 99% of opto-interrupter applications, a digital (0/1) input state will be a far better approach.

There are optical conditions that need to met - the optical flag must completely occlude the light oath for reliable sensing. If you are sensing partial occlusion, the type of input is the least of your problems. Power supply stability will cause greater problems.

RANT:
Design your solution - if only in your mind - before stapling, twisting and taping wires and gizmos together.
Every complex project is a collection of simpler solutions.
You're stuck on the digital equivalent of a switch, bulb and battery.
/RANT
We've all offered various levels of help, you really need someone sitting beside you to demonstrate and explain what you're seeing.

Sorry for being harsh but this is part of learning.

Robin2:
I have a 10k resistor on my QRE1113 optical sensor.

...R

QRE1113 typical ON current = 0.4mA
5/10,000 = 0.5mA
The Ops device typical ON current = 2.5mA
5/2200 = 2.2mA
Tom.... :slight_smile:

Referring to this part of the specifications:

[color=teal]Item                   Symbol   Conditions       Min  Typ  Max  Unit
Output  Light Current  IL       VCE=5V,IF=20mA   1    2.5       mA[/color]

This tells me that for 20mA through the LED, the minimum Current Transfer Ratio you can expect is 5% (1mA), the typical CTR would be 12.5% (2.5mA). The collector can control currents all the way down to the "Collector Dark Current", which is 100 nA maximum (0.1µA).

So, with 56K || 30K (INPUT_PULLUP), R2 = 19.5K
Minimum current through R2 is 0.1µA (worst case, transistor off) and maximum is about 2.5µA when the transistor is on. Should be no problem with the signal levels (just noise, rise time, fall time).

@SChiccarine, have you tried adding a 0.1µF capacitor (R2 to GND)?