Arduino Uno R4 WiFi LESSON 24: Make a Button Switch With a Pullup Resistor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy dcom and we're here today with episode number 24 in our incredible new tutorial Series where you're learning how to think like an engineer using the Arduino Uno R4 WiFi what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in the class we will be using the sunfounder elite Explorer kit hopefully most of you guys already have your gear but if you don't take a look down in the descript description there is a link over to Amazon and you can hop on over there and pick your kit up and believe me your life and my life are going to be a whole lot simpler if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today and what I'm going to do is I'm going to be teaching you a new component and that component is a push button switch switch it's about the easiest thing that you could imagine the switch is either on or the switch is off you either press the button down or you have not pressed the button down so it's a real simple little device but actually programming it takes a little bit of thought now it's very straightforward to do but you got to kind of think through it and believe it or not as you're working with the little onoff switch it sometimes can be something that people struggle with a little bit so what I'm going to do I'm going to show you a little bit then I'll give you a homework assignment and then next week I'll show you my solution to the homework assignment but in today's lesson just kind of go with it just sort of follow along with me and then I'll give you a homework assignment at the end of the lesson sound good I hope it does so let's see here what do we need to do we need to get in our uh I guess I need to get out of your way here to begin with and then what you want to do is you want to get in your sunfounder kit okay okay and there is a goodie bag here right your little goodie bag and what you want to do is you want to get out you want to get out this switch and I've already taken mine out so let me let me show you what I have here okay this switch okay it's this one you see it's about a centimeter on a side let me put it down here so you can see it a little bit more clearly okay you see this switch is in your goodie bag and then also you will find another little bag and that other little bag has got some little colored buttons and what I did was I took the blue colored button out now what we're going to do is we are going to take the little colored cap and we're going to press it on top of the switch okay we're going to get it lined up and then we're going to put it on there and then we're going to fight with it a little bit but get it on there boom okay now we have a bright blue or maybe you you used a different color a bright blue little switch and now you can see that you can press the switch down or let it up and it kind of goes clickey click when you do that now I need to show you how this switch works and what I should show you is the switching action happens between the two two pin that are not pointed towards each other so what I want you to see uh I I want you to see this okay do you see how this is sort of pointed back towards this it's kind of like their curve that is one wire that one wire is connected to itself this and this are connected together if I rotate it 180° this and this are one wire and those are connected to each other so it's always a short between here and here because that's one wire and then it is a short between here and here and that is one wire the switching action happens between here and here so if that button is up this is not connected to that if the button is closed this is connected to that now if I do a 180 again same thing over here these two are not connected together if I press the button that one is connected to that one so hopefully that makes sense and let's go ahead and let's just plug this in and you see when I jump over when I jump over that Center trench what do I want when I jump over that Center trench that and that are pointed towards each other so we want to put it in like that we do not want to put it in like that in fact I think it won't I think it won't even fit that way so these two that are pointing towards each other those are going to jump over the trench and now what I have here is I have a neat little switch that I can put in my project but if we want this thing to act as an onoff switch if we want this thing to act like an onoff switch we got to do a little head scratching okay we've got to do a little head scratching so now let me come over here let me see if I can get that straight there that's good it moved let's get it straight let's get it straight oh my goodness I need some more tape on the bottom I am not going to think about how crooked that is I'm not going to think about how crooked that is I'm going to move on I'm going to move on so we're going to come here and we are going to come over here to the old sketch pad view let me see if I can get my sketch pad up let's see if we can get the sketch pad up here there it is okay now let's think let's think give it a second it'll come back there it is okay let's think about that switch okay let's think about that switch so what I have is I've got this like this and so that's one leg and then I have this other leg and then I have got the button up here and this is a wire and then this is a little button like this and when it is like that this is not connected to this so This switch is off okay but now let's come over here and let's draw it again okay and let's imagine that I pushed the button down when I push the button down that wire comes down and it connects it and then this is connected to this this switch is on okay so that switches off this switch is on but now how do I detect how do I detect if the switch is on or off well I have to put a voltage to it and to put the voltage to it I have to use a pullup resistor and what a pullup resistor is a pullup resistor connects between one of the legs and 5 volts and so let's draw that and then let's think a little bit about it okay so I'm going to come here and I'm going to have that bottom leg okay and then I'm going to come here and I'm going to have that other leg okay and then I'm going to have my switch let's just have it off for right now off okay it's not pressed down now what was I going to put I was going to put a pullup resistor and we'll use about a 10K that's just kind of traditional what does 10k mean k means a th000 so that's 10,000 and if you look in your kit you've got a 10,000 Ohm resistor it's a little packet and it's labeled 10K so you'll want to use that and then I come over here and I hook this to the 5 volts on the Arduino now if that that one's to 5 volts we're making a little bit of a circuit here so this one over here is going to come to what it's going to come to g n d ground all right now where do we make our measurement we make our measurement here and this let's just go to one of the digital pens and we'll make a measurement with the digital pen let's say that I am going to use I want to use the same one that I used while ago so let me see I'm I'm going to use pin two okay and now this is where I am going to read voltage so now while the pin while the while the switch is off okay while the switch is off if I read pin two what would pin 2 see well is there any current flowing through that 10,000 Ohm resistor no because there's no place for the current to go why the switch is off so so there is no current if there is no current is there any voltage drop across the resistor no there's no voltage drop across the resistor because voltage drop is current times resistance and even though the resistance is 10,000 the current is what it's zero so the voltage drop across the 10K resistor is what zero and so what does pin 2 C pin 2 C's 5 Vols so if it's off what would pin 2 read it would read five Vols and in the world of digital that's a what that is a one okay so this is kind of strange but you see if the switch is off you are going to read a one because you're going to see that five volts well let's think about the other case okay let's come in this time okay we're drawing it again who who is our friend our friend is the 10,000 ohm 10K ohm I should put the ohm there too 10K ohm coming to what coming to the 5 volts and then where am I reading I'm reading at that pen at that leg of the switch and I'm reading that at pen two of the Arduino and this time I'm going to press the button down and now this is connected okay it's connected now when it's connected that 5 Vols it is going to drive current from the 5 volts over to the ground and it's going to go right through that little circuit now what happens across the 10K resistor well the voltage is going to drop across that 10K resistor it's going to drop across that K 10K resistor and what is pin 2 going to see it is going to see g n d and you can see that pin two is now connected right directly to what it's connected to it's connected to ground and so it is going to see 0o volts okay and what what is that going to be that is when that is when the switch is on the pin two is going to see 0 volts and that is going to be reported as a zero all right so when the switch is off we're not touching it we will read a one when we press it and it is on we will read a zero and that bothers some people because they say well if you're not touching the switch it should be off should be a zero no off is what the circuit it's what the circuit is doing and so in this case while that switch is off we will read a one when we press the button we will read a zero so what do we we need to do now we need to go in and we need to hook this circuit up let me draw it again here okay so these are the two legs of the switch one leg needs to go to ground and the other leg needs to go between a 10K Ohm resistor which you get from your kit and then 5 volts on the Arduino then this leg here right here this leg of the switch that is going to go over to pin two on the Arduino okay so let's see if we can hook that thing up and guys go back and watch this again because man I was a I was a University of Texas sophomore or Junior electrical engineering student and they were explaining the pullup resistor just like I don't even understand I don't need to know that and I kind of skipped over it and I was confused about it for years don't skip over it go back and make sure that you understand what I just told you because it's kind of important we're going to be using this switch a lot so this is the switch okay and what are the pins that we're working with we're working with this one and this one right now the switch is off if I press it down it becomes on and those two are now connected together well this left one what did we want to do with the left one go back and look at your picture we wanted to ground it so I'm going to take one of these little Bojack wires link down in the description if you guys don't have the Bojack wires yet and I want to take that left pin and I want to connect it to my ground rail so I'm going to come to my ground rail to the left pin okay this is in that same column and so this is now connected to my ground rail now what did I want to do over here with the right pin well I wanted to go through a 10K resistor which I happen to have here so that leg that right leg I've got this I've got this 10K resistor in the same column as that right leg okay that's what we wanted to do and then the top of the resistor the top of the resistor what did we want to do we wanted to connect that to 5 volts and so I will connect that one to my 5V Rail and I'm going to have to move it over by one because I kind of have that Blank Spot down there so let's try it again I've got the left leg right I've got the left leg connected to the right pin of the button you see that and now over here the other leg of the resistor we said we wanted to go to the power rail so you see this this one is going to this Row the red Row the plus row and then this side over here here this side over here is going to the ground okay now that is almost excellent but what did we say we also this leg we wanted to go to pin two we wanted to go to pin two on the Arduino because that's where we're going to be reading we're going to be reading from this one and that is going to come over here to digital pin two okay do you see that digital pen too okay that is all hooked up but what do we need to do well I need to make this rail I need to actually make it five volts so I'm going to come here and then I'm going to come over to the 5vt you see the 5vt pin I'm coming over to the 5vt pin I also need to what I also need to actually make this bottom row that negative I need to make it a ground so I'm going to come over with my black wire and I'm going to connect the black wire there to gnd D now what I have is this this is hooked up this is hooked up exactly like what I showed here right this leg goes two places to pin two and it goes through a 10,000 Ohm resistor to 5 volts this pin goes to ground so I have got that all right so now that we have that let's come back over to our code view okay let's come back over to our code view here and now let's see how I could make a measurement and what do I want to measure I want to measure whether that button is up or down if it is up it should read a one if it is down it should read a zero so we shall begin we shall begin okay now what do I have I have I have this I have this leg of the button I have it connected to pin two so I'm going to need a variable for that and so I'm going to say int and it's going to be butt pin because that's easier to say than button pin easier to type every time and that's connected to pin two boom look at that now what else do I have I might want to print something so let me set up a b rate and so that b rate rate is going to be uh 115 200 but what do I need to do I need to tell it it is an INT like that and then also before I do the BR I think what I want to do is I'm going to be reading from that pin too so I need a variable to put that reading into so I'm going to call that the butt valve for the value that I'm reading from the button and I'm not going to give it a value why because I'm going to read it right I'm going to Greed it so I'm not going to assign it to anything so that looks pretty good now down in my void setup I'm going to be talking to the serial monitor so I'm going to do a s a a Ser serial. begin and then what is it BR that I set it up at 115200 always make sure that you're the same down here as you are up here okay now I'm also going to be read reading from pin two I'm going to be reading from pin two right I'm going to be reading from pin 2 so this time when I do my pen mode we haven't done this before on one of those digital pins which pen am I using the butt pin and what is it going to be it's going to be an input this time because I'm not going to be writing to it I'm going to be reading from it so that pin mode on that digital pin is an input realize all the other times that we were using those pins 2 through 13 they were always outputs we were always writing to them this time we are going to read from it if that makes sense okay could it really be that easy I think I'm going to come down here and now what do I want to do I want to read from that pen so how would I do that I would say the butt value the butt Val is going to be equal to digital read where am I going to read I'm going to read the butt pin okay like that does that look good digital read yeah you see before we were doing analog reads with those analog pens but those digital pens I can write to them or I can read from them but when I read from them they're going to read a zero or a one they're not going to read 0 to 255 that is the analog in pens if I read from a digital pen digital knows how many things two things on or off it knows zeros or it knows a zero or a one so I'm going to read the butt Val and then I'm going to do a did I'm going to do then let's see what it is let's say serial.println what am I just going to print the butt B like that I was thinking about making a mistake in just deliberately printing butt pen and then you'd be confused I'm pressing the button not pressing the button and it's just it's just printing out a two but I'll spare you that little mistake this time we've got a little bit to go through so I'll try not to put so many deliberate mistakes in now I'm going to do a delay of DT let's come up up here and Define DT I'll say int DT and let's just make it like 50 milliseconds like that okay now could it really be that easy let's see if we can run it ah I already see I saw it before it gave me the orange and what what did I see before it gave me the orange I saw it down here like that so let's try it again that does not count as an error that was oh now this one does count as an error ah man I am skipping my semicolons like a micr python guy looking good this time looking good look at that and and and boom okay now let's turn on our serial monitor okay now what is it reading it's reading a one because when the button not pressed what does that pin two sees it sees the 5 volts so it reports 5 volts as a one now we come in with our finger and we press it and boom I get a zero okay do you see that I am now observing the button push I'm exer I'm observing the button push okay so let's just try this let's go ahead and just just take this a little bit further okay I think we're pretty good on time so let's come back over here and let's put a resistor let's put a resistor in this circuit so what I'm going to do is I'm going to put in a resistor I mean I'm going to put in an LED what am I saying resistor I will need a resistor and I'm putting the long leg to the left I'm putting the long leg to the left like that okay now I have my old trusty friend Mr 1K resistor and he is going to go from from the short leg over some convenient distance so he's connected to the short leg over some convenient distance and then he is going the resistor is going to need to be grounded so I'm going to connect to this convenient ground rail that I already have and now what am I going to need I'm going to need a PIN for my red I'm going to need a red uh a red uh uh pin I'm looking for a red wire because I like to do some color coding and I just happen to get lucky and find one there and so what I'm going to do is I am going to take I'm going to take this red wire and I'm going to go from the long leg from the long leg of the LED and let's come over to GPI or to the uh digital pin 10 so you see I'm going to be controlling that led through 10 pin through through pin 10 and it's going to come over here to the Long Leg of the LED like that okay now let's come back over to our code and let's see if we can put a little schnazzy action in here okay let's see if we can kind of get snazzy with this okay so we have this hooked up so now we're going to come back over to our code View and we're going to see if we can deal with that led well the first thing is if we have that led if we have that led what we are going to need to do with it is we're going to need to set it up and so I'm going to say int red LED and that was equal to I look over there it was pin 10 now I like to check as I put it in as I go and that even though I have OCD that is not OCD that's just good programming now if I go back and I check it five more times then that's OCD but one one check I get a free pass that's just good programming so the red LED no let's call it red pin let's call it red pin I think that's how we had been doing it red pin now if we're going to use red pin we need to do a pin mode so I'm going to say pin mode and the pin mode will be what red pin am I reading from it or I'm writing to it I'm writing it to it so it would be a wait for it output all right so what I want you to see is butt pin is an input because we're reading from it an output and and uh red pin is an output because we're writing to it now down here we're going to read the butt Val from the butt pin then serial.print line we're going to print what that value is now let's think if but Val don't forget get our parentheses if but Val equal equal 1 what does that mean that means the button is not pressed and that button two is seeing the 5 volts so if it is not pressed what do we want to do we want to do a digital right we want to do a digital right what to Red pen what we want to take it high no if it's a one if it's a one the button isn't pressed so then that's going to be low the LED will be off okay now we come that's all that we have in that if Clause so we come after the curly and then we say well what if but Val equal equal zero well what that means is the button has been pressed and if the button has been pressed here between the open and the Clos curly what do we want to do we want to do a digital right red pin and we want to do what with it we want to take it high because we want to turn the LED on so now if I press the button the LED will be on if I let the button out the LED will be off so let's see let's run this thing yeah yeah yeah okay yeah I put the semicolon inside the silly parenthesis es like that okay let's try it again looking good oh man I said looking good too soon it looks like I somehow got an extraneous extra close here so I'm going to get rid of that now let's check it this closes the void Loop okay now this closes this if statement this closes this if statement so I think those things balanced now let's try it take a sip of coffee now it's actually looking good okay you see it's looking good it's downloading and here we are it's done let's take a look here like that now the moment of truth let's come and let's watch the serial monitor you see that one that means the button is not pressed and the LED is what it's off now we come in hold your breath boom look at that the LED is on the LED is off the LED is on the LED is off who's your huckleberry huh boom we got that led going with that little switch okay so that is pretty neat we've sort of shown that we can control an LED with the switch what have we learned we've learned about pullup resistors we've learned how to incorporate a push button switch into our project and we've demonstrated it by controlling an LED okay now you will have a homework assignment okay you will have a homework assignment and you do not change your circuit your circuit stays the same as it is now you do not change anything about your circuit what do you do with your circuit leave a comment down below you don't change it okay because what I want you to do is I want you to change your code and really like if you think if you want your light to come on in the room you don't want to have to stand there and hold your finger on it right you want to what you want to do a button push and have the light come on and then it's your leisure you want to go back and have a button push and have the light go off and so to do that what I want you to see is watch this now understand that with a little slide of hand while I was talking to you I put a different program down into the Arduino and so the program we just wrote doesn't do this but this is what I want you to do boom look at that my hands are over here and the LED stays on oh a little bit too bright for you we'll come and boom it's off watch this if I hold it and let it up it comes on if I hold it and let it down it goes off boom that is a toggle switch and that is your homework for next week now I got to tell you you really have to work on this and you really have to think about it and please don't go look up on the internet and find a solution you've got to make that connection in your brain you've got to make it work on your own you got to make it work on your own by thinking through it not copying someone else's solution because I'll show you the solution next week but I don't want you to copy someone's solution I want you to think through it and it's kind of funny it's just a few lines of code it's not hard to do this really and you can do it with all the things that I've taught you already but this is is like the first time you see this problem it tends to be kind of like a really big challenge like you might have to work on it for three or four days before you could really feel figure out how to do it and let me tell you the old toggle switch that is how people left engineering and went into left the engineering department and went into home economics or or you know or basket weaving or whatever that that this is this is the little program that just drove people out of engineering even though once you see the solution it's it's really pretty simple so I hope you guys will look at this as a challenge and I hope you will do it without looking at someone else's work okay I hope you'll do it without looking at someone else's work that is your homework for next week and never fear next week I will show you my solution I hope some of you guys can figure it out on your own I hope you guys are having as much fun taking this class as I am making it I always want to give a big thank you to you guys who are standing with me on patreon your support and your encouragement is what is keeping me in the game you can also help me by giving me a thumbs up by leaving a comment down below and then also make sure that you subscribe to the channel and when you do ring that Bell so you'll get notification when future lessons drop and most importantly share this video with other people because the world needs more people thinking like an engineer and fewer people sitting around watching silly cat videos Paul mcarter with toptech boy.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 1,889
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: Rg9WvA8ovik
Channel Id: undefined
Length: 33min 19sec (1999 seconds)
Published: Thu Jul 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.