# Making a light detector using a phototransistor and an open drain comparator

I want to make a light detector using a phototransistor. To do that I connected a phototransistor and apotentiometer to the open drain comparator as in the diagram below.

When the light is dim, everything is OK, but when light is bright (normally or even slightly bright) then the LED lights even when voltage of the potentiometer is more than 700mV higher than the voltage of the phototransistor.

Specifically, when I turn the potentiometer knob, the voltage between the voltage source and the comparator output rises gradually until it hits maximum around the point where comparator inputs values are equal and then it remains constant.

So my problem is that comparator doesn't work like a step function, but more like a gradually increasing function.

What am I doing wrong? simulate this circuit – Schematic created using CircuitLab

• What does the datasheet (should be linked in the Q) say about operating off 3.3V supply? Feb 7, 2021 at 14:48
• Datasheet for the comparator. Note the part about common mode input voltages. Are you exceeding the allowed range for your supply voltage? Feb 7, 2021 at 16:15
• Supply voltage and common input mode voltage are different things. You're using an allowed supply voltage, but what about your common mode input voltage? Feb 7, 2021 at 16:30
• The others have already indirecly said that the input voltages must be 0...1.8V for reliable comparisons. I add that your circuit can easily work as an oscillator. If you build it on a breadboard the stray capacitances can collect anything, The same happens with long input wires. As the last nail your schematic doesn't show a slightest decoupling capacitor to prevent operating voltage instability. I would add say 100nF ceramic between the Vcc and GND with as short wires as possible. BTW is the ambient light constant or does it flicker at some maybe invisible high frequency?
– user136077
Feb 7, 2021 at 16:57
• One other random guess: R1 is huge for the light range you're interested in. The phototransistor gives ~ 0.5 uA per lx into 2Mohm, so your gain is something like 1 volt per lx, which means the phototransistor is saturated most of the time. I think the circuit should still work in saturation, but I would test with a 100k resistor and see if your problem goes away. Feb 7, 2021 at 17:20

In all practical cases you will want to include some amount of hysteresis. Otherwise, you will experience jitter in the output. And allowing that is always a bad thing. So you must design in some hysteresis behavior. For this, you'll need positive feedback.

Also, the TEPT5700 has only two pins: collector and emitter. The NPN base is floating and responds to photons impinging so as to generate recombination current to allow current from collector to emitter.

Finally, the TEPT5700 specifications focus on $$\V_\text{CE}=5\:\text{V}\$$. You are using $$\V_\text{CE}\le 3.3\:\text{V}\$$.

You can achieve this with a simple circuit using two BJTs: simulate this circuit – Schematic created using CircuitLab

Left side is the circuit with hysteresis. Right side is the circuit that activates an LED on the basis of light input.

You can make some adjustments, if needed, for light level. For example, for a wider hysteresis band you might try setting $$\R_2\$$ and $$\R_3\$$ to $$\47\:\text{k}\Omega\$$ and $$\R_1\$$ to $$\8.1\:\text{k}\Omega\$$. Etc. Basically, play around with $$\R_2\$$ and $$\R_3\$$ and also, separately, $$\R_1\$$.

I've intentionally left off a potentiometer for the above circuit. But you can sink unwanted phototransistor current by reducing $$\R_1\$$, for example. So that's a likely place for a potentiometer. But I'll leave that detail to you.

So my problem is that comparator doesn't work like a step function, but more like gradually increasing function

This sounds like the op-amp gives constantly on/off signal to the led, as the voltage of the phototransistor and the Voltage of the potentiometer are getting really close + due to the parasitic/stray capacitances the comparison of the two is fluctuating.