I have an optical slot sensor running on 5v as shown in the attached image. This plugs into another board where I have a 10K pull-up on the ENCODER_SIGNAL line that also plugs into my Atmega328P.
The issue is that the encoder signal is reading ~3.7v blocked and ~1.8v when open. It's not transitioning sharply 5v -> 0v. Is this optical slot sensor set up wrong? I had an old project where I set it differently by using a 4.7K pull down resistor on the Emitter instead of the collector pull-up I do now (old project shown in the 2nd attached image). I don't have this hardware with me to test/compare results, but wondering if this is the better way? Or is there a 3rd better option I'm not aware of?
Also worth noting that in my first screenshot, I have a 200R resistor on the other board connected between 5V to ENCODER_LED (Anode) but in the 2nd screenshot (older project) I have a 200R resistor on the Cathode side.
Please help me understand what type of setup is better to ensure a sharp transition in signal.
I should correct myself, I just found my old project and I did a 4.7k pull up in same location (Collector) instead of the new project where I'm having issues with a 10K pull up and had the signal come out in same spot, so ignore the 2nd image in the first post.
I also checked the voltage and the old project reads much clearer shifting from almost 0v to 4.9v. The only difference is I am using a different phototransistor now. I'm now using GP1S094HCZ0F | Datasheet | SHARP | LCSC Electronics
I seem to have found the issue I think. I switched the optical slot sensor, but kept the LED resistor at 200R. But it seems this new optical slot sensor needs a lower resistor value for the LED according to Google's Gemini. And my 10K pull-up is probably not helping either because it can't drive it low enough when unblocked. Here is what Gemini had to say:
--> I'm using this optical slot sensor, is there a recommended value for the LED and signal pull up? https://lcsc.com/datasheet/lcsc_datasheet_2410121933_SHARP-GP1S094HCZ0F_C920601.pdf
Yes, based on that datasheet, here are the recommended values and the reasons your circuit is not behaving as expected.
Recommended Resistor Values
LED Resistor (Input): 180Ω
Pull-up Resistor (Output): 4.7kΩ
Why Your Circuit is Failing (According to the Datasheet)
The datasheet reveals that this specific sensor is not very sensitive. The key parameter is the Collector Current (IC), which is listed with a minimum of only 40µA (0.04mA) under the test condition of VCE=5V and IF=5mA. This is an extremely low output current.
Your 10kΩ pull-up resistor is too strong for such a weak output signal. The phototransistor simply cannot sink enough current to pull the 10kΩ resistor's voltage down to zero, which is why your "LOW" signal is getting stuck at 1.8V.
Calculations Based on the Datasheet
1. LED Resistor (Input)
The goal is to drive the LED with a healthy amount of current to maximize the output. The datasheet lists a typical Forward Voltage (VF) of 1.2V. Let's aim for a Forward Current (IF) of 20mA, which is well within the 50mA maximum.
Formula: R = (V_supply - V_forward) / I_forward
Calculation: (5V - 1.2V) / 0.020A = 190Ω
Recommendation: The closest standard resistor value is 180Ω. A 200Ω resistor is also acceptable but provides slightly less drive.
2. Pull-up Resistor (Output)
We need a resistor that is weak enough for the feeble 40µA output current to pull down to near zero.
When the transistor is OFF, it has a Collector Dark Current (ICEO) of up to 100nA (0.1µA). The voltage drop across the pull-up will be negligible, giving a clean HIGH signal.
When the transistor is ON, the datasheet says it can sink at least 40µA. Let's see what voltage that produces with your 10kΩ pull-up.
Voltage at Collector: 5V - (I_collector * R_pullup)
Calculation: 5V - (0.000040A * 10000Ω) = 5V - 0.4V = 4.6V
As you can see, even with the minimum guaranteed current, the output voltage would be an unacceptable 4.6V. This confirms the 10kΩ resistor is too strong.
Let's recalculate with a standard 4.7kΩ pull-up resistor:
Calculation: 5V - (0.000040A * 4700Ω) = 5V - 0.188V = 4.8V
This is still not ideal. However, the 40µA is a minimum value at a low 5mA LED current. By driving the LED with 20mA (using the 180Ω resistor), you will get a much higher output current, allowing the 4.7kΩ pull-up to be pulled down effectively to create a proper LOW signal.
--> what's the lowest resistor value I should put on the LED?
The absolute lowest resistor value you should use is 76Ω.
How We Know
The datasheet for the GP1S094HCZ0F lists the Absolute Maximum Rating for the forward current (IF) as 50mA (0.050A). The calculation is based on ensuring the current never exceeds this limit.
Formula: R = (V_supply - V_forward) / I_forward_max
Calculation: (5V - 1.2V) / 0.050A = 76Ω
Practical Recommendation
Running a component at its absolute maximum limit for extended periods can reduce its lifespan.
For a balance of strong performance and long-term reliability, the lowest practical value to use would be the next standard resistor size up, such as 82Ω or, more commonly, 100Ω.
Adding a voltage comparator is an extra component and would require a re-spin of my board (not a big deal, but want to ensure I exhaust all options first).
Why is it that the RPI-243C1 works fine with readings at 200mV and 4.9V but the GP1S094HCZ0F (the one I currently use) does not? I just changed the LED resistor to 100R and the Pull-up to 4.7K and I get 4.3V when blocked and 2.4v when unblocked. Don't both photo interruptors work the same/have same readouts?
Also can you elaborate on the Hysteresis @LarryD ? I'm not sure I understand. Are you suggesting my optical sensor has a noisy output? If so, why would it?
You have experienced it every time you adjust a central heating thermostat or used an electric oven. You get an on click and an off click at different settings, of your control knob.
I read the data sheet section on the transfer function.
Estimating a typical Ic = 200 uA x 50K = 10V, which means the phototransistor should be saturated, as desired. Assuming the minimum Ic = 40 uA x 50K = 2V, so 100K would be more appropriate.
There is probably something very wrong with your wiring, but that is only a guess, because you forgot to post a complete schematic and a photo of the setup.
@jremington So I unplugged the encoder from the motherboard and checked the pin on the connector of the motherboard. It is reading 4.98V when pulled to 5V with 50K resistor. So it seems the resistor is fine. But when I plug in the encoder and read the voltage on the encoder output pin, it's never over 200mV.
I put back the 4.7K pull-up instead to make sure my setup is still good and it's back to reading the 2.4-4.7V range. So I'm not sure what's going on. I tried two different encoders of the same type too.
That device has very low CTR, only guarantees 40uA output current for 5mA LED current, which is a CTR of 0.8%. You'd need a much larger load resistor than 4k7 for it to work, perhaps 100k. I always try to find opto-devices with a decent CTR (if a photo-transistor device). And also the GP1S094HCZ0F is very slow, even for an opto-sensor.
The Rohm device is much better, although not particularly good for CTR, 2.5%
I need a package that is as wide as this slot sensor though at 5.5mm like shown on the data sheet. If you have a suggestion for one that is this wide or less, I'm all for it. Why do you say this is slow? I plan to use it as an encoder. I would imagine it'd be fast enough?
You could feed the output of the optocoupler through a capacitor, removes DC component; ends up an AC pulse.
Feed the output of the capacitor to a voltage comparator, adjust trigger level for comparator output of 0V to 5v; feed to GPIO input.