Arduino Tutorial 27: Understanding Pushbuttons and Pull Up and Pull Down Resistors

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and I am here today with lesson number 27 in our new improved series of Arduino tutorials what we'll learn about today is we are going to learn how to incorporate a push-button into a project and the closely associated concepts of pull-up and pulldown resistors and so I will need you to pour yourself a nice tall mug of iced coffee that's a strong black coffee over ice no sugar needed and it is most refreshing and then I need you to get your a Lego super starter kit out what you don't have one look in the description click the link 35 bucks you can get this kid it's got the Arduino and a boatload of components and all the projects that I am doing in this series are based on that kit and if you want to play along at home it is a little easier if we are using exactly the same hardware okay so let's get right into this and I will move out of your way and we will come over here to a little bit clearer view and this is what you will need for this project you are going to need to get out your Lego Arduino you're going to need to get out your breadboard you will need a red LED a 330 ohm current-limiting resistor you will need to get out your tiny switch from the kit excuse me and then a 10k resistor this will be our pull-up resistor or it will be our pulldown resistor okay so let's talk a little bit about how this tiny switch works and then let's get you ready to start incorporating push buttons into your project so let me get some clean paper here and we will dive right into this topic so let's understand how the switch works and I'll just start and draw two pictures and the first picture is the switch that is just sitting here when it's not being pushed it's just sitting here on its own it looks like this and then also similarly on this side the same thing is happening it's symmetrical about both sides but if the switch is just sitting between this lead in this lead it is an open circuit okay and so this would correspond to let's call it the up position of the button and if we have an up position of the button you can imagine we are also going to have a down position and this would correspond to you are holding the button down and so this is we'll call it the down position where you are actually pushing the button and in that case you will have this so in that case between these two leads you have a short so this you have a short the two are connected and in this case the two leads are open you can have a short or you can have an open now how do you take this then and turn that into something on an Arduino project well there is a new command that we have not done yet but you're going to learn it's very simple and that is a digital read now you remember from those pins 2 through 13 we could do a digital write where we could put 5 volts on the pin or we could put zeros on the pin by putting a high or a low ok well similarly we can use a digital read and the way the digital read works is we pick one of those pins we say digital read if we are applying 0 volts to that pin we will read from it 0 if we are applying 5 volts to that pin and we read from it we are going to read 1 so you're going to read a 0 or a 1 but this is how you have to hook it up in a circuit and that gets to this magical concept of a pull-up resistor so we're going to start by talking about using a pull-up resistor okay a pull-up resistor and the way that works is over here in our switch between this top lead we are going to put our resistor and then we are going to go to five volts where will you get this 5 volts you'll get this 5 volts from the 5 volt pin on the Arduino and then this other pin will simply go directly to ground all right and this in this project we are doing we are using a 10k resistor so let's think about this let's think about what happens remember Ohm's law voltage is current times resistance well if you look at this circuit from the 5 volts across the 10k and then down to ground how much current will we have well 5 volts there's an open no current will flow since no current will flow there will be no voltage drop across this resistor because the voltage drop is the current times the resistance the current is zero there is no voltage drop and so if I come here and I go to my read pin my read pin would be one a pin 2 to 12 one of those digital pins 2 to 12 we connect to it we got to do a pin mode then it would be like a pin mode say pin 12 is now an input not an output because we're gonna read from it and then we do a reading what's it gonna see it's gonna see 5 volts it's going to report a 1 why does it see 5 volts because there is no current flowing no voltage drop across the 10k the read pin is going to see 5 volts and it is to report back a one okay now let's come over here and let's think about the case same circuit but now we're going to push the button down this still goes to ground this is still across the the 10k resistor this is still connected to five volts and this still goes to our read pin okay but now what are we going to read here when we push the button the switch closes now current is going to flow the five volts is going to drop across our pull-up resistor and therefore we're going to be seeing zero volts and zero volts will report a zero so if we put this circuit together with a 10k pull-up resistor if the button is up we will read a 1 if the button is down we will read a zero and you can even see that right if this was connected like this that with the button down is connected directly to ground it's going to read zero volts here it's going to read five volts okay that is how a pull up resistor works let me just do show you you don't if you use pull-up resistors they will work for anything but I'll just show you how the pulldown resistor works here with a pulldown resistor you would connect directly to five volts and then here you put your resistor like that and then this is like this this is the button now this is a pulldown why do we call it pulldown well because now we are connecting to ground through the resistor and then here this would go to our read pin so let's say when this is open does any current flow no no current flows so what is the voltage drop across your pulldown resistor zero and so what is the is going to read this is going to read a zero now if I come over here and same set up I'm going to push the button and now because I push the button this is going to be closed this is going to five volts this is going like this okay if I push that button current is going to flow the five volts is going to drop across here and so when I read at this point what am I going to see I'm going to see the five volts and this is going to report a one okay very simple pull-up and pulldown resistor that is just showing you that if you're going to get this switch to work you can't hook it directly to five volts or ground you've got to put that resistor in there that pull up four pulldown resistor alrighty let's get this thing back over here in view and now we have our still need to get my breadboard here it is I almost lost my breadboard and all the mess here okay we'll have our breadboard and we'll get our LED current-limiting resistor wine LED just because you need to make the circuit do something so we'll use an LED alright now I guess there's one more thing that I need to draw here and that is there's four leads coming off of this this little switch and so we've got to know how to plug it in and so let me see if I can kind of draw a picture here you can see that the leads are kind of pointing towards each other like that you see how there and then like here you would have this other one like that okay the thing to see is these leads that are pointing towards each other the leads that are kind of curved towards each other like this this is always shorted together these two are always connected together and similarly this one and this one are always connected together your switching action happens between these two the switching happens between those two so what I want to do is I want to connect it where this would go to the resistor and this would go to ground because this is where the switching is occurring between and so that means is I need to connect it like that all right and if for some reason this doesn't work put it in the other way if you can't see what I'm talking about all right now what do I need I need from this pin right here I need to go through my pull-up resistor but this is kind of the problem do you see if i zoom in the switch only leaves me one connecting hole because it's kind of filled up the rest of them so what I'm gonna have to do is I'm gonna have to put in a very small jumper cable where I come from that over to a nice clean column okay so you see how I've just moved over to a new column now I have room to make my connections and so now I need to get my 10k pull-up resistor and I'm going to need to go from there over to another column and then remember this end the top end the pull-up resistor needs to come over here to 5 volts to 5 volts on the Arduino I've got a nice little 5 volt pen i hook it in and so let's look at what we have done here we have gone from 5 volts across 10k and we have connected it to that switch so now it's connected to the switch the bottom leg of the resistor or in my case the right leg of the resistor is connected to the switch what else do I need to do I need to read off of that connection I need to read I need to tap off of that bottom leg of the resistor and I'm going to come over to pin 12 on the Arduino so pin 12 is going to read off the bottom of that resistor now what is the final thing I need to do I need to ground the other side of the switch ok right the bottom side of the switch the bottom connection on the switch needs to be grounded and right I took this point up to pin 12 so the bottom side here of the switch needs to come over to GND so now I have that and let's just see let's go ahead Wow let's go ahead and put the resistor in let's finish building our circuit I mean put the LED in okay so we're gonna start now with the 330 ohm current limiting resistor I'll go between column 25 and 30 and then the long leg of the LED connects to the right leg of the current limiting resistor and then I need to come in from column 25 and I will go to digital pin 8 and so that's what's gonna make this thing work and then I need to go from the bottom leg or the short leg of the LED I need to go over to ground and now I am just gonna I'm going to say okay that is good so now I have a switch and I have an LED so at this point what we need to do is we need to come over to our Arduino code and so let me see what would be the suitable view here what will be the suitable view perhaps this one all right perhaps this one so let's start writing our code and I'm gonna get further I am going to get further out of your way here so maybe you can kind of see our circuit as we're going along as well so let's start coding all right let's think about some of the variables that I need well I've got an LED I'm gonna have an integer for my LED pin and that was an eight the LED is hooked to pin 8 so that looks good now I am also going to have a button pin and that was connected to 10 12 all right and then I'm going to have a read let's call it button read so that's what I'm going to read from the button and since I'm going to read it I don't give it a value I just declare it let's turn on in our Boyd setup let's turn on our serial monitor serial dot begin the old trusty 9600 then let's come down and do our pin mode spin mode a LED pin is a what it's an output I'm going to be sending a signal to it now pin mode button pin what is that going to be I'm reading from it it is going to be a what it's going to be an input yes indeed it is going to be an input so I've done my pin modes I've declared my variables I have turned on my serial monitor so what do we want to do in the loop the first thing we want to do in the loop is just read the value from the button and so what do we read that into we read it into the variable button read and what is button read equal to well did you toll breathe ah the happy little orange color that's good it recognizes digital read what button are we going to read from or what pen are we going to read from we are going to read from button pin and that's the only parameter with a digital read it's just where you're reading from now let's just do the simplest thing possible let's do a serial dot print fell in and then let's print button read what we just read and then close that and I like to put a delay in there and so let me come up here and make a new variable so that I will be doing things well int and I'll just call it DT for delay time and I will say that's equal to 100 or let me make it 250 just so it's a little easier to read right that's just trying to slow it down so that it doesn't run by so quickly that we can't see it alright so then I am going to put in a delay of DT delay time milliseconds all right this looks pretty good I'm going to try to download it so as I download it I do indeed need you to hold your breath to hope that this works everyone hold your breath oh darn it someone did not hold their breath who was it that did not hold their breath okay where on earth did we go wrong exit status one error compiling board and it's not telling me anything about that DT was set up void setup looks good void loop looks good button read does anybody out there see what I did wrong let's make sure that we've got a connection okay we are connected to we are connected to the Arduino on comm three this is quite perplexing it really looks so good to me okay were you guys yelling at me over this look at that button pin needed a white comma that was part of the problem but the real problem is you were not holding your breath so I need everyone to hold their breath this time okay boom okay boom and look at this it's printing one one one one why is it printing a one tell me why is it printing a one because is the button pushed no the button is not pushed and when the button is not pushed it should read a one because it is seeing the 5 volts when I push the button what should I see it'll connect that read pin pin 12 directly to ground and it should read a zero all right so let's press the button boom zero one zero one boom all right man we have a push button that is working so the Arduino now knows it's the button uppers the button down now what do we want to do well we want to do something right we want to do something on the project what can we say here if can you guys see this right if what would we say button read equal equal remember double equal on a conditional if button read is equal to one that means what does that mean that means that the button is not pushed and so what do we want to do a digital write to what led and what do we want it to be low all right so if you're not pressing the button you want the LED to be off and you guys know by now how much I hate that Arduino ads the close curly on there because then I end up putting an extra one what's the other case if button read equal equal zero what does that mean that means the button has been pushed and then what do we in again it added the closing curly so this is the start of the if conditional this is the end of the if conditional or the clause okay so what do we want to do if we press the button well button read will be zero if we press it and then we want to do a digital right and then we'll do LED and and we want to put it what hi so what's that saying if the buttons pushed turn the LED on if the button is not pushed turn the LED off and so let me download this hold your breath ah yes okay let's go over here and now I'm going to come over do you see the one-one-one-one meaning the button is not pushed I'm going to push the button in three two one now and the LED came on boom look at that on/off on/off okay it's a tad bit sluggish it is a tad bit sluggish so I'm going to take this down to 100 so it's a little snappier I will download that right that delay in there is putting a little bit of a delay in the in the responsiveness okay so that looked good let's press the button boom on/off on/off alright you guys have incorporated a push button into your project and made it actually do something and you learned how to do pull-up and pulldown resistors okay what would be the next thing that we're going to do and this sounds like it would be really easy but really you got to think about a little bit and fact I was gonna do it in this lesson but I just saw let's just learn about pull-up and pulldown resistors in the switch and now let's think about making a toggle switch and so you can go away and try to do this and then I will do it in the next lesson but what you want to do is like if I press the button I want it to come on and stay on and then if I press the button again I want it to come off and stay off okay and so that would be a toggle switch and that will be your next lesson which will be lesson number 28 okay guys I hope you felt followed along with this I kind of liked this project and would love to hear your comments down below really I like hearing from you guys let me know that people are actually watching these lessons and I'm not just sitting here talking to myself think about subscribing to the channel if you do make sure you ring the bell you've got to hit that little bill in order to get notifications and then also think about sharing this with other people palmach order from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 114,618
Rating: 4.9628186 out of 5
Keywords: Pull Up Resistor, Pull Down Resistor, Arduino, Pushbutton
Id: AgQW81zzR18
Channel Id: undefined
Length: 25min 1sec (1501 seconds)
Published: Tue Sep 10 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.