Post History
Starting with your first circuit: It seems a little klunky, but should work. I don't like the D1 and D2 diodes. The reason I placed D1 is to avoid triggering the mosfet with very low voltage li...
Answer
#1: Initial revision
Starting with your first circuit: <img src="https://electrical.codidact.com/uploads/xxuczvl3rgrunwe3c9qmlldsfivl"> It seems a little klunky, but should work. <blockquote>I don't like the D1 and D2 diodes. The reason I placed D1 is to avoid triggering the mosfet with very low voltage like 2V to 4V. Is D2 really necessary?</blockquote> The input voltage threshold for sensing high is the 8.2 V drop across D1, times 1.1 (the reciprocal of the R1-R3 divider ratio), plus whatever gate voltage is required to turn on Q1 sufficiently to pull OUT low. If the gate threshold is 2 V for example (you didn't link to a datasheet so I didn't look it up), then that comes out to 11 V. That's a reasonable threshold when the nominal input values are 0 V and 24 V. One problem with this is that the gate voltage to pull OUT low is usually poorly specified. Using D1 instead of having the R1-R3 voltage divider attenuate more is one way to get more consistency. Only you can say whether that's needed in your case. D2 should not be necessary as long as the minimum gate voltage to turn on Q1 is well above one silicon junction drop. It probably is. Check the datasheet. Note that D1 performs the same function by acting as an ordinary diode when the input is low. You could lose D2, but inject the pullup current immediately after D1. As for your second circuit: <img src="https://electrical.codidact.com/uploads/rksijcz7h2agxvkziawzg56ek5bc"> I would move R2 to the other side of R3. That way, R3-R2 form a voltage divider. With the input grounded, the forward drop on D2 will be about the same as what it takes to turn on the transistor. The voltage to the right of D2 needs to be attenuated to guarantee the transistor is off when IN is at ground. You could remove D2 completely, move R2 to the right side of R3, and adjust the input voltage with the R3-R2 divider: <img src="https://electrical.codidact.com/uploads/x3jyoxu9uy0o2p43soslkj4tm7g5"> There will be some slop in the input threshold voltage due to the uncertainty of the exact base voltage that Q1 will turn on at. There will also be some temperature dependence. However, if you are trying to sense "near 0 V" versus "near 24 V", this should do well enough. If this were an industrial situation, I would put a reverse diode across R2. That would protect Q1 from large negative input voltages. If you really want to make this robust, make sure it can tolerate 250 VAC at IN. That means R1 needs to be ½ W at least. 1 W would give good margin. I would also put a 400 V diode in series with R3. That prevents a large positive input from back-driving the 24 V supply, and reduces the power requirement to withstand 250 VAC. With the diode, I'd also make R3 a 1 W resistor. Here is the final industrial-strength version: <img src="https://electrical.codidact.com/uploads/vtr6352ut4c1yr9we2f41qohg7ve"> This will detect 0 V versus 24 V in, but can withstand 250 VAC accidentally connected to IN. One issue with this circuit is that it has no hysteresis. Perhaps that's not important in your application.