Simple Switch Debouncing on ESP8266 / ESP32
by Phil Bowles in Circuits > Software
4496 Views, 4 Favorites, 0 Comments
Simple Switch Debouncing on ESP8266 / ESP32
Hardware
The code works on any ESP8266 or ESP32. Plus of course you will need one or more buttons / switches.
Also an LED will allow you to see what your switch is doing.
Software
Understanding the Problem
Many beginners assume that when you press a simple switch it goes on and then goes off when you release it - seems obvious, right? However, because the contacts are made of springy metal, rather than going "click" they often go "boi-oi-oi-oi-ng" and can switch very rapidly on and off many times before settling in the final state.
This phenomenon is known as "switch bounce" and is discussed in extreme detail here . The short version is if you hook the switch directly to your code and do not have any "debouncing" code, you will get get multiple "phantom" presses every time you click it. These can range from none at all (if you are lucky!) to dozens. During testing I had one switch that bounced 37 times for a single press!
There are two common methods of switch debouncing:
- Hardware
- Software
Using hardware, you need external components such as capacitors, resistors and diodes etc and obviously a soldering iron and some degree of electronics knowledge, plus - it does not always work 100% unless you are an engineer (in which case, you already know all this :) Why? because not all switches bounce the same way. Even two switches of identical type from the same batch can behave very differently, so a single one-size-fits-all solution may not work for some particularly bouncy switches.
The software method is by far the simplest solution for beginners, especially if someone has already done the hard work for you and written general pupose debouncing code that will work with any switch.
Connect the Hardware
Identify the longest leg of the LED and connect it to the 220 ohm resistor
Connect the other end of the 200 ohm resistor to pin D4
Connect the short leg of the LED to GND
Connect one side of the button to D3
Connect the other side of the button to GND
Install Software
Install ArduinoIDE and the H4Plugins library
Upload the Code
Understanding the code
H4Plugins is different from other example code, for example there is no setup function or loop function: the library does all that for you as long as you always include lines 1 and 2
Lines 4 and 5 tell the code which pins you are using.
Line 7 defines the LED as an output pin: "ACTIVE_LOW" means its is ON when pulled to GND (most builtin-LEDs work this way) and it starts with being OFF
Line 8 is where the magic happens. Here we define our BUTTON as a debounced input. The value of 15 milliseconds is just a typical starting value, you need to experiment with this value (see next section)
Lines 10-14 are "glue" code that H4Plugins needs to connect the debounced input to the function onGPIO where you can then write your own code to react to the debounced button press.
Lines 16-21 are where you put your own code. In this simple example we just print the current button state so we can see in the monitor window if it is bouncing. We also set the LED to the same state as the button. If you hold the button down the LED will stay on. Release the button and the LED goes off. When you get the best value for the the number of milliseconds, this will happen once only per press / release.
So how do we find the best value?
Experiment to Find Best Value for Your Button
15 mS is only a starting point. See the graph above for what this represents. As you reduce it - say 5mS - you may see some bouncing. If the switch bounces for more than 5mS you definitely will. As you increase it, you will greatly decrease the chance of any bouncing, but you will also start to notice a lag between when you press the button and when the LED lights.
Experiment with the value until you get the lowest value (to reduce lag) that removes all bouncing. You will have to press the button many times to get the right figure as the switch bounce can appear very random: sometimes it will not bounce at all, other times it will bounce like a kangaroo on a trampoline - it all depends on how hard / fast you press and/or release it.
Have fun finding the best value for each of the switches you will use! When you have got the ideal value, you can then use the code to guarantee that it will only ever get executed ONCE per press or release.