Showing results for 
Search instead for 
Did you mean: 

An optimal input circuit for an optical sensor?

Senior III

Here's what I have:

The opto-coupler simulates (very roughly) the TCST2103 optical sensor.

My goal is to accurately measure the pulse frequency. The Q MOSFET's gate represents the MCU GPIO input. The 1M resistor represents the input resistance.

I wonder if 2k2 pull-up resistor isn't a bit too low. It makes a voltage divider with the input resistance, and the input resistance is AFAIK "undefined" / very high. Theoretically - it shouldn't matter. Practically - if we left the MOSFET input without a very weak pull-down, it could dwell in high state in case of the input pin floating. Also, I'm not sure if the higher this pull-down resistance is - the more the internal capacitance of the input matters, creating a low-pass filter. Finally, I'm not sure what really lives in the STM32 chip. Bare MOSFET's gate, or is there a high value resistor?

Guessing like 100k input resistance 2k2 pull-up provides negligible voltage drop. Are there any disadvantages of it beside the power consumption? I've seen similar input circuit with 10k pull-up, but 5V power supply. Also 10n blocking capacitor in parallel. In my simulation 10n introduces some ugly voltage spikes that would trigger spurious edge detection on MCU. In fact - I observe it really does it. With the capacitor I get spurious edge detection, when the capacitor is removed, it works properly.

However - I probably should have a blocking capacitor, but not of such high value. On the simulation 33p works great, also - in the real circuit I get correct readings. So - should I leave the 33p or maybe it's safe to remove it?

BTW, the measured signal is 20Hz to 200Hz dirty square with like 5% duty cycle, inverted, so it's low when the sensor is lit.

Lead II

Actually what is your simulation goal?.Connect an TCST2103 or model the input of an STM32?

Why don't you connect the TCST2103 as shown in the datasheet:

the collector to VCC, a resistance (~100 Ohms) between the emitter and ground, and the GPIO connected to the emitter?

What MCU do you use? On the datasheet of the F429 the GPIO input leakage is 1uA. This can be neglected with respect to the current through the resistor (~4mA).

Turn on / turn off of the signal is ~10us. So the signal frequency will be < 50KHz. This is very slow for a STM32 GPIO. Don't worry with "what really lives in the STM32 chip".

In my opinion the real question is what level are you going to get on the emitter? It will probably be necessary as indicated in the datasheet to play with the current of the LED to adjust this, check the STM32 datasheet for Vil, Vih and Vhys values.. I don't think this can be done in your simulation, which doesn't even use a model of the TCST2103 .

> Actually what is your simulation goal?

> In my opinion the real question is what level are you going to get on the emitter?

Well, that's exactly why I started to draw that circuit in the simulator. I wasn't sure about the input resistance of the MCU, so I tried a couple of guessed values to see how the voltage changes. Also, I wanted to test why when the 10n capacitor is connected on the input, my counter reports the doubled value of the measured frequency.

For 47k (so like a weak pull-down was used) and 10k for the detector supply - the voltage would drop to around 2.7V - that would be unacceptable. Why 10k not 100R? Because what I do is redesign an older device that worked with TTL. I have the original schematics and they put the 10k resistor there. I tested what could happen if I left the resistor with lower supply voltage, and if somehow my input resistance was as low as 47k. I figured out it could affect the readings, so I decided to lower the detector resistor to 2k2, that gives over 3,1V in that case. Knowing that in fact the input resistance of STMH747 GPIO is much, much higher, it would probably work with just ANY resistor, even 47k for example.

But there's a limit. We can't reduce the current below certain threshold because it could either become undetectable for GPIO, or it would cause undefined states.

So 100R would work for sure, but what would be the advantage? The detector would just use more current for the same effect. So I put 2k2, the current is negligible, but still high enough to be way above any noise I get in my shoddy wiring. The input voltage in high state is over 3.2V, so it's good.

Another goal of the simulation was to see what effect does the parallel capacitor have. In the legacy version there was 10n capacitor used. It seems to introduce bad voltage spikes. It looked bad on the simulation, it also broke the reading - the voltage spikes were read as extra slopes that shouldn't be there and my counter read the doubled frequency.

My device works fine without any capacitor used, however, I'm not entirely sure if it would still work as good in a very noisy industrial environment. So I'm just cautious about removing the blocking capacitor entirely. The simulation shows, that using 33p instead of 1n causes so small oscillations that they can't be detected by GPIO.

Lead II

Of course 33 pf has less effect than 1nf, but also much less useful. If your signal is continuous you can filter it with a capacitor. But a cap is useless on a frequency.

" I wasn't sure about the input resistance of the MCU" Again, read the datasheets before you do a design.

The STM32 GPIO input impedance is much higher than 47k ohms, in the order of 1M ohms.

In my opinion you have to trust the TCST2103 datasheet more than an old design, whose specifications you may not know.

What is the length between the sensor and the MCU input ?

Do you use coax cable with suitable impedances? Or a properly connected shielded cable?

This is the best way to avoid noise in an industrial environment. If the signal arrives distorted on the GPIO, often it is too late or too difficult to correct it.

I think this is it. I agree, so no capacitor. The cable is circa 1,5m, shielded (production device). My test prototype has unshielded cable, roughly the same length. Since it's tested without the capacitor and works, I remove the capacitor from the new schematics.