Microcontroller input protection for button - Page 1
Forum section: electronics / beginners

author: Veketti
author_rank: Regular Contributor
timestamp: February 18, 2023, 11:03:30 am
content: Dear All,I'd like to hear your opinion about this button input circuitry for microcontroller, whether it works? There is low pass filter for button debounce (1k+0.01uF) and BAT54S clamping schotty diode for voltage spikes. I read from TI's publication that you shouldn't clamp directly to VCC as it might destroy all device which are connected to VCC. So I was planning to use 5V1 Zener diode to clamp it to ground. So it should clamp any voltages over 5.3V (0.2V Vf + 5.1V Zener) to ground. Am I right that this would be better than clamp it directly to VCC?Thank you in advance.

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 18, 2023, 11:48:32 am
content: If it is just a push button like in your schematic then you don't have to worry about protection. The RC de-bouncing filter already gives protection.But if you like to add some protection a TVS diode is better than a zener diode. A single TVS or zener diode to ground will do the job, so the other diodes in your schematic are not needed.

author: Veketti
author_rank: Regular Contributor
timestamp: February 18, 2023, 12:58:40 pm
content: Thanks, got it. However just would like to know whether that BAT54S and 5.1V Zener would work like described, even though it is not needed? Clamp negative voltage spikes to ground and positive spikes over 5.3V to ground as well?

author: Kalvin
author_rank: Super Contributor
timestamp: February 18, 2023, 01:56:57 pm
content: You could also consider splitting the 1 Kohm resistor so that the resistor values would become as follows:10 ohm => 470 ohms1 Kohm => 470 ohmsThat would reduce stress to the clamp diode, should the button produce high voltage spikes.If you want to reduce component count, you could remove the BAT54S altogether, and use the 5V1 Zener instead. A proper 5V1 TVS-diode would be even better, as the TVS-diodes will tolerate higher peak currents.

author: Kalvin
author_rank: Super Contributor
timestamp: February 18, 2023, 02:00:45 pm
content: Quote from: Veketti on February 18, 2023, 12:58:40 pmYes, BAT54S would clamp negative voltages to approximately -0.3V level, and BAT54S and the Zener would clamp the positive voltages to approximately 5.3V level.

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 18, 2023, 02:01:32 pm
content: Yes, that is correct. The BAT54S to ground will suppress negative voltages below the forward voltage of it and the other BAT54S in series with the zener diode will suppress any voltage above the zener rating plus the forward voltage of the BAT54S.The zener it self when directly connected between the signal and ground does the same, but works on positive voltages above the rated zener voltage and negative voltages below the I guess forward voltage of the diode it then sees. Don't recall the exact term for it, but that is how a zener reacts in that situation, like a diode.A TVS diode behaves similar but can withstand more power.

author: Infraviolet
author_rank: Super Contributor
timestamp: February 18, 2023, 06:24:14 pm
content: Unless the switch is physically far away from the microcontroller, or in an environment with a lot of ambient voltage spikes, a circuit where a switch pulls an input pin down to ground when pressed usually won't need protection at all. It is long wires, or environments with strong fields in them, which make protection on these sort of switches necessary. The only other circumstance I could think of would be if the switch somehow has an environmentally exposed conductor within it, that would need protection so it couldn't send environment derived ESD and similar spikes in to the mcu.

author: Veketti
author_rank: Regular Contributor
timestamp: February 24, 2023, 07:26:03 am
content: I didn't have any TVS diodes in inventory so I started studying them and ordered some for future projects. However that got me bit confused as for example SMAJ5.0CA which has Vr5.0 volts and to my understanding should be protection for 5V circuitry. My measurements show that it starts conducting at 6.7V and considering eg. Pic microcontroller datasheet absolute max for pins is VDD+0.3V = 5.3V. So are these TVS after all right components to protect the uC inputs?Or should I look for TVS diode where Vbr maximum is 5.3V.. That however doesn't even exist in at least littelfuse sortiment.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: February 24, 2023, 08:11:03 am
content: The clamping voltage of the MCU or etc. itself will be vastly higher than VDD, during an ESD pulse. Don't worry about it. The abs. max. rating is a DC figure only; how long you can apply some overvoltage is not defined. Evidently, it can handle -- well, whatever ESD it can. (Usually 1 or 2kV HBM is default; this isn't always defined in the datasheet(!!) but may be in supporting e.g. Quality documents.)(Occasionally devices will carry a rating like "VDD+2V for 2ns" which accounts for overshoot on signal traces due to poor signal quality. I don't think I've ever seen something for intermediate time scales i.e. us or ms. I've also never seen it defined in terms of an ESD waveform alone -- but this is implied by the ESD test itself.)(Also beware, ESD is done either powered down / disconnected, or at best assuming a power cycle immediately afterwards. ESD is quite likely to cause CMOS latchup, resulting in high dissipation (or failure) until power cycled.)You will also occasionally see a current limit figure, which seems exclusive (redundant) as the pin would have to be driven beyond the < -0.3V / > VDD + 0.3V allowed range to flow any current at all; but the meaning here (and usually there's a note to this effect) is that operation up to, whatever voltage these currents develop (perhaps 0.6-0.8V beyond the rail, typical?), is permissible so long as the current is within this rating. This may also be specified in another location (e.g. I/O characteristics rather than device abs. max.), and may be called "current injection" or something like that.Note that SMAJ size TVS are excessive for ESD purposes, but are handy if you need surge as well (very long wires?), or to shunt higher currents (e.g. as part of cross-wiring fault protection?). Not to say don't use them; they are certainly adequate for ESD purposes.Just that there are smaller and cheaper parts available.Anyway, suppose you want to limit peak current, regardless -- what then? Well, just connect a series resistor between TVS and IO pin. This limits current based on TVS's peak clamping voltage and IO's allowed ESD rating. For example, an ESD pulse clamped down to ~30V, then dropped through 10 ohms, is still less destructive than 2kV HBM -- which most devices are rated for as-is.I suppose you could cascade another protection stage (this time perhaps using schottky clamping diodes) to get the voltage fully within ratings, or very nearly, as well. But that's a lot of bother for not a lot of value.---As for the pushbutton, is ESD even a hazard? Consider using a 5-pin type where the metal cover plate can be grounded directly. ESD can never strike the signal line. There will still be some induced voltage/current due to sheer proximity, and some leakage along/through the ground plane*, but that's easily dealt with by the other components as mentioned. I might prefer more than 10nF in that case, but some combination of clamp diodes, filter cap, and series resistance (between switch and filter/protection, and protection and IO), will certainly do the job.*Assuming the existence of one as you should almost always build on a ground plane!There's also the non-solution solution: just debounce in software. I'm quite fond of a hysteretic counting method. See:https://github.com/T3sl4co1l/Reverb/blob/master/main.c#L182Poll the inputs fairly frequently (you'll usually have a periodic timer interrupt or main() loop cycle of some ~ms where this can be placed), and every time it reads 1, increment up to a maximum; 0, decrement down to a minimum. Only when the min/max has been hit, change the output state. Note this is also a digital low-pass filter, with delay and cutoff frequency equal to the number of counts, i.e. the output can toggle no faster than the slew rate of the filter allows. This can be used to drive functions like hold-and-repeat (as shown), double-click, etc.If using a keyboard matrix, just perform a complete scan, and then evaluate each position in this way. Whether you want to do one step of a scan per tick, or a full scan quickly during a tick, is up to you; obviously you get less bandwidth in the former case (you don't get a full scan until N ticks have elapsed). Also you can sacrifice synchronicity (i.e., poll and debounce each column of the matrix per pass) to distribute the work more evenly over time.Anyway, the first time I developed this algorithm, I took a bare wire and dragged it across a rusty pitted plate. Contact and release were read quite comfortably despite the noise.Probably the worst thing you can do is use pin change interrupts: worst case, radio interference spams the interrupt, throwing the CPU back into the ISR almost as soon as it returns. You definitely need analog filtering if you're using this approach.Tim

author: Veketti
author_rank: Regular Contributor
timestamp: February 24, 2023, 08:51:50 am
content: Thanks, this clears it up. Ah, SMAJ is overkill. Would you mind recommending more appropriate TVS package size/product?This ESD/EMF/whatever is interesting. I have one device which I designed few years back which has PCB inside plastic case where there is button to wake it up. PIC microcontroller and the device goes to sleep after few minutes. Wakes up with button push interrupt. I have noticed that if I drag plastic bag or similar over the device it wakes up. I assume it will induce (negative?) voltage to the case internal button wires and wake it up. Or something else magic is happening.I didn't at that time have any capacitor - resistor bypass filter on the input, but do you think that would have prevented this issue? I might just revisit that design If I knew how to prevent it from happening.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: February 24, 2023, 12:54:00 pm
content: Yes, static does the same thing, it's EMP. Albeit very small, but still, a very sharp change in voltage, which can couple into wires and traces, and even small discharges like those can damage components if up close (but more likely just being weird, as with your observation).Bypass caps to lower the trace impedance and give more charge capacity (...imagine that!) help a lot, yeah. Debouncing too, since it's only a momentary disruption.Typical parts: zillions; shop around and find what you need. Offhand, I've got in my library CPDT-5V0U-HF, D1213A-04S-7, NUP4202W1T2G, BAV99, BAT54S, PGB102ST23WR (24V, good for CANbus), etc. A couple of these might be poorly available or obsolete now, I haven't checked.Tim

author: Veketti
author_rank: Regular Contributor
timestamp: February 25, 2023, 09:01:25 am
content: Hopefully I'm not going too much off topic here, but I noticed following thing on my STM32 discovery IOT kit schematics. I understand the purpose of all the other components, but I don't understand what's the purpose of this 10pf capacitor. Once the button is pressed it will be directly shorted to ground. Could someone explain, what's the purpose of it?

author: T3sl4co1l
author_rank: Super Contributor
timestamp: February 25, 2023, 09:30:26 am
content: Beats me. It's too small for RFI immunity even (and there's filtering already), and waaay too small for wetting current.Tim

author: pcprogrammer
author_rank: Super Contributor
timestamp: February 25, 2023, 10:29:26 am
content: It delays the rising edge, but why they did it, beats me too. If I calculated correctly RC time 1 micro second. (100K x 10pF)The low pass filter ((1K + 100K) x 100nF) has a way bigger effect on it.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: February 25, 2023, 01:40:53 pm
content: Even less than that: the initial step is the 1/101 resistor divider, at a Thevenin resistance of 0.99k. 1k * 10p = 10ns time constant, like I said, not even suitable for RFI immunity (30MHz+). Well, Fc is exactly 15.9MHz so maybe, but that's not many dB by 30MHz. And again, the 100n dominates.Tim

author: Siwastaja
author_rank: Super Contributor
timestamp: February 25, 2023, 01:49:21 pm
content: Cargo cult engineering. The fact we are discussing it here seriously proves the designer succeeded very well. Designer even added a comment about the placement of that capacitor. Looks very professional.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: February 25, 2023, 04:56:08 pm
content: Quote from: Siwastaja on February 25, 2023, 01:49:21 pmEmphasis added?Hmm, engineering as postmodern performance art?Feels very apt in these *GPT days...Tim

author: floobydust
author_rank: Super Contributor
timestamp: February 26, 2023, 02:46:40 am
content: I find all of these input protection circuits so far, are lovely EE theory but in practice are terribleProblem is the ESD hit is simply too much voltage (drop) for the small 1kΩ input resistor and it will simply arc across it or at best to something nearby.So it's an EE myth the little input resistor can drop say 10kV, we need to stop making that assumption.With luck ESD usually arcs to the nearby ground pour or traces instead. I admit to using an ESD gun in the dark, to find out why a hit on an input zapped an unrelated circuit - it was a bad PCB layout and the input trace had a path near another trace and arced to it.In practice, the input cap is placed right at the input terminal. Pic related I'm using 4.7nF 100V but 10nF can work. This greatly attenuates the ESD impulse in the first place, to under 500Vpk (ISO10605 330pF/2kΩ contact into 10nF) which is then quite manageable by the components downstream. 10nF I think was ~200Vpk with 150pF/330Ω 15kV.Placing the input cap right at the switch/connector/terminal block also shunts RF enough to pass BCI tests. It depends somewhat on the cable/wiring, if the switch is far away.Then an RC into the MCU like 33k-47k/33nF for filtering. I rely on the MCU's diodes if their injection current rating is decent, or add double Schottky or a real ESD protection diode/zener.You can also consider the instance of an installation error where somebody wires the input terminal to 12V/24V - you don't want to burn up the input resistor or push enough current to +VDD that the rail goes up.Pic shows having a large cap first greatly lowers the ESD impulse, assuming it's correctly placed close to the ingress point. The ST diagram with a 10pF cap is hilarious. Totally useless.For S/W debouncing, for years I used the simple shift-register approach so 16 pin-sample and shifts before all bit values identical. But it's crap, any single bit error of noise makes it indeterminate (bouncing) for 16 more samples, so I no longer use that. The S/W integrator approach is working great.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: February 26, 2023, 07:09:29 am
content: That's why I'm not concerned: the 10nF helps, though I'd rather use 100nF (and maybe a smaller pullup to maintain input rate, though 10ms is still more than plenty for HMI). Worth noting a resistor between cap and I/O will reduce pin current, since the cap only does so much clamping by its value alone, has a very low transient impedance (so another low impedance in parallel with it, like clamp diodes, can still draw a ton of current), and will always have significant (10s V) residual peak voltage due to ESL. So maybe use 100k pullup, 100R series to limit wetting current, 100nF shunt to debounce/filter, and 100R series to I/O to limit ESD current. If you have a lot of switches to debounce (but not so many you need a matrix), consider resistor arrays for the 100's. 100k can also be dropped in favor of internal weak pullups (typically 20-60k).When isolation is available, small capacitors can be used -- this is the purpose of the 1nF 1.5kV cap on the Ethernet interface (between cable termination and chassis if available, circuit ground otherwise). High isolation Ethernet transformers are available, but they do have degraded specs, and the easiest way to deal with ESD is to swamp it with a small cap like this.

author: Veketti
author_rank: Regular Contributor
timestamp: February 26, 2023, 10:46:56 am
content: I was planning to use 10k pullup resistor as all the PIC examples are using that values. Alsohttps://learn.sparkfun.com/tutorials/pull-up-resistors/allsay that the pullup resistor value should be 1/10th smaller than the input impedance of the input pin. I haven't found input impedance value from PIC datasheets so I assume it is ~100k. Is this true or would 100k pullup still work?Then another guestion regarding this protection. In battery powered devices which doesn't have wired strong ground where the voltage spikes could be directed, how the protection actually works? Voltage spike will be conducted to the minus terminal, but still within the device.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: February 26, 2023, 12:12:15 pm
content: Pin impedance is a lot higher than that. You're looking for input bias or leakage current. If you figure say 1uA (max, at Tj(max)), out of a 3V supply, and need to stay within 30/70% of supply or 0.9V, that's 0.9V/1uA = 900kohms. A 1M would be close enough, though it would technically fail at the worst-case process corner and max temp -- albeit rarely.So, 100k is fine, or use the internal pull-up (if available), or 470k or even a bit more is still fine really.Mind to add any leakages from any other components connected to it! The required value may come down depending on TVS, capacitor, etc.Note that you can save on supply current by only turning on the pull-ups before polling the buttons. This might not help much with a long time constant (~ms), but with minimal capacitance and software debounce, it'll be effective. Or for a button matrix, that's...basically how you have to do it regardless. (Probably doesn't matter any in practice, but maybe if you have something where the buttons could get stuck, or, I don't know, say it's a remote control that's prone to being sat on, I don't know(!?) -- you might want to avoid the current consumption of the held buttons.) (Of course you still need static bias on whatever button wakes the device from sleep.)In contrast, when supply consumption is not an issue, and when noise immunity is, using a lower resistance helps sink noise currents upon that trace. The capacitor helps more, but the resistor still helps out at low frequencies where the capacitive reactance is large.This can also be relevant on something like I2C bus, which shouldn't be ran over long distances in the first place -- but in the event you need to, staying on the low side of allowed pull-up resistance can improve immunity just a little bit, and maybe that's just enough to get it passing.But for I2C, the bus impedance is pretty well defined -- well, it's not, that's the problem with I2C, really, but in the sense that it's traces on a PCB (orrarelywires in a cable -- hopefully, a shielded one!!), you've got a transmission line impedance in the ballpark of 100 ohms, so you'd hope for pull-up(s) in the same order of magnitude, to terminate the line a bit. But allowable values aren't so small (the open-collector drivers don't pull down hard enough to handle that heavy of a load), so you get only modest assistance from the couple-kohm that are allowed. Still, it's not nothing.Tim

author: f4eru
author_rank: Super Contributor
timestamp: February 26, 2023, 06:42:52 pm
content: Quote from: Veketti on February 25, 2023, 09:01:25 amIt could be to clean the switch contacts.If a switch does not commutate real current, the contacts can quickly just accumulate oxide, until the switch fails to close.10p seems too low though.

author: shapirus
author_rank: Super Contributor
timestamp: February 26, 2023, 07:01:44 pm
content: Quote from: f4eru on February 26, 2023, 06:42:52 pmThis is interesting. Can you elaborate on the idea behind this? How does it work? And what should the value of the capacitor be?

author: floobydust
author_rank: Super Contributor
timestamp: February 26, 2023, 11:54:00 pm
content: Quote from: f4eru on February 26, 2023, 06:42:52 pmI think it's a theory that a capacitive discharge through switch contacts can act as wetting current. Most contacts slide a bit and you want the entire strip wetted not just the initial contact area. Calculate the energy stored 5V/0.1µF is a microjoule. You really need to run several mA through switch contacts, unless they are self-wiping, for wetting.Quote from: T3sl4co1l on February 26, 2023, 07:09:29 am1kΩ (0603) goes open-circuit after an ESD hit. Did not try 100Ω.There are specialty MLCC's specifically for (swamping) ESD protection AEQ200, that don't degrade with ESD hits. Kemet gives ratings i.e. a 50V cap takes 25kV hit OK. I don't know why the larger parts have no spec, either they exceed the top limit... or there is a penalty with using higher capacitance parts?