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!

Transcript

Hey guys, Aidan here from Core Electronics. Today we're going to take a look at the Arduino Uno and interfacing it with switches. We're going to cover off on what switches are and how we can use them. We're also going to take a look at a couple of different types of switches and the important things to look out for with switches.

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 the switch. So the poles actually refer to the amount of circuit you can control. And if you look at the schematic, it's pretty representative of that. And you've also got the throw, which is the amount of positions that each switch can take.

With a single throw, you can see on the left here, that the single throw only has one position that it can be in. So it's either 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 single pole, single throw switch with an on-off state. I've got the toggle switch here, which is the same sort of thing. It's just 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 illuminated latching push button. It's a little bit different because when you push this button down, it actually latches into its on state. And until you press it again, it will stay on. The final switch that we see is this one here that I've got 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 our 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 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.

I've got this circuit made up in accordance to our Fritzing sketch. You can go onto 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 pin 4 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.

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.

A switch is a physical object that uses a mechanical means to control the circuit, and what we can see with our sketch, with our 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 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 switch is pulled high, and when you press it, it is not anything close to what you would expect. It's not a nice neat square wave, it actually 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 stay, but it will also delay reading it again until the debounce delay is up.

Let's have a look at that sketch a bit closely. I've written the sketch, and the code is available underneath this video if you wanted to take a look at it. 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 all our values for our buttons and pins. So the button pin will be pin 3, like we said before, and the LED pin will be pin 4. 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, what the state was. We'll then have a debounce time, which will run off the millis() 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 debounce was enabled, and we'll also have a debounce 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 that, all the issues that we see with the bouncing.

I've just got some if statements set up here. We're going to read the button pin. Now if that reading isn't equal to the last button state, then store this as the current button, the current last button state, and store that time, and then compare that time against the debounce delay. And if it's greater than the debounce 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 the LED pin. 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. Let's see how the LED is flashing. There we go. Like we expected, we define 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. And the idea is that no matter how many times I press that, it won't double toggle. Now, if we didn't have that debounce 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 button with an LED in the Arduino. If you guys wanted to look at more advanced switches and what you can do with them, 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.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.