Hardware debouncing - Page 1
Forum section: electronics / beginners

author: Lucky-Luka
author_rank: Unknown
timestamp: January 11, 2021, 12:55:27 pm
content: Hi alla) How can I change my simple debouncing circuit used with a push-button with a pull-down resistor when I use a push-button with a pull-up resistor?I will attach 2 schematics: is the handwritten one right?b) Do I need to include a 74HC14 IC to have properly debounced push-buttons?Thanks

author: wraper
author_rank: Unknown
timestamp: January 11, 2021, 01:01:28 pm
content: Just replace GND with VCC and vice versa. Capacitor GND is better to be kept as is though. It's hard to imagene what you have thought when screwed up that circuit and made something very different. Almost as randomized part positions. Also 1k Resistance for R1 is a bit too much. Something like 100 ohm is more adequate. Also Rp1 is better to be on the other side of R1, and then 1k R1 will be fine.QuoteNo.Also I would not use HW debounce at all and would do it in firmware.

author: ohms law
author_rank: Contributor
timestamp: January 11, 2021, 01:21:42 pm
content: A Schmitt trigger can do hardware de bounce - here is a good article to readhttps://mansfield-devine.com/speculatrix/2018/04/debouncing-fun-with-schmitt-triggers-and-capacitors/

author: wraper
author_rank: Unknown
timestamp: January 11, 2021, 01:29:02 pm
content: Circuit

author: wraper
author_rank: Unknown
timestamp: January 11, 2021, 02:00:23 pm
content: Quote from: ohms law on January 11, 2021, 01:21:42 pmMost MCUs, for example AVR, already have inputs with Schmitt trigger/ hysteresis.

author: ratatax
author_rank: Regular Contributor
timestamp: January 11, 2021, 02:55:10 pm
content: Any reason why you can't do it in software ? I tried hardware debounce for a commercial product and stopped using it (for encoders and buttons). Software debounce was easier, cheaper and can be changed at any point with a firmware upgrade in case you find an even better algorithm. With hardware debounce better be 100% sure it's perfect otherwise you're screwed

author: Lucky-Luka
author_rank: Unknown
timestamp: January 11, 2021, 03:03:59 pm
content: Don't be so negative about hardware debouncing.I was just exploring this opportunity.I don't think it's a sin.btw I don't know how easy it can be to handle multiple push-button using sw debouncing.

author: Lucky-Luka
author_rank: Unknown
timestamp: January 11, 2021, 03:07:42 pm
content: Quote from: wraper on January 11, 2021, 01:01:28 pmThanks for your reply but I think that part was over the top: my thought were just like yours but I've inverted the position of 2 components (I knew something was probably wrong, so I asked), period.

author: Peabody
author_rank: Super Contributor
timestamp: January 11, 2021, 03:18:17 pm
content: Both of your drawings set up a voltage divider, so when the button is pushed the voltage never gets all the way to Vcc, or ground. Wraper's drawing in reply #3 solves that problem. The attached picture shows the debounce circuit recommended in a Bourns rotary encoder datasheet.

author: Lucky-Luka
author_rank: Unknown
timestamp: January 11, 2021, 03:26:51 pm
content: I was mislead reading this tutorial:https://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/

author: Peabody
author_rank: Super Contributor
timestamp: January 11, 2021, 03:54:23 pm
content: Quote from: Lucky-Luka on January 11, 2021, 03:26:51 pmWell it looks like he does it wrong in his first circuit, but the schmitt trigger circuit has it right. In any case, with multiple buttons you could end up with a lot of parts to implement hardware debouncing. The firmware routines work quite well, are responsive and non-blocking, and can be interrupt based or polling.Neither method is absolutely foolproof if mechanical switches are involved, but if a rare missed button push, or double count, isn't the end of the world, I would usually choose software debouncing.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: January 11, 2021, 04:59:55 pm
content: The hand drawn schematic "debounce2" is not good because of the erroneous placement of the 1k resistor. The purpose of this resistor is to prevent high peak currents through the switch and therefore must be placed between the capacitor and the switch. A ceramic 100nF capacitor can deliver very high peak currents (think 10A or more) and this can wear out the switch prematurely, but the high current can also upset the power supply or GND plane of the PCB.The 10k resistor should also be best placed on the side of the switch, instead of directly to the capacitor to prevent a voltage divider from being built.The timing capacitor can either be connected to GND or Vcc, and the switch to Vcc or GND, depending on the behavior you want. Connecting the capacitor to GND is more common, but both ways are valid.Adding a schmitt trigger is beneficiary for hardware debouncing, but what's needed depends on the switch and what you connect to it.Different switches have different bounce characteristics, and this should be taken into account when designing a debounce circuit. Jack Ganssle has written an article (available in .pdf format) that describes different switches and their bouncing behavior, and it's worth reading.

author: Peabody
author_rank: Super Contributor
timestamp: January 11, 2021, 05:27:27 pm
content: Just to complete the idea about software debouncing, in case you want to try it, here is some Arduino polling code I use for a pushbutton that is active low. It immediately recognizes the press, but requires time to pass in the released state, with no bouncing, before it will recognize another press. This works quite well since the switch is unlikely to bounce much when being released and the contacts are moving away from each other. The code is responsive and non-blocking. The 100mS countdown time can be adjusted depending on the characteristics of the switch, and likely can be much shorter.Code:/*This is Arduino polling code for pin D4 as button A, low when pressed.It reacts immediately to a button press, but then requires the buttonto be released continuously for 100mS before another press is recognized.*///in setup:int newCount = 100;int buttonApin = 4;int button_A_count = 0;unsigned long newMilli_A;unsigned long oldMilli_A;// in loop:if (button_A_count) {newMilli_A = millis();if (oldMilli_A_ != newMilli_A) {oldMilli_A = newMilli_A;button_A_count--;if (digitalRead(buttonApin) == LOW) button_A_count = newCount;}}else if (digitalRead(buttonApin) == LOW) {button_A_count = newCount;oldMilli_A = millis();...whatever you want to do in response to a button_A press...}// same setup and loop code for button B, button C, etc.

author: Lucky-Luka
author_rank: Unknown
timestamp: January 11, 2021, 07:38:35 pm
content: I've found a HW debouncing circuit involving a diode.What do you think is its purpose?

author: wraper
author_rank: Unknown
timestamp: January 11, 2021, 08:18:51 pm
content: Quote from: Lucky-Luka on January 11, 2021, 07:38:35 pmTo make capacitor charge faster, circuit is completely screwed up though. It makes things worse, not better. It would make some sense if diode was connected the other way around. What you need is fast action on switch activation and slow action on release so all connection interruptions are smoothed. Switch bounces only in position when contacts are closed after all. There is no need to debounce switch in open position. Though using diode makes very little sense in any direction. All you need is that resistor between switch and capacitor has much lower resistance than pull-up resistor to get asymmetric action.

author: TimFox
author_rank: Super Contributor
timestamp: January 11, 2021, 09:12:25 pm
content: The best debounce circuit requires an SPDT switch, with each side feeding an input of a simple set-reset flipflop, which can be made from two NAND gates. The switch should not bounce between the two contacts, only bouncing when making or breaking the contact at one position. Seehttps://learnabout-electronics.org/Digital/dig52.phpfor an elementary discussion.

author: ohms law
author_rank: Contributor
timestamp: January 12, 2021, 09:32:08 am
content: Not sure about others but arduino does not have built in schmitt - software debounce coding with many inputs can be a pain

author: jpanhalt
author_rank: Super Contributor
timestamp: January 12, 2021, 09:39:17 am
content: @TimFoxTrue (See also: Ganssle), but how do you apply that to the ubiquitous push button switch?

author: wraper
author_rank: Unknown
timestamp: January 12, 2021, 10:46:13 am
content: Quote from: ohms law on January 12, 2021, 09:32:08 amAVR based arduino certainly has schmitt trigger, you cannot take schmitt trigger away from MCU.

author: ohms law
author_rank: Contributor
timestamp: January 12, 2021, 11:51:42 am
content: Humble apologies it does indeed have on the digital pins. most of the de-bounce is due to the external switches - the 2 options one being software and the other being hardware to deal with de-bounce input.https://www.radishlogic.com/arduino/arduino-uno-schmitt-trigger-voltage-levels/

author: TimFox
author_rank: Super Contributor
timestamp: January 12, 2021, 02:53:02 pm
content: Quote from: jpanhalt on January 12, 2021, 09:39:17 amIt obviously doesn't work with an SPST push button, as shown in the various examples with R-C networks, but SPDT push buttons are available. It depends on what the poster's options are for his design.

author: rstofer
author_rank: Super Contributor
timestamp: January 12, 2021, 06:30:14 pm
content: Not only does the ATmega328P (Arduino chip) have a Schmitt Trigger, it also has the ever-popular 2 series D-flops to synchronize the input to the internal clock domain. Very nice!See diagram on page 64 here:https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdfA simple RC as given above is probably adequate as long as the RC time constant is longer than the bounce time. Bounce time is hard to measure...Here is a variation that uses a diode to speed up the charge time. I don't know that it is necessary but there is some design info given in the explanation:https://mayaposch.wordpress.com/2018/06/26/designing-an-rc-debounce-circuit/

author: jfiresto
author_rank: Frequent Contributor
timestamp: January 13, 2021, 10:20:21 am
content: Here are a couple switch debouncing circuits that Don Lancaster gives in his "CMOS Cookbook":These examples have the capacitor clean the contacts of a higher current switch. If you want to be fastidious, you could insert a small resistor between the switch and and capacitor to limit the peak current to the switch's maximum rated switching current – while you are considering ESD.

author: wraper
author_rank: Unknown
timestamp: January 13, 2021, 12:38:38 pm
content: Quote from: jfiresto on January 13, 2021, 10:20:21 amHe does not know what he is doing.He both damages the contacts by not limiting discharge current, and makes circuit prone to failures dues to very high pull-up/down resistance. Switches, especially TACT type, often become somewhat leaky, so using 220k pull up/down is simply stupid.

author: nigelwright7557
author_rank: Frequent Contributor
timestamp: January 13, 2021, 12:48:18 pm
content: I usually de-bounce key(s) in software.You can do it i nan interrupt routine or if the software has nothing else to do it in main code.

author: Zero999
author_rank: Super Contributor
timestamp: January 13, 2021, 01:18:01 pm
content: Quote from: wraper on January 13, 2021, 12:38:38 pmThose circuits are clearly designed for a switch which can take a resonable current. 5nF charged to 5V is only 62.5nJ, which most switches can handle with no problem. If the switch is so leaky, that a 220k pull-up/down is an issue, then there's a problem with moisture ingress. And it you read the full post, you'll find a mention of using a series resistor to limit the current was made.

author: wraper
author_rank: Unknown
timestamp: January 13, 2021, 01:20:20 pm
content: Quote from: Zero999 on January 13, 2021, 01:18:01 pmIt's 50n, not 5n.QuoteWelcome to the real world. Simply staying in humid climate is enough for this to happen within a few years. There are plenty TV, monitors and other electronics which failed this way.QuoteThat comment was not made by book author. And my comment was about book author in particular.

author: Zero999
author_rank: Super Contributor
timestamp: January 13, 2021, 02:32:00 pm
content: Quote from: wraper on January 13, 2021, 01:20:20 pmI got the decimal place wrong, but so what? 625nJ is still no problem for most switches.QuoteOnly if they're left out in the rain or used in somewhere like a kitchen, or bathroom. Normally switches tend to fail open, or go very bouncy.

author: wraper
author_rank: Unknown
timestamp: January 13, 2021, 02:50:50 pm
content: Quote from: Zero999 on January 13, 2021, 02:32:00 pmYou have no idea what you are talking about. For example in tact switches silver (contact coating) dendrites grow over plastic surface in between of contacts.https://reliatrace.com/silver-migrationThere is no need to place device in kitchen or bathroom. It may fail in living room if climate is humid in that location.

author: Zero999
author_rank: Super Contributor
timestamp: January 13, 2021, 03:12:40 pm
content: Quote from: wraper on January 13, 2021, 02:50:50 pmNo you're the clueless one. The original poster is using a push-button, not a membrane switch. The risk of that happening on a proper switch, with gold plated, or even plain old copper contacts is close to zero. Any dendrites will be destroyed as the contacts open and close. Unless you go stupidly high, i.e. >1M, there are two main issues with using a high value resistor: 1) noise and 2) the wetting current being too low, but both can be mitigated by using a decent sized capacitor.

author: wraper
author_rank: Unknown
timestamp: January 13, 2021, 03:19:42 pm
content: Quote from: Zero999 on January 13, 2021, 03:12:40 pmDo you have troubles understanding what I wrote, what TACT switch is (the most common pushbutton), and what type of plating they use? Also by construction it's basically a membrane switch with plunger attached.QuoteHow many switches have gold plated contacts, like <1%? Among switches used in consumer electronics you likely to find that only in some microswitches. Plain copper is much worse than silver plated, but for other reasons.

author: Zero999
author_rank: Super Contributor
timestamp: January 13, 2021, 05:35:32 pm
content: Quote from: wraper on January 13, 2021, 03:19:42 pmA tactile switch is nothing like a membrane switch. Yes, some might be silver plated, but the mechanical action of the surfaces moving will get rid of any whiskers. The circuit posted above is also good for removing whiskers because they will be vapourised by the capacitor discharging through them.Irrespective of the above: using a lower value resistor wouldn't help, unless it's stupidly low, because silver is an excellant conductor.QuoteGold plating is very common for switches carrying small signals. Yes, copper isn't good for signal, because it oxidises, so requires a relatively high wetting current, but the capacitor will help with that.

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 13, 2021, 05:44:26 pm
content: I'll never use another tactile/button/anything without Schmitt's stuff...https://www.ti.com/lit/ds/symlink/sn74lvc1g17.pdf?ts=1610548621855

author: wraper
author_rank: Unknown
timestamp: January 13, 2021, 06:11:34 pm
content: Quote from: Zero999 on January 13, 2021, 05:35:32 pmDendrites grow over plastic in between of contacts and are never touched by anything. You apparently have no idea about switch construction. There are no whiskers.QuoteIt will help tremendously. Faulty switches I measured usually had several tens to hundreds of kOhm resistance. Again these are not a whiskers, which BTW do not appear on silver without reaction with sulfur.

author: Zero999
author_rank: Super Contributor
timestamp: January 14, 2021, 10:44:58 am
content: Quote from: wraper on January 13, 2021, 06:11:34 pmThe picture you've shown, is nothing like a membrane switch.I've never seen a switch fail in that manner. Normally the springy metal dome fails, so the contacts don't make. I'm not saying it doesn't happen, it doesn't seem to be common.Dendrite is just another name for metalic whisker. The problem you're describing is not unique to switches. It applies to PCBs, made with lead free solder.https://www.google.com/search?client=firefox-b-e&q=lead+free+solder+dendrite+growthChanging the circuit design will not help. If you're paranoid, use leaded solder and switches with gold plated contacts. The amount of gold is tiny, so they're not that much more expensive.https://www.tme.eu/gb/details/ksc223jlfg/microswitches-tact/c-k/ksc223j-lfg/https://www.tme.eu/gb/details/kms233glfg/microswitches-tact/c-k/y28b23310fp/

author: wraper
author_rank: Unknown
timestamp: January 14, 2021, 11:22:40 am
content: Quote from: Zero999 on January 14, 2021, 10:44:58 amIt's nothttps://en.wikipedia.org/wiki/Dendrite_(metal)https://en.wikipedia.org/wiki/Whisker_(metallurgy)QuoteYou took like 10th result that Google showed you, and proclaimed that all results before it are incorrect. You picture is for a less commonnon-tactilemembrane switch. Not to say your point is moot, because membrane switch was only listed as one among the more general things in article I linked. And you saw it as point to nitpick. "commonly occurring inprinted electronics, microelectronics, components, PCB assembliesand membrane switches"QuoteThanks for your judgment, oh God of engineering. For some strange reason, as I already said, my measurements in faulty devices showed completely opposite.QuoteOnly like 5-10+ times more expensive for tact switches.

author: janoc
author_rank: Super Contributor
timestamp: January 14, 2021, 11:58:25 am
content: I wonder why nobody has posted this here yet:https://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/https://hackaday.com/2015/12/10/embed-with-elliot-debounce-your-noisy-buttons-part-ii/https://hackaday.com/2010/11/09/debounce-code-one-post-to-rule-them-all/And Jack Ganssles's treatment of the subject:http://www.ganssle.com/debouncing.htmhttp://www.ganssle.com/debouncing-pt2.htm

author: Zero999
author_rank: Super Contributor
timestamp: January 14, 2021, 03:29:50 pm
content: Quote from: wraper on January 14, 2021, 11:22:40 amWell when I Googled membrane switch construction, the first image I got showed a switch constructed like a PCB, but to be fair, your image did appear further down the page.I concede your points about dendrites vs whiskers, but whiskers on PCBs seem to be definitely more of an issue, than dendrites on tactile switches and how would using a 10k resistor, rather than a 220k resistor help?Google knows virtually nothing about dendrites on tactile switches, but lots about whiskers on PCBs.https://www.google.com/search?q=tactile+switch+dendrite&client=firefox-b-e&hl=en-US&ei=bGIAYKOSMtLrgAbN1KaQBw&start=10&sa=N&ved=2ahUKEwij-OPu3ZvuAhXSNcAKHU2qCXIQ8NMDegUIEhCHAw&biw=1280&bih=882https://www.google.com/search?client=firefox-b-e&ei=ImMAYK-ALYGR8gLc056YBg&q=whisker+PCB&oq=whisker+PCB&gs_lcp=CgZwc3ktYWIQAzIICAAQBxAKEB4yAggAMgYIABAIEB4yBggAEAgQHlDkKliyMGDtM2gAcAF4AIABlwGIAdEDkgEDMS4zmAEAoAEBqgEHZ3dzLXdpesABAQ&sclient=psy-ab&ved=0ahUKEwivmcPF3pvuAhWBiFwKHdypB2MQ4dUDCAw&uact=5Just because you might have experianced the problem, because you bought cheap, Chinese switches, or shoddy equipment, it doesn't mean it's an industry wide issue.

author: wraper
author_rank: Unknown
timestamp: January 14, 2021, 04:10:56 pm
content: Quote from: Zero999 on January 14, 2021, 03:29:50 pmEasy. Say bad switch has 50k resistance. With 220k resistor device will fail. Not just non-working button, but completely inoperable as it will be detected in pressed position all of the time. With 10k it continue to work just fine since divided voltage will be above input threshold of IC.QuoteI've seen this fault in TV, monitors and other equipment from reputable brands.

author: Zero999
author_rank: Super Contributor
timestamp: January 14, 2021, 07:12:17 pm
content: Quote from: wraper on January 14, 2021, 04:10:56 pmIf your switches are failing due to silver dendrites, then reducing the resistance to 10k will not help. Silver has a resistance of only 1.59×10−8Ω·m, so even if the dendrite is a couple of nm wide and 1mm long, it will have a lower resistance than that.It's more likely you're having problems with humidity/moisture ingress, especially if there's lots of flux left on the board.

author: wraper
author_rank: Unknown
timestamp: January 14, 2021, 07:19:44 pm
content: Quote from: Zero999 on January 14, 2021, 07:12:17 pmI wrote actualmeasured resistanceof such faulty switches. And it can be seen in microscope as trace over plastic in between of contacts, no flux or anything. I think I posted photos on this forum but it was a long time ago and will be very hard to find.

author: Zero999
author_rank: Super Contributor
timestamp: January 14, 2021, 07:43:27 pm
content: Quote from: wraper on January 14, 2021, 07:19:44 pmThat may be true, but something else must at play. You'll measure a much lower resistance, than 10k, if the contacts are shorted by a piece of metallic silver, especially if it's large enough to see using an optical microscope. Do the maths.

author: wraper
author_rank: Unknown
timestamp: January 14, 2021, 08:05:34 pm
content: It looks like semi transparent deposition, so there is very little metal.

author: Zero999
author_rank: Super Contributor
timestamp: January 17, 2021, 06:43:57 pm
content: Quote from: wraper on January 14, 2021, 08:05:34 pmThat image doesn't load for me. I imagine impurities would explain why the resistance is higher than expected. I wonder how much extra life a lower value resistor would give? Did you try it?

author: Lucky-Luka
author_rank: Unknown
timestamp: January 27, 2021, 08:55:48 am
content: Quote from: janoc on January 14, 2021, 11:58:25 amHave you really read the thread? Both sources were mentioned...

author: Lucky-Luka
author_rank: Unknown
timestamp: January 27, 2021, 08:58:45 am
content: Who would have thought that WW3 could start because of HW debouncing?!

author: janoc
author_rank: Super Contributor
timestamp: January 28, 2021, 11:11:16 am
content: Quote from: Lucky-Luka on January 27, 2021, 08:55:48 amYes, I did - and you likely didn't. Or think that a reference like "(See also: Ganssle)" will help someone who isn't familiar with Jack's work to find the info. The same with the Hackaday links - only the first one was posted and specifically the last one from 2010 is interesting because it shows different ways of doing software debouncing.

author: Lucky-Luka
author_rank: Unknown
timestamp: January 28, 2021, 12:25:18 pm
content: C'mon, don't embarrass yourself...Bye

author: gnuarm
author_rank: Super Contributor
timestamp: January 28, 2021, 08:43:59 pm
content: Interesting that no one has addressed the issue of operation as the board powers up. With the cap in the circuit it will take an RC time (approx) to charge up if that is the resting state of the circuit. So an MCU that comes out of reset quickly may see an initial (phantom) button press at start up. This happens if the switch is across the cap, discharging it when pressed. That is a reason to use the diode perhaps. However, if the switch is on the side of the circuit that charges the cap when pressed this is not an issue.I've seen these conversations rage on and on arguing over many configurations and many circuit and operational details. There is no one answer to the problem. They all have limitations or require extra parts or function better or worse. Choose your poison.

author: Kerlin
author_rank: Regular Contributor
timestamp: January 28, 2021, 09:13:20 pm
content: Quote from: Lucky-Luka on January 27, 2021, 08:58:45 amWife:- "Come to bed its very late and you have to go to work tomorrow"Husband:- "I can"t because some one on EEVblog is wrong"

author: TimFox
author_rank: Super Contributor
timestamp: January 28, 2021, 09:15:42 pm
content: Quote from: gnuarm on January 28, 2021, 08:43:59 pmThat can also be a problem with the R-S flipflop/SPDT switch circuit if the switch is momentary in both directions. However, if the switch has no center position and is not momentary, the power-on state is well-defined by the position of the switch on power-up.

author: janoc
author_rank: Super Contributor
timestamp: January 29, 2021, 08:42:09 am
content: Quote from: Lucky-Luka on January 28, 2021, 12:25:18 pmIn other words, no arguments so we resort to ad-hominem.

author: Zero999
author_rank: Super Contributor
timestamp: January 29, 2021, 02:40:29 pm
content: Quote from: wraper on January 14, 2021, 08:05:34 pmIt looks like a Lichtenberg figure. Very weird. I've not seen one of those on a switch before.

author: nigelwright7557
author_rank: Frequent Contributor
timestamp: February 23, 2021, 12:45:49 am
content: Quote from: Lucky-Luka on January 11, 2021, 03:03:59 pmSoftware de-bouncing is easy.If switch is active low.Look for first low.Then starting looking for a high every 1ms.Once its high for 20 cycles assume switch no longer pressed.Been doing it that way for 40 years !

author: coppice
author_rank: Super Contributor
timestamp: February 23, 2021, 12:52:30 am
content: Quote from: Zero999 on January 29, 2021, 02:40:29 pmRight. Has that switch suffered a major ESD event?

author: wraper
author_rank: Unknown
timestamp: February 23, 2021, 01:03:06 am
content: Quote from: coppice on February 23, 2021, 12:52:30 amI don't think it a switch, just something else with the same silver dendrite growth problem. I found this picture on internet. I should have photos of actual faulty switches I made years ago but could not find them after searching for 20-30 minutes. It has nothing to do with ESD, its silver dendrites growing over time. A thread where monitors failed with the same problem:https://www.eevblog.com/forum/repair/repair-of-acer-h223hq-monitor/msg3475800/#msg3475800

author: nigelwright7557
author_rank: Frequent Contributor
timestamp: June 03, 2021, 03:31:09 am
content: I cant say that in 40 years of using microcontrollers i have ever used hardware de-bounce.Its just so simple to do in software and save the cost and pcb space of the extra components.