empowering creative people

Videos / Switches, Debouncing and Arduino

Today we are going to look at the different switch types we can get, the specifications of these switches, the schematic symbols and jargon we hear when working with switches and then we will look at the real-world applications of switches and problems we encounter in making circuits that use switches. Especially when interfacing with the Arduino!

Search Videos

Related Content



Hey guys, Aiden here from core electronics. today we're going to look at the Arduino Uno and interfacing over switches we're going to cover off on what switches are and how we can use them we're also going to look at a couple of different types of switches and the important things to look out for the switches.

So, like I said some of the important things to look out for with switches can be found on this nice little diagram here. now we've got different poles and throws of a switch, so the poles refer to the amount of circuit you can control and if you look at the schematic that's representative of that and you've also got the throw which is the amount of positions that each switch can take. So, with a single throw you can see on the left here, but the single throw only has one position that are convenient so it's either this the switch is open, or it's closed. whereas this is controlling two circuits on the double throw side and it can be on for that circuit or on for that circuit and being on for one circuit means it's off for the other circuit, unless you've got a double throw circuit in which case you can sort of manage that.

I've got three switches here that we're going to take a quick look at I've got the rocker switch, which is just a on single pole single throw switch with an on/off state. I've got the total switch here which is the same sort of thing this has got the on and off state and you can essentially put these switches in series or something like that and they'll just control current flow through the circuit. I've also got this little illuminated latching push-button it's a little bit different because when you push this button down it latches into it on state and until you press it again it will stay on. The final switch that we see is this one here that I put on the breadboard and we'll take a little closer look at that later but that's essentially a momentary push button.

What we're going to do today, is interface a momentary push-button such that the LED will be controlled by the state that the switch is put into we'll write a sketch that can remember that the last time that the button was pushed and what the current state of the button is and it will be able to compare that and turn the LED on or off. So, I've got this circuit made up in accordance to our Fritzing sketch you can go on to the tutorial and see that there and it's all a pretty straightforward sketch. we've just got an LED in series with a resistor just a current limiting resistor there and that's connected to Digital pin4, of our Arduino. So, it's just connected to digital pin 4 to the LED there and then I've got the button with a pull-up resistor between its leg and ground and the control of the button is over here on digital pin 3 with the switches out of the way. now this is all a pretty basic setup and the idea of it is that when we push that button the LED will turn on and when we push the button again the LED will turn off, now a switch is a physical object that uses a mechanical means to control the circuit and what we can see with our sketch out without components actually is that when we press a button the kinetic force of the contacts touching can sometimes bounce back when that bounces back we can get a voltage level that doesn't quite hit a nice perfect square wave like our Arduino we know might be expecting and we can get that button toggling multiple times in a very small period of time. I've got an oscilloscope reading here actually that we can take a quick look at and it's a pretty good representation of that. So, you can see that the switches pulled high and when you press it is not anything close to what you would expect it's not a nice neat square wave it bounces quite a bit and you can see it even like comes straight back up like all the way back up to the high voltage level and back down. essentially what we can do is create hardware solutions that manage that, or we can create a software solution that'll just delay the reading, so it'll trigger on a certain change of that button state, but it will also delay reading it again until the D mails delays up so let's have a look at that sketch of it closely. I've written the sketch and the code is available under the underneath this video if you wanted to look at. as far as the Arduino sketches that we're used to writing go it's a pretty simple one we're just going to define the lower values for our buttons and pins, so the button pin will be pin three like we said before and the LED pin will be pin four now we're going to have the LED state and that is the level of the pin that is attached to the LED here we're also going to have the button state currently and the last time the button was pressed the state was. will then have a D bounce time which will run off the release function within the Arduino system and that's just the current time so we're going to store the current time as the time that the last D bounce was enabled and we'll also have a D bounce delay variable that we can set prior to uploading the sketch and like I said, the bounce will happen over a period of time so we're going to delay it for as close to that period of time as we can get and hopefully we'll be able to eliminate any of the issues that we see with the bouncing. So, I've just got some if statement set up here we're going to read the button pin. now if that reading isn't equal to the last button stay then store this as the current last button state and store that time and then compare that time against the D bounce delay and it's greater than the D bounce delay, we can then do some things which include, changing the button state reading and also changing the state of the LED and we use the equals not LED state as it will change it from low to high or from high to low it'll be an opposite thing and then we digitally write that value to LED pin.

And the final thing we do, is save the reading as the last button state and then it will run back through that loop it's all a pretty simple sketch and it's really cool to watch it in action so I'm just going to go ahead and upload it. take a closer look there we go, what we expected, we defined that led to be on to start and what we're going to do is when we press this button we expect it to just turn off and it does turns back on a new idea is that no matter how many times I press that, it won’t double toggle. now if we didn't have that data out solution in play what would be happening is when I press that sometimes it wouldn't register as a press sometimes it would, and we have a lot of different things going on there.

So yeah that pretty much covers off on using a simple push problem with an LED and the Arduino. if you guys wanted to look at more advanced switches and what you can do with that feel free to check out the rest of our tutorials where we'll go over some of those things. thanks for watching guys, have a great day