{"id":108,"date":"2023-02-10T12:47:56","date_gmt":"2023-02-10T11:47:56","guid":{"rendered":"https:\/\/portfolio.porombka.pl\/?p=108"},"modified":"2024-10-22T19:40:27","modified_gmt":"2024-10-22T17:40:27","slug":"debounce-analog-switch-contacts-noise","status":"publish","type":"post","link":"https:\/\/portfolio.porombka.pl\/index.php\/2023\/02\/10\/debounce-analog-switch-contacts-noise\/","title":{"rendered":"Debounce analog switch contacts noise"},"content":{"rendered":"\n<p class=\"has-x-large-font-size\">TL;DR<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>A bouncing effect of the analog switch button can be reduced to minimum by simple RC low pass filter which can be upgraded by diode to allow faster switching.<\/p>\n<\/blockquote>\n\n\n\n<p>When I started working on one of my projects &#8220;Pinball&#8221; I faced problematic issue with the noise generated by the analog switch. I plan to have many analog limit switches in my project so I can&#8217;t have &#8220;noisy switches&#8221;. The problem illustrates an image below:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" data-src=\"https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/switch-bouncing-effect.png\" alt=\"\" class=\"wp-image-109 lazyload\" width=\"527\" height=\"272\" data-srcset=\"https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/switch-bouncing-effect.png 527w, https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/switch-bouncing-effect-300x155.png 300w, https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/switch-bouncing-effect-116x60.png 116w\" data-sizes=\"(max-width: 527px) 100vw, 527px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 527px; --smush-placeholder-aspect-ratio: 527\/272;\" \/><figcaption class=\"wp-element-caption\">Analog switch contacts bouncing effect.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Switch has analog contacts that bounce few times before they are touching each other in stable state. That bounces &#8211; vibrations, generate a noise that we can observe above. Each time we switch ON or OF, the metal contacts bounces a little bit for some time, that we can call &#8220;bounce time&#8221;, and after that period we see stable state &#8211; switch is actually switched. We can see it below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" data-src=\"https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/switch-bouncing-effect.webp\" alt=\"\" class=\"wp-image-110 lazyload\" width=\"457\" height=\"267\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 457px; --smush-placeholder-aspect-ratio: 457\/267;\" \/><figcaption class=\"wp-element-caption\">Analog switch bouncing effect (<a href=\"https:\/\/forbot.pl\/blog\/czym-jest-filtr-rc-jak-dziala-i-co-warto-o-nim-wiedziec-id40448\" data-type=\"URL\" data-id=\"https:\/\/forbot.pl\/blog\/czym-jest-filtr-rc-jak-dziala-i-co-warto-o-nim-wiedziec-id40448\" target=\"_blank\" rel=\"noreferrer noopener\">source: Forbot<\/a>)<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Why is it a problem? <\/h2>\n\n\n\n<p>Normally when you use analog switch to turn ON\/OFF a light bulb or a motor or other analog device, it is ok. The problems appear when you want to drive a controller pin, like Arduino or, like in my project, ESP8266 Node MCU. Microcontrollers are more sensitive (TTL\/logic devices) that detects any such kind of noise. Controller will detect that bouncing as a fast switching ON and OFF of your button. It may be solved on software side, adding some delays, software debouncing, but I&#8217;m not a fan of that kind solutions.<\/p>\n\n\n\n<p><strong>First<\/strong>, that really complicates you program logic, and easy part like switch handling becomes a hard thing.<\/p>\n\n\n\n<p><strong>Second<\/strong>, I&#8217;m a fan of clean solutions, the problem is on hardware side, solve it in hardware.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">RC filter to the rescue!<\/h2>\n\n\n\n<p>To make the switch smoother I used an RC low pass filter shown below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"662\" height=\"610\" data-src=\"https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/low-pass-filter-rc.png\" alt=\"\" class=\"wp-image-115 lazyload\" data-srcset=\"https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/low-pass-filter-rc.png 662w, https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/low-pass-filter-rc-300x276.png 300w, https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/low-pass-filter-rc-65x60.png 65w\" data-sizes=\"(max-width: 662px) 100vw, 662px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 662px; --smush-placeholder-aspect-ratio: 662\/610;\" \/><figcaption class=\"wp-element-caption\">RC low pass filter<\/figcaption><\/figure>\n<\/div>\n\n\n<p>It worked great! But&#8230;after some tests I noticed another problem &#8211; when I was switching very fast, some &#8220;states&#8221; were simply skipped at OUT pin. When I started switching button ON and OFF faster and faster I got to the moment, when I was switching so fast, that it was not detected at all. The capacitor in the RC circuit was not discharged that fast, so next switch, just sustained previous state. I had to find a solution to force capacitor to discharge faster, to allow my switch to be switched ON\/OFF faster, and to still detect it correctly in my ESP8266 interrupts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final solution<\/h2>\n\n\n\n<p>The solution was to add a diode for \\(R_2\\) resistor.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"630\" height=\"581\" data-src=\"https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/low-pass-filter-rc-with-diode-1.png\" alt=\"\" class=\"wp-image-122 lazyload\" data-srcset=\"https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/low-pass-filter-rc-with-diode-1.png 630w, https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/low-pass-filter-rc-with-diode-1-300x277.png 300w, https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/low-pass-filter-rc-with-diode-1-65x60.png 65w\" data-sizes=\"(max-width: 630px) 100vw, 630px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 630px; --smush-placeholder-aspect-ratio: 630\/581;\" \/><figcaption class=\"wp-element-caption\">RC low pass filter with diode<\/figcaption><\/figure>\n<\/div>\n\n\n<p>As you can see, I increased also the \\(R_2\\) resistance to increase also the voltage on diode, to make the diode work as expected here. An effect is the smooth rise and fall as we can see below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"534\" height=\"279\" data-src=\"https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/switch-debouncing-effect.png\" alt=\"\" class=\"wp-image-121 lazyload\" data-srcset=\"https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/switch-debouncing-effect.png 534w, https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/switch-debouncing-effect-300x157.png 300w, https:\/\/portfolio.porombka.pl\/wp-content\/uploads\/2023\/02\/switch-debouncing-effect-115x60.png 115w\" data-sizes=\"(max-width: 534px) 100vw, 534px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 534px; --smush-placeholder-aspect-ratio: 534\/279;\" \/><figcaption class=\"wp-element-caption\">Debounced analog switch noise.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>I tested the circuit manually by pressing the button really fast and it was working all the time. The interrupts were triggered correctly. I&#8217;m not so experienced to tell you what is the minimum delay between  ON\/OFF switches that will be detected correctly, but it seems to be about 20-50ms, it looks like c.a. 50Hz should be supported.<\/p>\n\n\n\n<p>I hope that it may help someone, thank you for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A bouncing effect of the analog switch button can be reduced to minimum by simple RC low pass filter which can be upgraded by diode to allow faster switching.<\/p>\n","protected":false},"author":1,"featured_media":127,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,29,130],"tags":[30,32,36,31,33,34,35],"class_list":["post-108","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-electronics-en-en","category-first-in-series","tag-arduino","tag-circuits","tag-controller","tag-electronics","tag-esp8266","tag-pinball","tag-rc-filter","grid-sizer"],"_links":{"self":[{"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":11,"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":359,"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/posts\/108\/revisions\/359"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/media\/127"}],"wp:attachment":[{"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/portfolio.porombka.pl\/index.php\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}