PNP/NPN detection circuit

I am trying to create a circuit that I can connect a 3 wire 24v photoelectric sensor to and have one led light up if its pnp and a different led light up if its npn.

Leds will be swapped for optocouplers later and fed into separate inputs on an esp32 as part of a bigger project.

Problem. I’ve tried a bunch of different circuits I’ve found online using different variations of resistors, transistors and diodes but can’t seem to have both leds stay off with no input.

Any help appreciated

1 Like

Hi Larry
Welcome.
Would it not be easier to look up the data sheet for that particular sensor.

The basic difference is that a PNP sensor will connect the 24V to a load (High side switching) and a NPN device will be Open Collector (Drain) and connect the low side of a load to ground (Low side switching).
Cheers Bob

1 Like

I’m not sure if it would be ‘easier’. We are in a high demand food manufacturing plant with limited internet access in most areas so looking up things is almost impossible without leaving the room which requires changing and sanitizing twice per trip. Just wanting to be able to leave a test rig in each room toolbox to plug in and get the info we need.

1 Like

I honestly at first thought it would be a simple rig but everything I’ve tried gives me current across both leds when there’s no input firing the opto couplers even with no input

1 Like

Hi Larry

Nothing is ever easy is it
BUT surely you know what sensor type you are using so you could look up the details BEFORE entering. If your paper trail is so poor you can’t find that info without physically eyeballing the object I would give up.

Yes the only simple way to test for type I think is something like this


But as you say both LEDs are ON when there is no output from the sensor.
I don’t at the moment see any way around this. It is sort of a negative test.

You could try about an 18V zener in series with the bottom LED and reduce the value of the corresponding resistor to allow about 10mA to flow to match the brightness of the top LED. That should work (I think). Or maybe it won’t.
Cheers Bob

If this is driving some sort of logic you might be able to cope with this there.

1 Like

Hi Larry
Another alternative. Arrange for the sensor to have a definite output while testing. Should not be too hard to arrange and that will let you use the simple circuit above. After all you are only checking the polarity (NPN or PNP) of the sensor. If both LEDs remain ON you possibly have a faulty sensor. Then you will have to use a trusty old multimeter to confirm.
Cheers Bob

1 Like

Hi Larry
I have been doing a bit of thinking about this.
I am dot sure why you want to do this in the first place.
Surely if the sensors are in a working situation there must be some record somewhere regarding the type used and not need to go to all this “testing” operation.

If, on the other hand they are yet to be fitted somewhere what is wrong with getting the sensors that you will need (NPN or PNP).

There is another scenario which I shudder when I think about it. That is you intend to set up a system where it does not matter which polarity is used. I don’t think you can do that. Not easily anyway. There will probably be some exotic method of doing this but I think it would involve a bit of hardware which would probably slow things down a bit. It probably depends on how fast you need the response time, you have not mentioned that.

There used to be and probably still are sensors around which allow selection of NPN or PNP. I think from memory some with a switch and some with 4 wires. Just use the output wire that is required. You might have to look further afield than the hobby market for these.
Cheers Bob

1 Like

I’d rather not get into explaining why it would come in handy, to be fair right now it’s probably more about satisfaction of figuring it out.

The circuit you posted was one that I tried along with a similar one using a pnp and npn transistor till I figured out there was always a path across both LEDs and both resistors pulling half the current 5ma. Otherwise operates exactly how I want it when it gets a + or - input. Correct LED lights up and I get full current 10ma.

I feel like I need some sort of diode matrix to cut that separate that path

1 Like

Hi Larry
I have an idea. Will try to post a schematic tomorrow.
Cheers Bob

2 Likes

Hi Larry
As promised a schematic that should work. Be aware the sensor will have to be activated to show any LED response I can’t see any way to test sensor polarity without activating it.

We have a “window” comparator. The LM393a will activate (Pin 1 LOW) when the input is above 18V (PNP) and LM393b will activate (Pin 7 LOW) when the input is below 6V (NPN).

PNP will show the top LED while NPN will show the other as marked. T activity should be both less OFF.

Please note: I have no way of knowing the status of sensor output while inactive. R9 and R10 might be required to bias the sensor output to half rail voltage, in this case 12V while in the idle state. This will ensure that both LEDs are OFF when the sensor is idle.

This circuit is untested but is a tried and proven arrangement so I reckon should work.
Cheers Bob

2 Likes

You’re a legend Bob…

I’ll build this out and let you know how I go.

Appreciate the extra effort.

I have also asked a friend about it, I’ll let you know what he comes back with as well.

1 Like

Great news. It worked exactly as needed.

FYI - R9 and R10 broke the functionality. Worked fine without them

Thanks again Bob

1 Like

Hi Larry
That’s good. I put R9 and R10 in there as I did not know what the sensor did at idle, that is not activated. For this to work the sensor output has to sit between 6V and 18V in idle condition. When activated NPN = Ground (0V) and PNP = rail (24V). Approximately. The sensor probably sits at about 12V in the non activated state.
Cheers Bob

1 Like

So after looking at this a bit closer the 2 LEDs are still turning on with no input. Just not as brightly as last time. Only 1/3 current through hem not 1/2.

Hmmm

1 Like

Larry

Yes a bit strange.
The only way a LED should come on is if the comparator inputs go outside the 6V to 18V (approximate) range. If left floating the sensor output could be doing this for very short periods which due to your eye persistence would appear to make the LEDs glow dimly. By the way, this is usually not linear.

Just had a light globe moment. I have seen this before. It is due to the 393 output (open collector) not being pulled all the way to 24V when off. Due to the LED being a diode.
The fix… Another couple of resistors, 10k seems a good value, connected from 24V to the output of both comparators (2 resistors) Pins 1 and 7. This will not affect the normal operation of the LEDs.
That should fix it.

By the way what was the effect when R9 and R10 were fitted. I h might have a magnetic variety of such a sensor in an obscure box somewhere but would not hold out much hope of finding it so O could not say for sure just what the effect would be. If you could post the sensor info I could have a look.
Cheers Bob

1 Like

I’ll give that a try next time I’m back at work. Just on a 4 day holiday.

I was testing it on a bench top power supply with a current meter. LEDs were definitely being supplied power. 3.74ma with no input. 10ma with an input.

Re r9 and r10… Testing my memory a bit here but I believe my pnp light was solid on all the time. One sensor changed states of the other led and the other sensor did nothing.

1 Like

Hi Larry

1mA to 2mA LM393 quiescent current, 0.6mA for R!, R2 and R3 reference resistors across the supply. That leaves a bit over 1mA for the rest.

The idea of the 10k resistors effectively in parallel with the LED / Resistor combinations is to make sure the LED has the same Voltage on both ends when not grounded by the comparator output. Thus will not be turned on and no current.

Regarding R9 and R10. Are you sure they were BOTH 51kΩ. If for instance R9 was 5k1 and R10 51k then what you describe would certainly happen as the input voltage to the comparator would then be well above the 18V reference.
If they were both 51k the junction would be 12V which would not switch either comparator. UNLESS this somehow upset the Sensor enough to switch the upper comparator. I have no idea what these sensors are so can’t comment at the moment. Try removing the sensor with R9 and R10 fitted and let me know what happens.
Also if you let us in on the secret of the sensor types it may help.
Cheers Bob

1 Like

Yeah almost certain I had the resistor values right but small chance I got it wrong. Very small though since pulled them all out before the build and had no spares.

If you are asking which sensor model we use, how long do you have buddy, we have 100s and 100s of sensors across 3 manufacturing lines. Sick barrel vte18 range is likely what I was testing with since we have a lot of them

1 Like

Hi Larry
Had a quick look. See what you mean.
The “18” bit refers to the thread size, M18. Some of the other numbers could refer to internal opto couplers as they nearly all start with “4N” XXX.

See what the 10k resistors do fitted across the LED/resistor combination. I am pretty confident that will work. I think I remember coming across this problem before.
Cheers Bob
Modified schematic. Added resistors designated R11 and R12

1 Like

Hi Larry
I am a bit concerned about the result you got with R9 and R10 in circuit.
There are about 3 things that could happen here.
The inclusion could have upset the sensor operation somehow. I just can’t see it at the moment.
The resistor values are not the same as I suggested.
R10 is not connected to ground. This would put the full 24V at the resistor junction instead of 12V.

Are you using a breadboard? Some breadboards have gaps in the supply or ground line or both. ie; they are not continuous right through. These strips are usually indicated with a Red and Blue line printed on the board. If the line is continuous it is a fair bet that the connections are continuous. But if the coloured line has any gaps there may be a gap in connection at that point. If this is the case you could have got the ground end of R10 in the wrong place which would leave this point ungrounded. A point worth checking.
Without these resistors in the comparator inputs are left floating. Not a good idea for guaranteed stable operation.
Cheers Bob

2 Likes