# 12 V input on 3.3 V GPIO, TVS pulled down or Schottky pull up?

I am building my own PLC that has to accept permanent inputs of up to 30 V DC into a STM32F with 3.3 V inputs.

The switched input will have to work with 8-30 V, but 90% of the time the input voltage will be fixed at 12 V or 24 V. Inputs will only ever be switches such as limit switches, so I am not bothered about detecting inputs less than 8 V or inputs from sensors, etc., I'm also not worried about speed as realistically the fastest the switches will move is every 1 s; I just need to be sure that my microcontroller is protected.

I want a universal circuit I can use in multiple products/projects of similar types so component count, cost and PCB space has to be at a minimum so I don't really want to use optocouplers.

Two electronic engineers have recommended the following, but I'm not too sure what way is best:

Should I use the top one or the bottom one? Any why?

• I don't usually say this, but this sounds like a good place for an optocoupler. Nov 14, 2017 at 14:22
• @MattYoung could you update your comment to say why an optocoupler would be needed? Nov 14, 2017 at 14:36
• The circuit that I ask about in this question is an input circuit from a major brand PLC. Nov 14, 2017 at 16:08

This is actually an age old problem with PLCs and not nearly as simple as your intended solutions.

The biggest issue you have is that as well has having a wide variety of potential logic voltages you need to be able to handle, the actual logic levels can be far higher than the 3.3V rail you are using internally. Some sensors and devices have logic thresholds up above 5V. As such simply using a cut-off circuit as you have indicated will not detect the low level from such sensors.

The input stage of PLCs needs to be much more flexible.

Even if the low level logic level is acceptable these circuits each suffer from different issues.

Zener / TVS Limiting.

This circuit has the benefit that, for a known input voltage, the zener can be sized to never allow the voltage to exceed the rail voltage. Normally you would chose a zener with a smaller reverse voltage than the rail, but higher than the high level logic threshold.

However, the zener will spend a lot of it's life reverse biased, as such you pay a penalty in the form of reverse recovery time when the input signal drops which will delay your signal a smidge.

The other issue with the Zener, is the actual voltage it will limit at is dependent on the current through it. As such that voltage will depend on the signal voltage to some extent. You therefore need to design the resistor for the maximum input voltage, and recalculate for lower voltages to see if the zener is not limiting the voltage below your $$\V_{IH}\$$ level.

Over-Rail Limiting Diode

Using the diode up to the rail has the issue that the output voltage will still exceed Vcc all be it only by a little. However, that can still be detrimental to the input. Further, in this case the reverse recovery time means, for fast input edges, a high voltage will make it through very briefly.

So

Since both of these circuits include a high resistor at the input, both of them require whatever is driving the input to have a low output impedance. Of the two, the zener version provides better protection but at the cost of performance. Neither of them will work if $$\V_{OL}\$$ of the attached sensor > 1.5V or so.

Alternatives

Opto-Coupling.

A common method used by PLCs is to use opto-couplers.

simulate this circuit – Schematic created using CircuitLab

This method gives you the added benefit of isolation and ground separation. The issue with it is you need some form of signal conditioning between the sensor and the input to ensure the LED is lit at the right threshold, and that the correct amount of current is fed through the LED. That conditioning could be the simple resistor shown above, or a complex circuit which includes a comparator of some sort.

The speed of the opto-coupler is also a limiting factor. However, this method is commonly used because it gives you complete flexibility.

Another method is to accept the signal in an analog form, compare it to a variable reference with hysteresis, and generate the logic level that way.

simulate this circuit

Obviously the components, including the comparator, need to be chosen to accommodate the maximum input voltages. The circuit shown is fairly simple, it can get much more complex with filters, regulators, ESD protection etc.

Combination

For isolation reasons, you could combine the above, and have the comparator power a constant current driver to the LED of an opto-coupler.

If I were developing a product, I would assemble all that on a small plug-in module that could be plugged into card edge sockets on a "mother" board, like they use for cards in PCs. That way you can easily replace them should one get fried. That method also lets you make other input types available, for example, a fibre-optic input.

• Would a BJT or a FET not be a good, safe, cheap way of doing this? OK, the signal would be inverted, but software would fix that. Genuine question, not trying to be clever. Nov 14, 2017 at 16:04
• @DiBosco Same problem of $V_{OL}$ not necessarily being 0 V. Nov 14, 2017 at 16:07
• Thanks for your very detailed answer, I have just updated my question to answer some of the issues you mentioned. Input voltage will be 8-30v with a maximum freq of 1s Nov 14, 2017 at 16:43
• @TerryGould cool, then I have already shown you pretty much all you need to know :) Nov 14, 2017 at 16:48
• Would it change much if the input was 5V tolerant? Many on STM32F are, with absolute maximum at 5.3V. Also Vih is 0.7*Vdd so 2.3V for a 3.3V rail. Nov 14, 2017 at 17:51

100k is way too high. It would trigger from almost any relay or switching operation nearby. Not really reliable for a PLC if you ask me.

There are actually standards and regulations for PLC's. Since you'd want to have all PLC vendors to have similar behavior in installations, and it would be nice if various models could be connected to each other without problems.

For example, the input only considers it an on when it's sinking at least ~2 mA and it's above 10V. (IEC 61131-2)

You can't get this precisely with passives, which is why there are parts like SN65HVS880.

In my previous answer I've given a schematic example on how you can try to get close to this behavior with passives*.

A simple 100K and a BAT54S won't be reliable, I can tell you that from experience.

*separate schmitt trigger not essential

Both are acceptable. You'll need to make sure the resistor is sized correctly so that the input current doesn't cause the input voltage to drop below V_IH, but with CMOS this is trivial since the input current is so small (100k is almost certainly fine)

With the second the only caveat is that you'll need to be sure that the total load on 3.3v is never less than 30V/100k (times however many inputs you have), or else the 3.3 V rail might be pulled up to a voltage that could damage the devices on it. If you put the micro into sleep mode it might draw that little.

The other caveat is that in both cases the 100k resistor acts with the input capacitance as a low pass filter, which slows down the inputs. If there's 10pF input capacitance they will have a maximum toggle rate of about 100kHz and a delay of about 2 microseconds.

• Neither works if $V_{OL}$ of the attached sensor > 1.5V or so. Nov 14, 2017 at 15:42
• "Neither works if VOL of the attached sensor > 1.5V or so." - add a resistor from GPIO to ground to form a voltage divider. Problem solved! Nov 14, 2017 at 17:45