Tutorial 14: How to Use Buttons with Arduino
Free Arduino Course
Using Buttons with Arduino
Become the Maker you were born to be. Try Arduino Academy for FREE!
Figure 1: the schematic from the video.
How to Use Buttons with Arduino Transcription
Hey everyone. Welcome to another Arduino lesson. I hope everybody’s doing great. So we’ve talked a little bit about blinking LEDs and stuff like that. A lot about Theory how the board works, coding. And all that stuff is definitely need to know. But today we’re going to talk about buttons and using buttons with the Arduino and this is something that you’re going to find yourself doing a lot of so this is basic yet important.
And as always to avoid information overload and complicating things we’re going to start off simple and then progress from there. So I have here a little push button switch the kind that you might use with Arduino and I have a schematic representation of this switch. And one of the first things to notice about this is that we have four pins 1 2 3 4 and 2 of them are connected internally and another two are connected internally and if we flip this guy over indeed we can see there are four little pins. When you’re using this button, you got to be really careful that you hook it up right. Because again, there are two and two pins that are connected internally. So theoretically you could use this button to control two devices at once but you may or may not want to do that. The question here that we’re going to try to answer — or we are going to answer actually is how do I know out of these four pins which are connected?
The answer to that is pretty simple and what I’m going to do in order to stabilize this so it doesn’t move around is — I want to break out this little breadboard here — and I’m going to stick this button first in the breadboard across this this Gap because all these points are connected and this gap breaks it. So these are not connected all the way across the board. So I’m going to do that and I’m going to grab my trusty ohmmeter and I’m going to put it on continuity now — you don’t have to do this because if you put it in a breadboard wrong, you’re going to get a wrong reading for example, if I were to stick the button here since these points are all connected and I put it in the wrong way. I would get a false reading — but I’m just doing this because I don’t want the button to move around when I’m trying to take the reading so I’m going to go ahead and do my reading here and I’m going to take these two pins and hear that noise. That means these two pins are connected internally and on the other side same situation. We have these two pins that are not unless of course, I press the button which I’m doing now and now I’m letting it go.
So that is how we figure it out which ones are connected and how to properly wire a switch. So let’s get rid of that. I’m going to bring in this circuit here.
And here we see the Arduino Uno, we have a push button switch and an LED and a couple of resistors. There’s a 1K here to limit the current to the LED and then here is a 10K resistor that serves a different purpose. We’ll talk about that in a minute, but I’m going to go ahead and demonstrate the circuit. So first we know what it does and I think you already probably have a pretty good idea of what it’s supposed to do.
When I push the button the LED comes on when I Let Go it goes off and I want to kill this light for a minute so you can see little bit better.
Okay, so I’m pushing the button the LED comes on when I left the button go it goes off and there’s a slight delay there but for all intent that that’s the way it works and that’s fine.
So I want to look at the schematic take a closer look at the schematic and talk about how this is wired. And then we’re going to get into the code and discuss the code for this and maybe make some modifications to make this circuit a little bit better.
Well, I don’t want to use or better because it really depends on what you’re doing with it.
So we’re going to go ahead and make some modifications to the code once we get into the code to change the behavior slightly. But first we’re going to jump over and take a look at the schematic and discuss really quick how its wired.
Okay, everybody here is a schematic before we get into the code. I just want to quickly go over it. So here we can see we have our push button switch and like I said, these two are tied together here internally so we’re not using these other two pins. We’re just using the one going to 5 volts. And then the other one is going to this 10K resistor here, which we’re going to talk about in a second and I do want to note before we go any further that these two points here — they are not connected. Okay, just to make that clear, there is no connection here. So we have just an LED with a current limiting resistor the cathode goes to ground and then the resistor limits the current so we don’t burn out the LED or actually burn out the Arduino because these pins can only source so much current and then here on pin 12, we have this 10K resistor and when the switch is open this pin 12 sees zero volts, but when you push the switch and it closes what happens is it sees the 5 volts applied the 5 volts drops across this resistor and the pin twelve sees 5 volts.
So if you were just to imagine taking your voltmeter and then putting the positive lead here and then the negative lead to ground, if you were to do that and then close the switch you would see 5 volts. So this resistor is here. So the pin sees the 5 volts when the button is pressed. So this pin, pin 12, kind of controls pin 11 and you’ll see in the code exactly how that works. But really that’s all there is to the schematic here not much else to it. So let’s go and take a look at some code.
So here we are in the Arduino IDE, and I already have the code up that controls the circuit that we talked about and turns the LED on when the button is pressed off when it’s released. So let’s go through this line by line.
So we know what all this stuff means up here. We just have a couple of integers we declare and these are just PIN numbers.
The button was connected to pin 12, remember and the LED to pin 11. So that’s all this is its just PIN numbers here. Then we have button state which is another variable and we’re going to go ahead and set that equal to zero. It’s going to give us the push-button status.
So that’s all before setup. And here we come in to setup and you see the pinmode function we talked about in an earlier tutorial and really all we’re going to do here is take the button pin make an input and the LED pin make it an output and that’s all there is to setup.
Now we fall into the loop and we’re looking at the button State again and we’re going to say, okay, we need to set the button State equal to whatever digitalRead — which is the other function we talked about — to and the earlier tutorial digitalRead is going to read the status of the button and it’s going to be either true or false, 1 or 0.
So if button state is either going to be well a 1 or 0 now we set it equal to zero already. So it’s going to be off by default. Then we have an if statement now, you might remember hearing about the if statements in the earlier lesson. I think it was Lesson 12 on control structures. So I’m not going to go into great detail here because I did talk about if and if-else statements back in lesson 12. If you want to review it, go back to Lesson 12, but we need this if statement to decide whether or not the LED is on or off. So we’re just going to fall right in here and if button State equals high — and notice, I have a double equal and am not using the assignment operator, we’re using the double equal comparison operator — So if button state is high, we’re going to use a digitalWright function again, and we’re going to write a high to the LED pin, which was pin 11 and that’s going to turn the LED on.
Else If button state is not high then we’re going to use digitalWrite and write a low. And here I have a delay. That is actually I don’t know what it’s commented it out, but I did want to insert a small delay to so that way the LED has time to react so that’s the code in that shell — not too complicated.
But what if we what if we want to press the button once and then let it go and have the LED stay on and then if we want to turn it off we come back later, press it once again, and it goes off?
See with this you have to hold the button if you want the LED on and then as soon as you let it go it goes off and there may be some instances where this kind of code in this kind or setup works, but it is possible to change it so that way we can turn the LED on and keep it on once we let go of the button and we don’t have to modify the circuit at all. We can leave the circuit alone – same schematic same circuit. So let’s think about how we can do that.
So what we want is a variable that the loop function can examine and check to see if the LED should be on or off then when we’re in the loop. We need to check the push button. Now if it’s pressed we got to check the LED state if that state 0 we got to change it to 1 and if it’s 1 we got to change it to 0 and this is called toggling by the way when you toggle something on and off. Toggling is another word for it. And then we also want a little delay. So we’re going to use the delay function again so we can get our finger off the button. It just gives us time to let it go because it’s going to run through the loop pretty fast remember, so we need time to react and let go of the button before it runs through the loop a second time. So, let’s see here. What do we need to do? We need another another variable to check the LED state. So I’m going to come up here and I’m just going to make it integer.
And we’re going to set that to zero right now. We’re just going to assume it’s off. We don’t need to change anything here and then we get into the loop. Let’s see button state probably leave that there and I want to add — we’re going to use more if statements and we’re going to do a little bit of something called nesting and we’re going to Nest these two if statements. So, let’s see if button state bracket it wasn’t State and then if now I’ll explain this in a minute.
You got to be careful when nesting control statements because it can get pretty confusing and a good thing about the Arduino IDE is it does show you where you’re at. So it helps eliminate some of the confusion.
And then I’m going to say else LED State equals 1 semicolon.
And let’s make sure we got two if statements or two different Loops.
That is it now we did want a delay. So I’m just going to do half a second delay. That’ll give us enough time to take our finger off of it.
And I want to change this line of code right here.
I’m going to change this to if LED State equals — well one and higher the same thing — I’m just going to put the number one.
And then digitalWrite high, else digitalWrite low. I don’t know that we need this delay here. So I’m just going to comment this out. So let’s go ahead and check this and see make sure I didn’t make any mistakes first of all because I kind of did this on the Fly.
Okay, so it looks like it compiled and you can’t see it.
I’m going to go ahead and upload it to the board. And while it’s uploading just for clarity. I want to have a little inset here a picture of what we want to do. We want to press the button once — LED stays on and then press it again it goes off and as we can see from the little inset, that is what we’re going to be doing.
Okay, everything looks good. So now let’s step through this again. Now these three integers and variables which are integers. They didn’t change at all. We just added a new variable – another integer LED State and you’ll see why we need that in a minute this stuff stays the same no need to worry about that. And this one stays the same to now here. This is just saying, look if button state is true or 1 then fall into this other if statement. So if LED state is true, or set or 1 we got to turn it off so we can fall into this here and we turn it off by making it 0 so if button state is a 1 if it’s pressed then we come over here. We check the state of the LED and if it is on we turn it off if it’s not on.
Then we turn it on with this else statement here. And then we have this little delay that gives us time to take our finger off the button and now here again we have okay, so if LED State equals 1, we write a high to the pin 11, let me turn it on or if not, we right a low to 11 and turn it off. So the only tricky part about this code here again is just when you get into nesting if statements and nested Loops, it can be a little bit confusing to see how the logic flows but again, we’ll go through it one more time just to make sure you guys understand.
So we fall into this if statement here and if button state is true or 1, okay, because if it’s if it’s not true we skip this part and then we make it true.
And then we turn it on. Okay, but if it’s true and on or set to 1 we fall into this other if statement.
And then we check the state of the LED and it’s either going to be 0, 1 — off or on. If it’s on it’s going to be 1 then we’re going to toggle it to off and set it to 0 and that’s really all it does.
So there’s a lot of other things we can do with buttons and in our next lesson, we’re going to actually add more buttons and do some more things. But for now guys go out there give this a try see if you can write code like this first try it without peeking and then then see if you can’t get it right come back check. See if you know what your mistake is if you made any and then see if you can alter it go off after a certain amount of time after pressing the button. Anyway, I will see you guys in the next lesson.