How to De-Bounce Switches on the Arduino - Ultimate Guide to the Arduino #21

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the three in one smart car and iot learning kit from Sun founder is a Hands-On all included Electronics kit that is perfect for anyone who wants to learn how to master the Arduino the kit comes with an Arduino 22 different sensors and modules thread boards jumper wires and everything else you need to build a bunch of fun and interesting projects learn about robotics by building a remote controlled smart car that can be controlled with an infrared remote controller or drive on its own and avoid obstacles or Follow The Line learn about the internet of things with a project that lets you monitor the temperature humidity and light level of a room from an app on your smartphone [Music] and build a plant monitor that tracks the temperature humidity light intensity and soil moisture and displays it on your smartphone so you can keep your plants water remotely it's a super cool kit and I had lots of fun building all the projects in it so click the link in the description below to order the kit from some founder foreign [Music] switch balance is a problem with almost all mechanical switches when a switch bounces the Arduino is momentarily unable to tell if the switch is open or closed and that can introduce errors in your project debouncing can be done in either Hardware or in the sketch we'll take a look at both ways in this video but first let's see what a bouncing switch looks like a count up timer each time the button is pressed the sketch should count up from zero one digit at a time this is a tactile push button there's a pull-up resistor leading to the 5 volt pin and a jumper wire leading to the input pin pin 7. the other side of the button is connected to the ground pin so when the button is pressed the signal of pin 7 should go from high to low and that'll trigger the sketch to count up one digit and print the count to the serial monitor the code for the sketch in a little bit but for now just watch what happens when I press the button see how it counts up multiple times with each button press each button press should only increase the count by one but it's going up multiple digits each time this is caused by switch bouncing in order for a microcontroller to detect when a switch is open or closed it has to constantly listen to or pull the switch to see when the signal changes when the button is pressed the metal contacts inside the button don't make an instant electrical connection the button might hit one side of the contacts then the other side several times before making a reliable connection there could also be bumps or dirt on the metal contacts that prevent a good contact right away this causes the signal to bounce up and down before settling on a resting state the bounces might only last for a few milliseconds but the Arduino runs so fast that each one can be counted as a button press if we look at the signal from the button on an oscilloscope this is what a clean non-bouncing signal would look like the signal starts low at 0 volts then goes High to 5 volts and this is what a bouncing signal looks like the signal starts low but bounces up and down between 0 and 5 volts before settling on 5 volts the time scale here is on one millisecond per division so this is happening very fast and even though the bouncing only happens for a few milliseconds it's still detected by the Arduino there are a few different ways to fix switch bouncing one way is with Hardware and another way involves adding some code to your sketch first let's see how to debounce switches with Hardware then we'll see how to do it in a sketch the easiest Hardware solution is to place a capacitor across the terminals of the switch here I have a one microfarad electrolytic capacitor across the switch with the negative terminal leading to the ground side of the switch ER works with a pull-up resistor to form an RC circuit RC circuits take a brief amount of time to charge and discharge and that Smooths out the transition between low and high voltage States which filters out the bounces on an oscilloscope the signal from a switch debounce with the capacitor looks like this see how the signal is curved up like that that line follows the charge curve of the capacitor the size of the capacitor will affect the responsiveness of the switch larger capacitors take longer to charge so it'll take longer for the switch to reset to its resting state smaller capacitors will make the switch more responsive but may not filter out all of the bouncing let's see how this works on the button counter from earlier I've inserted a one microfarad electrolytic capacitor across the switch so it's working pretty well it's not jumping up like it used to the only problem with this method though is the curve in the low to high transition microcontrollers like to have really sharp transitions so debouncing with only a capacitor might cause problems in some situations luckily there's a better way to debounce that will keep the sharp transitions to do that we'll be using what's called a Schmidt trigger Schmidt triggers are usually used to convert analog signals into digital signals but they can also be used to debounce switches a Schmidt trigger takes an analog signal as the input and outputs a digital one when the input to the Schmidt trigger exceeds an upper voltage threshold the digital output switches to high the output doesn't switch low until the input signal Falls below the lower voltage threshold a Schmidt trigger will take that smooth curve created by the debouncing capacitor and turn it into a nice sharp edge the Schmidt trigger I'll be using is the sn74 hc14n from Texas Instruments the sn74 hc14n actually has six separate Schmidt triggers the inputs are here and the outputs are here 's the PIN for VCC and the PIN for ground let's see how to use this in our button circuit from before since I need several Power connections I've run the 5 volt and ground wires from the Arduino to the power rails of the breadboard the switch is wired the same way it was in the last circuit you still need the pull-up resistor and capacitor across the switch but instead of connecting the output of the switch pin to pin 7. it gets connected to the input of one of the Schmidt triggers you can use any one of the Schmidt triggers but I have it connected to the first one here then the output of the Schmidt trigger gets connected to pin 7 of the Arduino all of the unused inputs are connected to ground to prevent floating pins the VCC pin of the chip connects to the positive power rail and the ground pin gets connected to the ground rail okay so I have everything wired up here I'm going to use the same sketch we used before all right so it's working the switch doesn't seem to be bouncing at all it looks like it works just as well as having a capacitor across the switch but the important difference is now the edges of the high to low transitions are much sharper okay now let's see how we can solve the bouncing problem with programming software debouncing has the advantage that no extra components are needed but it does add complexity and increases the size of your program you'll get the best results by debouncing switches with Hardware but let's look at how to do it in software anyway in software to bouncing the program listens for the high to low transition of a button press when the first transition occurs a timer is started if another high or low transition occurs within a short amount of time it'll be ignored first let's take a look at how the button counter sketch Works without software to bouncing this is the same button counter sketch we used before at the top we Define the input pin pin 7. then we have a variable called counter which will be used to hold the pin number printed to the serial monitor next we have a variable called button state this variable keeps track of the current voltage state of the button either high or low if the button is pressed the button State variable will hold a low value and when the button isn't pressed it'll hold a high value we also have a variable called last button state to hold the previous state of the button down in the setup function we set the input pin as an input and initialize the serial monitor in the loop function we take a digital read from the input pin and store it in the button State variable then we have this if statement that says if button state does not equal a last button state so in other words if the button state has changed since the last time through the loop if the button state is the same as the last button State we know that nothing changed and the button wasn't pressed but if the button state did change and the button was pressed the program will enter this block of code where there's another if statement this is a nested if statement if button State equals low then counter plus plus so in other words if the state of the button changed and the button state is low then increment the counter variable by one then we print the value of the counter variable to the serial monitor at the end of the loop we set the last button State variable equal to the button State variable when we loop back up to the top we digital read the input pin again and save that as the button state if the button state is changed the program will enter the if statement increment the counter and print it to the serial monitor if not it Just skips over the if statement and loops around waiting for the button state to change now let's see what this sketch looks like with some software to bouncing added to add debouncing to the sketch we need to start a timer when the first input signal change occurs if the signal bounces within a certain amount of time it'll be ignored only if the signal change persists for a set amount of time will it be counted as a button pressed for example if the signal goes High three times within 50 milliseconds the sketch will ignore it and only count the signal after 50 milliseconds up at the top of the sketch we have the same variables we declared in the previous sketch we just need to add a few more variables for debouncing the first variable current button state will hold the reading from the input pin instead of the button State variable like we saw in the last sketch the last debounce time variable will store the time after the initial signal change is detected the debounce delay variable sets the length of time in milliseconds that Sigma bounces should be ignored the setup function is the same as in the sketch without debouncing in the loop function we take a reading from the input pin and store it in the current button State variable then we get to an if statement that asks if the current button state is not equal to the last button State enter this block so if the input signal has changed since the last time through the loop it will enter the if statement inside here we have last debounce time equals Millis the milliseconds function outputs the number of milliseconds that have passed since the Arduino was turned on so this line will store the time of the first input signal change in the last debounce time variable then we get to another if statement this statement says if the current time minus the last debounce time is greater than the D bounce delay enter the if statement the milliseconds function is constantly increasing but the last debounce time is the time when the initial signal change occurred so Millies minus last debounce time is the number of milliseconds from the first signal change to the next one if that is less than the D bounce delay this block will get skipped over if it is greater than the D bounce delay the code in here the code that increments the counter will get executed for example say we press the button and have a signal bounce that lasts for five milliseconds the last button state was high and the current button state is low so the current button State and the last button state are not equal so the program enters here say 10 000 milliseconds have passed since the Arduino is turned on the milliseconds function outputs ten thousand and that's stored in the last debounce time variable down here milliseconds is still ten thousand and last debounce time is ten thousand so this is zero zero is not greater than fifty so this is skipped at the end of the loop the last button state is set equal to the current button state the signal is still low since this is happening very fast the loop Cycles through multiple times in one millisecond going back to the top the input pin is still low so the current button state is still low so current button state is equal to last button state and this if statement is skipped now say only one milliseconds has passed this equals 1001 minus ten thousand which is one and not greater than 50. so this is skipped as well this loops around and around when the five milliseconds passes and the bounce is over the reading at the input pin is now high again so high value is stored in the current button state therefore current button state does not equal last button state so the program enters this block and the timer is reset it's only until a signal stays low for greater than 50 milliseconds that the program will enter this block in here we just have the code that increments the counter and prints the result to the serial monitor as we saw in the previous sketch in the next video we're going to continue learning about input devices and see how to connect keypads to the Arduino [Music] Sun founder is my go-to source for sensors modules and other parts for the Arduino and Raspberry Pi they have a huge selection of stem Robotics and iot kits and lots of useful sensors and modules every product has an online tutorial with wiring diagrams and example code they also offer free shipping on all orders with no minimum give them a try at www.sunfounder.com next time you need to order some parts
Info
Channel: Circuit Basics
Views: 6,021
Rating: undefined out of 5
Keywords: arduino, raspberry pi, circuit projects, electronics projects, circuits, circuit basics, arduino projects, raspberry pi projects, IOT, robotics
Id: psLDNvs9dkk
Channel Id: undefined
Length: 17min 51sec (1071 seconds)
Published: Wed Apr 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.