# Detecting ground on GPIO and protect from positive voltage

I need to sense a logic low on the GPIO. The input will come from external resources (By end user) so I need to protect the circuit as well from high positive voltage. Generally the circuit will be used in places where I don't expect a voltage of more than 30 Volt. So from external input, it can be a ground (If user does it correct) or a max of 30 volt, in case user does a wrong wiring. Is this circuit going to be good enough?

simulate this circuit – Schematic created using CircuitLab

Please suggest if there is any mistake and if this can be simplified.

• I don't see how your circuit can possibly work. Can you explain how it is supposed to work? What does V1 0V supply represent? – Vince Patron Sep 5 '17 at 16:09
• Where is the "user input" in this diagram? – Ale..chenski Sep 5 '17 at 16:12
• In your scenario, is a grounded input equivalent to an unconnected floating input? – brhans Sep 5 '17 at 17:29
• V1 is the user input.. It can be anything between Ground or 0 to 30 V. I need to detect when it is grounded. I have updated the schematic, removed V1 and marked it as user input. – Rakesh Mehta Sep 5 '17 at 18:48

Why can't you just clamp the input and put a high resistance to keep the input current low, like this simple circuit?

simulate this circuit – Schematic created using CircuitLab

With 30V input, the resistor sees about 25V across it. 25V / 10k = 2.5 mA. Pretty small. Power dissipated would be 25^2 / 10k = 0.0625 Watts, quite small.

The GPIO input of most MCUs are CMOS so current is ideally 0. Realistically most datasheets will spec maybe 100 nA, so 10k would be low enough to certainly detect the logic state at the input.

The zener would also protect from user accidentally injecting negative voltage as well as ESD strikes.

• I want to sense ground and not a positive volt on the input. Just need to protect the input so that even user apply +V instead of ground my GPIO is safe... So the intention is to detect when user is applying ground to the input.. A floating input means I'll detect LOGIC HIGH on the GPIO – Rakesh Mehta Sep 5 '17 at 18:52
• Then add a weak pullup to this circuit. The point is that you don't need all that complexity in your circuit. – Vince Patron Sep 5 '17 at 18:55
• When 30V applied, what will be the zener current? Also are zener diodes quite dependable? I have heard many times that practically it does not work exactly as per it's breakdown voltage. Just looking for suggestion.. – Rakesh Mehta Sep 5 '17 at 19:00
• Sorry, discard my last message.. I looked incorrectly.. I treated left as input and right as output. :) So sorry... In floating input condition, if my GPIO is internally pulled up, it will detect logic high, right? – Rakesh Mehta Sep 5 '17 at 19:11
• Yes, CMOS inputs float. Some MCUs have a setting for "weak pullup", in which case, an open input will pull up and indicate high. Make sure you understand external circuit driving this. If you use weak pull and there is a long wire connecting input, it could pick up AC line voltage and give you 50 Hz or 60 Hz signal. You might need to do some SW filtering or apply external stronger pullups. Zener diodes are absolutely reliable. The problems are almost always from engineers who don't understand them. – Vince Patron Sep 5 '17 at 19:15

The answer is to use a simple diode.

simulate this circuit – Schematic created using CircuitLab

Yes, it's that simple. If your input is grounded, the GPIO will detect a logic zero. If it is above ~1V, it will detect a logic high. The voltage you can apply externally depends on the reverse breakdown voltage of the diode.

• Its difficult and quite costly to have a diode with reverse breakdown of 30V – Rakesh Mehta Sep 5 '17 at 19:04
• Sorry, I am wrong there.. 40V reverse breakdown diodes are cheap indeed. Looking at the data sheets.. I am such a dumb :) – Rakesh Mehta Sep 5 '17 at 19:06
• Just a question, when I am applying ground to the input, will it not be 0.7V at the GPIO? – Rakesh Mehta Sep 5 '17 at 19:09
• Yeah, but as a rule of thumb the µC will detect anything less than half of Vdd as low. – Janka Sep 5 '17 at 21:04