Author Topic: Protecting GPIO pins on MCU  (Read 7080 times)

0 Members and 1 Guest are viewing this topic.

Offline MrVeeTopic starter

  • Contributor
  • Posts: 14
  • Country: au
Protecting GPIO pins on MCU
« on: August 04, 2018, 05:38:59 am »
Hi All,

There has been some past threads on this topic and I have learn't a bit from free_electron and others in this thread (https://www.eevblog.com/forum/microcontrollers/robust-microcontroller-input-protection). I was hoping to get some feedback on the circuit I have design below. The specification is that I am running a 3.3v MCU on a 24-28VDC vehicle. I need to have robust inputs that can detect either +28vdc switched or GND switched. The MCU that I am using can be configured for a Pull-up or Pull-down resistor internally (value varying from 20kR - 60kR). I wanted to keep this software definable pull resistor.

In the falstad circuit I have drawn up and attached, starting from the input side I have simulated the 3 possible signals that are most likely to occur (I know with esd etc there could be many many more scenarios). Switched ground, switched power and a reverse voltage.

After the first current limiting 1kR resistor there is two BAT54 schottky diodes for quick shunting of either a reverse voltage situation (bottom diode) or an overvolt >12v situation in which a clamping zener is switched on to regulate the 28vdc down to 12vdc. (in fact this zener voltage would be 0.1-0.5 volts under the 12v power supply so it is always biased allowing it to 'switch on' faster.

The second 1kohm current limits again and a 3v zener regulates to remaining 12v to 3v.
A capacitor and pull-up resistor in the case of switching ground creates an RC circuit for switch debouncing.

The 20k resistor switched between 3.3 and gnd is internal in the mcu as well as the schmitt trigger.

I realise this all could be done with less components if I could simply increase the size of the 1kR resistor at the start to 10kR, however this causes the 'low input' voltage when switch to ground be too high to be recognised as a 'low' by the schmitt trigger, due to the voltage divider. Frustratingly the 20kR pull resistor could be higher by design and would fix this issue.

One issue I see is that in a reverse voltage situation, the first 1kR resistor is subject to 0.5W.

Anyway, I feel like I may be missing something here, an easier way to skin the cat if you will. I am not an expert by any means but would like to learn off the more experienced members here if possible.

« Last Edit: August 04, 2018, 05:46:19 am by MrVee »
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5068
  • Country: bt
Re: Protecting GPIO pins on MCU
« Reply #1 on: August 04, 2018, 07:29:13 am »
1. how fast should be the input circuitry? Signal rise/fall times?
2. all today's MCU's inputs have already got the 2(or 4) clamping diodes (the same as your BAT54s), with typical max current 2-5mA
3. it seems to me a single zener could do it (provided we talk 3.3V MCU)

---> 1k ---> 3.9V zener (cathode to 1k, anode to gnd) ----> 1k -----> MCU input

With say 30V input the zener current will be aprox (30-3.9)/1000 = 24mA.
The current via upper clamping diode will be aprox (3.9-3.3-0.7)/1000 = ~0mA.

With say -30V input the zener's If current will be aprox (-30-(-0.7))/1000 = -29.3mA.
The current via lower clamping diode will be aprox (-0.7-(-0.7))/1000 = ~0mA.

You may increase the first 1k to say 10k, with 10x lower currents then. The input will be slower, however (mind the RC constant).
You have to elaborate with zeners - their "knee" is rather wide with 3-6V zeners (it means the diode starts to conduct at a bit lower voltage than the zener one). From 6.2V up their knees are much sharper.

When not concerned with speed I would use 10k-zener6.2V-10k-10nF-MCU_input. You may add the 2 BATs diodes wired directly to the MCU's input via 1k resistor - it would be then "according to the book" (in line with a good engineering practice) :)
« Last Edit: August 04, 2018, 08:35:23 am by imo »
 

Offline MrVeeTopic starter

  • Contributor
  • Posts: 14
  • Country: au
Re: Protecting GPIO pins on MCU
« Reply #2 on: August 04, 2018, 11:15:39 am »
Hi imo,

Thanks for the reply.  To answer your question, rise and fall times are quite slow, we are talking about detecting a switch being turned on and off (mechanical actuator, could be 5minutes to 3 hours).

So my concern is with this equation:

Quote
With say -30V input the zener's If current will be aprox (-30-(-0.7))/1000 = -29.3mA.
The current via lower clamping diode will be aprox (-0.7-(-0.7))/1000 = ~0mA.

The 3v zener that I am looking at (MM3Z3V0ST1G) is rated to 300mW, so... 3*0.0293 = 87.9mW dissipated. All good for the zener, however my concern is with the 1kOhm resistor that is actually doing all the work, dropping 29.3v @ 29.3mA = 858mW. That would mean quite alot of heat generated, and need to put atleast 1W resistors in the circuit.

As you suggested you can raise the 1kOhm resistor to 10kOhm. The problem then arises when you want to connect the input to a switch that switches to ground. Because of the voltage divider network the MCU could see up to 1.2volts in the 'Ground State' which will not trip the lower threshold of the internal schmitt trigger, due to the 20kOhm pull-up resistor.

This is currently what is causing me to:  |O. I have increased the input resistor to 2kOhm and it is very close to the lower threshold limit of the mcu input, and I beefed up the 2kOhm resistor to a 3/4Watt.
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5068
  • Country: bt
Re: Protecting GPIO pins on MCU
« Reply #3 on: August 04, 2018, 11:24:58 am »
Quote
To answer your question, rise and fall times are quite slow, we are talking about detecting a switch being turned on and off (mechanical actuator, could be 5minutes to 3 hours).
:)
When you just need to monitor whether a switch is on/off you may increase all your resistors to 100k with no 20k pullup.
Why do you need the 20k pullup there??
« Last Edit: August 04, 2018, 11:31:36 am by imo »
 

Offline MrVeeTopic starter

  • Contributor
  • Posts: 14
  • Country: au
Re: Protecting GPIO pins on MCU
« Reply #4 on: August 04, 2018, 11:38:28 am »
The requirement is that a switch may be connected to the MCU GPIO pin that is either 'open/+28V' or 'open/GND'.

Internally inside the MCU there is a 20k 'pull' resistor that can be programmed pull-up or pull-down.

I feel as though I am missing something fundamental here, but when using an open/closed switch to ground a pull-up resistor is required to hold the pin 'HIGH'... otherwise it's state is floating. When using an open/closed switch connected to +VDC then a pull-down resistor is required to hold the pin 'LOW'.
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5068
  • Country: bt
Re: Protecting GPIO pins on MCU
« Reply #5 on: August 04, 2018, 11:44:40 am »
This will work with your internal 20k pullup. Inverted logic, easy to maintain in sw. Transistor any modern NPN.
« Last Edit: August 04, 2018, 11:58:43 am by imo »
 

Offline MrVeeTopic starter

  • Contributor
  • Posts: 14
  • Country: au
Re: Protecting GPIO pins on MCU
« Reply #6 on: August 04, 2018, 11:56:56 am »
Thanks, imo. Would you mind please running your simulation 0v to +28Vdc square wave (instead of -28vdc to +28vdc) and show what the results are?
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5068
  • Country: bt
Re: Protecting GPIO pins on MCU
« Reply #7 on: August 04, 2018, 11:59:19 am »
See the third picture :)

When at 0Volt, or disconnected, the current which may open the transistor comes from Ireverse of the upper BAT diode. The schottky diodes have got pretty large Ireverse (up to few uA) therefore the R8 shall stay ie 10k. You may use 1n4148 instead (Ir few nA), then the R8 could be larger ie 100k.

And all 3 and 5 signals:
« Last Edit: August 04, 2018, 12:17:05 pm by imo »
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22283
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Protecting GPIO pins on MCU
« Reply #8 on: August 04, 2018, 12:14:17 pm »
This is a bad mess...

Better is:

1. Don't.  Use another GPIO port to control pull-up/downs at bus voltage.  (You're still writing exactly one port to set pull state.  What's it matter that it's called PULLUP instead of OUT?  >:D )  Sense the input voltage with a voltage divider (and preferably a schmitt trigger gate as well, so the GPIO isn't seeing indeterminate logic levels in case of that).

2. Pull both simultaneously, so the open-circuit input pin voltage is 14V (or whatever Vbus/2 is).  Sense the input voltage with a split voltage divider (3 resistors in series), wired to two GPIO pins.  When GPIO(low) and GPIO(high) are both high, you know you have ~28V on the input.  When GPIO(high) is high and GPIO(low) is low, you know you have ~14V (o/c).  When both are low, you have ~0V.  Bonus points for using a precision schmitt trigger (e.g. 74HC7014) or comparators to set well-defined thresholds here.

3. Do (2), but with ADC inputs.  Read the full, say, 0-36V range, and set thresholds in software.  Bonus: add digital filtering and hysteresis, so your input circuit is as simple as possible (three resistors and a capacitor, maybe a TVS optionally), while your detected states are precision and robust.

Note that 2 is just a discrete (unary) ADC, and 3 is using the onboard hardware, with many more bits (and binary!), for the same end.

I've done (1) and (3), professionally, with excellent results.  MCUs are cheap as sand; PCB space, BOM count and engineer time isn't!

Tim
« Last Edit: August 04, 2018, 12:16:54 pm by T3sl4co1l »
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline MrVeeTopic starter

  • Contributor
  • Posts: 14
  • Country: au
Re: Protecting GPIO pins on MCU
« Reply #9 on: August 04, 2018, 12:20:55 pm »
Thanks for persisting with this! So I have run my own simulation and I have confirmed yours, it looks good but the flaw is this: A square pulse drives from 0 to 28vdc or -28vdc to 28vdc does not represent the case of a switch simply being open circuit and then switching to ground. The simulations running detect a ground and and open circuit as the same thing. However in the case of the requirements for the project you have to be able to detect with a ground is applied to an otherwise open circuit.

1. Must be able to detect open/28vdc
2. Must be able to detect open/gnd

Your circuit works beautifully for a 0-28, -28 to 28 etc signal but it can not detect a switch that is open, and when closed: it switches to ground.  ;D Sorry to be a pain! I'm not the best at explaining things
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5068
  • Country: bt
Re: Protecting GPIO pins on MCU
« Reply #10 on: August 04, 2018, 12:25:45 pm »
Draw a schematics around the switch.. With the 28V battery, the switch, the LOAD switched on/off by the switch,  the common ground and the wire which goes to the MCU's input..
« Last Edit: August 04, 2018, 12:30:30 pm by imo »
 

Offline MrVeeTopic starter

  • Contributor
  • Posts: 14
  • Country: au
Re: Protecting GPIO pins on MCU
« Reply #11 on: August 05, 2018, 12:59:58 am »
Quote
Draw a schematics around the switch.. With the 28V battery, the switch, the LOAD switched on/off by the switch,  the common ground and the wire which goes to the MCU's input..

Its simply mechanical actuated switches that, when not in operation are open circuit, when closed they are connected to either power or ground. Power being nominally +28VDC. See the attached schematic with the two possible switch inputs.

T3sl4co1l,
Quote
1. Don't.  Use another GPIO port to control pull-up/downs at bus voltage.  (You're still writing exactly one port to set pull state.  What's it matter that it's called PULLUP instead of OUT?  >:D )  Sense the input voltage with a voltage divider (and preferably a schmitt trigger gate as well, so the GPIO isn't seeing indeterminate logic levels in case of that).

Not a bad idea, the resistor being pulled high and low by either setting the port High or Low as an Output.

Quote
3. Do (2), but with ADC inputs.  Read the full, say, 0-36V range, and set thresholds in software.  Bonus: add digital filtering and hysteresis, so your input circuit is as simple as possible (three resistors and a capacitor, maybe a TVS optionally), while your detected states are precision and robust.


Are you talking about something like what I have attached in the schematic below? This works as the input is held at 1.65 volts (3.3/2) by the voltage divider, when 28VDC is applied it is regulated to 3.0v by the zener and the input is raised to 3v, when a ground is attached it is pulled close to 0v. By attaching this to an ADC input on the MCU, it will give me a whole lot of flexibility with the software. 3v = switch closed, 1.65v = open circuit, 0v = switch closed

Quote
2. Pull both simultaneously, so the open-circuit input pin voltage is 14V (or whatever Vbus/2 is).  Sense the input voltage with a split voltage divider (3 resistors in series), wired to two GPIO pins.  When GPIO(low) and GPIO(high) are both high, you know you have ~28V on the input.  When GPIO(high) is high and GPIO(low) is low, you know you have ~14V (o/c).  When both are low, you have ~0V.  Bonus points for using a precision schmitt trigger (e.g. 74HC7014) or comparators to set well-defined thresholds here.

How exactly are the resistors connected?

                        GPIO 1          GPIO 2
                           |                   |
INPUT--/\/\/\/\/---|---/\/\/\/\/\--|---/\/\/\/\/\----GND


 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5068
  • Country: bt
Re: Protecting GPIO pins on MCU
« Reply #12 on: August 05, 2018, 08:37:50 am »
Ok, you have got a switch with 3 positions:
1. 28V
2. 0V
3. High impedance (aka "open)
You want to detect all three states reliably with single MCU input.
Is that correct?
 

Offline MrVeeTopic starter

  • Contributor
  • Posts: 14
  • Country: au
Re: Protecting GPIO pins on MCU
« Reply #13 on: August 05, 2018, 08:54:31 am »
Yes, thats correct IMO. However, reliably determining the state is not so much the challenge as doing so while making the circuit robust enough to take transient voltages and reverse voltages. Basically protecting the GPIO pin, while maintaining the flexibility of setting the Pull resistor based upon the type of switch connected.


 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5068
  • Country: bt
Re: Protecting GPIO pins on MCU
« Reply #14 on: August 05, 2018, 09:38:56 am »
Ok, and your primary idea of detecting the HiZ state (Edit: "Open" or High Impedance) has been with those internal pullups/pulldowns -  getting 1/0 while switching pullup/pulldown in tells you it is the HiZ, right?
« Last Edit: August 05, 2018, 09:51:26 am by imo »
 

Offline nfmax

  • Super Contributor
  • ***
  • Posts: 1592
  • Country: gb
Re: Protecting GPIO pins on MCU
« Reply #15 on: August 05, 2018, 10:02:14 am »
Does your vehicle ever leave the ground for any length of time? If so, please bear in mind that your 28V nominal supply may go as low as 8V during engine start, and will have transient voltages of as much as +/- 600V from ground occurring from time to time. And that when lightning strikes, the definition of 'ground' will vary by hundreds of volts between different parts of the airframe.

A long time ago, I was involved with designing a DFDAU (Digital Flight Data Acquisition Unit) for aircraft flight data recorders. We used the combination of a 'slow' OPAMP Schmidt trigger (LM224, I think) followed by a 'fast' 54HCT14 Schmidt trigger. Ahead of this was a resistive divider (about 10:1), clamp diodes, and low-pass filtering. The protection network could be made quite lossy, and hence provide better protection, because the OPAMP provided gain. You could install a pull-up or pull-down resistor depending on whether you had a series or shunt discrete.

I may have the circuit (or a rough equivalent) somewhere - I'll look it out if you are interested.
 

Offline MrVeeTopic starter

  • Contributor
  • Posts: 14
  • Country: au
Re: Protecting GPIO pins on MCU
« Reply #16 on: August 05, 2018, 12:01:59 pm »
Quote
Ok, and your primary idea of detecting the HiZ state (Edit: "Open" or High Impedance) has been with those internal pullups/pulldowns -  getting 1/0 while switching pullup/pulldown in tells you it is the HiZ, right?

Yes, that is correct ;D

Quote
Does your vehicle ever leave the ground for any length of time?

... Yes indeed it does! And you raise very valid points, hence my concern with protecting the MCU. Looking at the datasheet for an Expansion GPIO IC by NXP, for example the PCA9554B, I am thinking that the ESD qualifications of this device would mitigate the requirement for additional hardware to handle transient voltages. Perhaps its more cost effective to spend the money here than additional components and pcb space as T3sl4co1l pointed out.
 

Offline jpham

  • Newbie
  • Posts: 5
  • Country: us
Re: Protecting GPIO pins on MCU
« Reply #17 on: August 05, 2018, 07:18:14 pm »
how about using semtech smo5 (family smo5 to smo36), it is a smd device, about the size of a grain
design for transient voltage supression
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf