Raspberry Pi LESSON 6: Understanding GPIO Inputs, Pull Up and Pull Down Resistors

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with toptechboy.com and we're here today with episode number six in our incredible new tutorial series where you're teaching your raspberry pi who's boss what i'm going to 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 sun founder sun founder is sponsoring this series of video lessons on the raspberry pi and in these lessons i will be using the most excellent sun founder ultimate raspberry pi kit now believe me your life in my life are going to be a lot easier in this class if we are working on identical hardware so if you don't have your kit yet look down in the description there is a link over to amazon hop on over there and get your gear put together but enough of this shameless advertising let's talk about what i am going to teach you today what i'm going to teach you is i'm going to teach you how to get input from the gpio pins on the raspberry pi now what i have to say is what i'm going to teach you today i could just show you how to do it in about three minutes and you could copy what i did and it would work when you used it the problem is if i do that you really wouldn't understand it and so what i really want to do is i want to take some time maybe 30 minutes so that you really understand the concept of getting inputs from the gpio pins and you understand this mysterious concept of pull up and pull down resistors you probably have heard somewhere along the way people talking about pull up and pull down resistors but really not very many people really understand how it works and what you are trying to do okay they don't understand how it works or what you're trying to do and so what i'm going to do is i'm going to jump in and i'm just going to show you some simple things and then we'll start building and building and building and then you will end up understanding how to design a circuit how to design an input configuration for your raspberry pi with either pull up or pull down resistors and you'll understand why they are necessary and you won't ever just jump in and try to do it without having it configured properly sound good okay well let's jump in what i am going to need you to do is get out your raspberry pi and i think i can switch over here to this view and you will be able to see the code view and you will be able to see the raspberry pi and we will just jump in and start having some fun but to start with you are going to need a most simple component from your ultimate raspberry pi kit and what you're going to need is you are going to need a female you're going to need a female to female jumper wire i always like to show my studio at least once i like to show my studio at least once during the video so you can see i am trying to keep my studio clean you can see i have my sun founder kit i will put it over here and then we're going to open that up and if you go to the second level you take the little shelf out if you look down here you will find your female two female ribbon wires and then i have pulled one of those out and so that's what we will need to get started now i will warn you we are going to get over to the nice wonderful push buttons but we're going to start understanding the concept just with a simple wire okay and so i'm going to show you the simplest way possible to read an input to get an input from the gpio pins and so let me come back over here to my overhead view okay and what we are going to do is we are going to look at our pin outs and i'm going to see what would be a really convenient set of pins to use okay what would be a convenient set of pins to use well i see that the bottom right pin in this pin out the bottom right pin is a gpio pin and so that's what i'm going to use i'm going to use that as a as the gpio pen that looks like that is pin 40. and then if you look at the upper left pin if you look at the upper left pin it is pin 1 it is 3.3 volts now you've got to understand when i'm saying uh up and i'm saying right let me come back over here to this view and i want you to make sure that you have your raspberry pi oriented to yourself the way i do i have the usb the usb ports pointed towards me the power and the hdmi are to the left and the gpio pins are to the right because you got to make sure that you hook this up right because if you hook it up wrong it's going to burn out your pie you'll fry your pie okay in fact some people would say don't do what i'm doing but i'm going to do it and just say be careful and make sure that you do it correctly so the first thing we're going to do is we are going to hook our little female to female wire we're going to hook it to pin 40. physical pin 40 and what was that that was the lower right okay the lower on the right we're going to hook it there to start with now we're going to come back over here and let's just look and look at that the upper left pin 1 the upper left is 3.3 volts and so we're going to see if we can read something from pin 40 but if we're going to read something from pin 40 we've got to hook it to something so what am i going to hook it to 3.3 volts which is upper left which is pin 1. if you hook it to pin 2 upper right you will fry the pipe because you can't put 5 volts on a gpio input okay that would be very bad so very careful upper left lower right now i have gpio pin 40 hooked up to pin one which is 3.3 volts okay could i say it any clearer i don't think i could say it any clearer so now let's see if we can go out and read let's see if we can go out and read from that pin and remember in the digital world of the raspberry pi a 3.3 volts is counted as a 1 as an on and a 0 volts is counted as a 0 or an off zero is in false not zero is in the number zero zero is in the the logical false or the logical zero so three point three reads digitally as a one and zero reads digitally as a 0. does that make sense so we are hooked up to what 3.3 volts so we would expect to read what we would expect to read a 1 there all right so now how do we configure the gpio pins so that we can read from them well we need to come over here and we need to fire up we need to fire up a new uh we need to fire up a new terminal and i need to take just a second to manage this window to make sure that you can see it properly okay that looks good and now what i need to do is i am remember i am ls i am in my home directory and remember in the last lessons i created the python directory which we write our python programs in and so i'm going to go ahead and create a new program and i'm going to call it nano and i'm going to call it gpio and then i'm going to call it input okay gpio input because that's what we're doing dot py and the dot py is kind of important and boom fresh new python program just waiting for you to write it all right and so let's just do something really really simple let's do something really really simple okay i need to do a tad bit of windows management here all right that looks good okay so let's see we're going to do the simplest gpio read possible on the raspberry pi so the first thing we're going to need to do is if we're working with those gpio pins we're going to have to what import our m port our friend mr rp uppercase little i dot gpio import that library is what as gpio you could import it as kitty litterbox if you wanted to but i think gpio is more descriptive now anytime we want to use that library all we have to do is reference gpio okay now what we need to do is we need to set the mode of the raspberry pi so it knows what numbering scheme we are using on the pins and so we need to do a gpio.set mode we are going to set the mode and we're going to set it as what g p i o dot what b o a r d that way we are using the physical numbering system on the pins not the bcm numbering system so it'll be one two three four on down like i explained ad nauseam in the last few videos so i'm really not going to explain that anymore so now it's set up to use the board numbering system now what do we need to do we need to set up pin what gpio dot set up set up which pin we're going to set up pin 40. okay now let's do a little let's do a little better than that let's set up a variable in we'll call it n pin and n pin is going to be equal to 40 and then we'll come down here and we're going to set up what in pin and then we're going to set it up as what a g p i o dot what i n so just like dot dot o u t dot out would make it an output this will make it an input notice all uppercase the uh the case does matter quite a bit here all right so we've got it set up as a input pin now how do we read the value from it we we give it a variable i'll say i'm going to read into read val okay read val is going to be equal to gpio dot input inputs lowercase notice what am i going to read from in pen okay i'm going to read from in pen and so now at this point i have read the value from from pin 40 and it's gone into the variable read valve now the simplest thing we could do is just print read val like that okay and then what do we always want to do we want to be good boys and girls and so we are going to do a gpio dot clean up so we're not leaving a mess for the next people that might want to use the gpio pins and so now if we run this what do we expect we would expect to read a what you tell me okay we're hooked to a 3.3 volt pin so we would expect to read 3.3 volts no we expect to read a digital value of 1. okay so let's control x to leave enter or a y okay and then enter and now we're here and so now we should be able to instead of doing nano we are going to want to what python that all right everybody hold their breath [Music] oh gpion n pin is not defined what did i do what crazy thing did i do did i have an error there probably in pen uh yep just i didn't get my old finger off of the upper case soon enough so hopefully you guys caught it okay control x yes enter all right everybody somebody didn't hold their breath that's what went wrong one of you guys didn't hold your breath so this time i need everyone to hold their breath boom look at that a digital value of one okay why because this is hooked to 3.3 volts all right let's come back over here and let's look at our pin out and let's find a ground what would be a convenient ground a convenient ground would be pin 39 which is lower left okay and so what we're going to do is we are going to come back over here and we are going to take this wire from pin one and we're going to put it down at pin 39 and so now i have pin 40 hooked to ground all right and so we can up and we're going to run it what do we expect this time what do we expect everyone hold their breath zero yes okay so we have done our first digital read from a gpio pin on the raspberry pi very good very good seems pretty simple doesn't it it seems pretty simple all right well now let's go in and let's take it up a level because you're not going to want to just sit and run the program every time so let's come back and edit it what you would really like to do is let's say that we would like a loop okay we would like a loop where we would every time through read it print it read it print it so we're going to come here and i'm just going to say what i'm going to say while i could say while one equal equal one or i could just say while true when is true true true is always true so i have created an infinite loop and now i need to tab these over like this okay and so now it would just continue to read print read print read print until i killed the program with like a control c what is the problem control c crashes the program but when it crashes the program when it ends the program it would never get down here to do the gpio cleanup well what if i put the gpio cleanup in the while loop then it's going to cancel the gpio pins every time through and that's not going to work either so i need a way to leave the program i need a way to leave the program gracefully where it ends up down there to do the gpio cleanup and this is something i probably should have taught you a long time ago but all right i'm a little delinquent but i will go ahead and do it now you do it with a try and accept and so what do i want to do i want to try to do the following thing now you can see that this is sort of like inside of a loop here and so now everything under the try has to be tabbed over so that the whole while loop and everything is part of this try clause so it's going to try to do the loop it's going to try to read print read print read print it's going to do that until until what it's going to do that until you do the accept okay x sept and what do we want to accept out on key board interrupt all right now notice keyboard interrupt the k is uppercase the i is uppercase and so this is saying it just keeps trying it just keeps looping it keeps looping it keeps looping with that while loop until what until it gets the control c and then that takes it to the accept and what do we want to do in the except we want to clean up the gpio pins so all this is doing is it just runs forever like normal but when it sees a control c it jumps down here to the accept clause and it does a cleanup you could do other things in the accept clause whatever you want to close out whatever you want to clean up you put all of that in the gpio cleanup so now what it should do is it should just keep reading and printing and reading and printing and reading and printing okay so now though we don't want to go that fast and so we want to slow it down a little bit so we need to put a delay in there and so what i can do is i can say from time import sleep all right now i could just say import time but if i just import time then anytime i want to delay i have to say time dot sleep if from time i just import sleep i can directly call that method just by saying sleep if that makes sense so by importing the library this way it makes it a little bit easier so now what i'm going to do here is i'm going to put in a little bit of a delay so i'm going to say sleep and i'm going to say let's sleep for one second so there should be a one second between the reads does that make sense ctrl x y and enter okay now we're going to come up and we're going to run the program right man i hope that you could see all that i hope let me go back and look and make sure you oh yeah you all would be able to see all that okay just want to make sure that i hadn't covered up what i was doing okay so now i need everyone to hold their breath what should we expect tell me what we should expect if you look at the circuit what should we expect yes zero zero zero i will need you to hold your breath and darn it i guess i had better really get a little further out of your way though so you can see okay that's that how about that one no how about this one okay so now we're gonna run it everyone hold their breath zero zero zero zero okay man look at that zero zero zero zero so we're sitting there and we're reading a zero because we have pin forty the pin that we're working with hook to ground that is really good now what do you want me to do you want me to take this loose and you want me to put it to pin one okay like that and now or yeah pin one and remember pin one upper left 3.3 volts don't fry the pie okay now i'm getting a one one one one so now what you might think is okay we could get the pus push button switch and if we press that switch we could connect uh the pin 40 to 3.3 volts and it would read one okay but what is the problem the problem is if the switch isn't pushed my gpio pin pin 40 would be connected to what it would be connected to nothing and that is the problem with getting inputs from a gpio pin you cannot leave that pin floating or disconnected why because let me disconnect it and show you what happens when i disconnect it okay it read a zero it read a one it read a zero a zero when you are floating like this you might read a zero or you might read a one okay and so if you just hooked a switch up in there and in your circuit configuration it could be left floating then yeah if you press the button you're going to see the right thing but if you leave it floating you are not going to see the right thing and that is called a floating input and that is always bad and that is why we need a that is why we need a pull up or a pull down resistor so let me start by showing you how a pull up resistor works okay let me show you how a pull up resistor works and so i'm going to have to come over here to my little sketch pad and give me just a second to get this thing called up that looks good and if i come over here i should have a sketch pad and let's see if we can get that thing uh on here let's see [Music] try it again [Music] ah disconnected cable it'll get you every time all right let me put that on there now we should see the thing pop up here there it is okay sorry for that little delay but i had a cable that was not connected all right there it is okay so let's look at what we just did all right what we just did is we had pin 40 here this is pin 40 on the raspberry pi okay now here we had 3.3 volts and if we hooked pin 40 to 3.3 volts we read a 1. if we hooked pin 40 to the ground we read a what a zero now where does the problem come the problem comes when pin 40 is not hooked to anything it's in an indeterminate state you might get a one or you might get a zero all right so the first thing the first thing that you might think about hooking up would be you might think about coming in and taking pin 40 and then coming down through a switch like this and then coming to 3.3 volts now if you close the switch if you close the switch sure enough it's going to see the 3.3 volts and it's going to read a 1. but if you leave the switch open it's in that indeterminate state it might read a one or it might read a zero so you can't have a push button switch like that because if you just hook it up in the simple and straightforward way what is going to happen while the button is not pushed you're going to have that indeterminate state okay so that is not going to work so this is the way we do it okay we come to the input right this is pin 40 all right and then we come through a resistor and a good resistor would be something around 10 k or 10 000 ohms and then we come up here to 3.3 volts now in this particular case what is pin 40 going to see well there's no current there's no way for current to flow so pin 40 is going to directly see the 3.3 volts what's it going to read it's going to read a 1. but then if pin 40 is also connected it's connected to 3.3 volts through the 10k resistor if it is connected also to ground [Music] through the switch when the switch is open does any current flow no what is the voltage across the resistor the voltage is the current times the resistance there's no place for the current to go so the current is zero there's no voltage drop across the 10k resistor and therefore you are indeed going to read a what you're going to read a 1. but now let's come in and let's close the switch when we close the switch now the current can flow through here and the voltage is going to drop across that 10k resistor and what does pin 40 see it sees ground you see you've got a dead short you're connected completely here and you're going to read a zero all right so if we want to make this thing work where we have a switch what we need to do is we need to have this circuit we're going to have pin 40 okay and then pin 40 is going to go two different places it's going to go to one leg of the switch and then the other leg of the switch or the button i should say the other leg of the button is going to go to ground that's half the circuit now on this top side pin 40 is also going to go to 3.3 volts so let's just review when the switch is when the the button is not pushed when the switch is open there's no current through this lower leg and therefore there's no current through this 10k resistor and all pin 40cs is 3.3 volts if we close the switch pin 40 directly sees ground and all the 3.3 volts is dropped across this resistor i hope that makes sense so let's see if we can hook this thing up and let's see if we can make this thing work okay so i'm going to have to put this away here and then what we are going to do is we are going to get out i guess i should go back to my view here we're going to get our sun founder kit and we're going to need a couple of things out of it one of the things is we are going to need a little push button switch now in mine it's kind of like the upper left compartment of the top tray you see this little you see this little baggie of all of these exciting little components and you can see you've got the three potentiometers in there you've got all types of different things one of the things you have in this little bag is a very small little switch a very small little push button okay can you guys see that that little push button okay you need to get that push button and then also what you're going to need is what you're going to need a resistor and so you need to get out your little bag of resistors and you want to get a 10k or a 10 000 ohm resistor now i want to show you remember in that last lesson we did the binary counter and what did i do with my 330 ohm resistors from the binary counter can you see that i kind of taped them together and i labeled them so next time i need 330 ohm resistors i've got them labeled and taped together and not just in here askew unfortunately you really can't put them back into these things once you pull them out you can't put them back in there and so what i like to do is i like to label them with masking tape and put them like that all right now what i'm going to need to do is i'm going to need to find the 10k resistors i'm going to need to find the 10k resistors uh okay here they are you can see that it is labeled 10k ohms and then what i'm going to do is i'm going to pull off one of those 10k ohm resistors okay so i have my 10k ohm resistor and i have my switch and now what i'm going to need is i'm going to need some number of female to male wires and so remember the female to male wires were on this little ribbon and in the last lessons we already pulled some off and so i'm just going to get out the ones that i'd pulled off all right now i'm going to put away my female to female because i'm not going to be using that anymore i'm going to be putting it away and then i'm going to very neatly put my box back together and i don't have it quite going in there uh okay i got those things holding it always make sure you get your box closed nice and easy keep everything neat all right now let's go back to our build view and uh what one other thing do i need that i didn't get out i need my breadboard [Music] so let's get the breadboard out let me make sure i've got my builds area square and so i'm going to get out my breadboard [Music] okay so just as a reminder this is what we are trying to build okay so we go to pin 40 so i'm going to get the female and i'll just use a red i'll use i'll use a red wire as my control wire so my red wire is going to go to what pin 40. and you need to be able to see that so i come over here my red wire went to pin 40. and i'll get my breadboard in view here right and so now i just need to bring that over here somewhere to the board so i think i put it about in column five all right now let's look back over here and look at our circuit and see what we want that to hook to well one leg or one thing that pin 40 goes to is it needs to go to one leg of the little switch so let's put the little switch in there okay let's put the little switch in there give me just a second here okay so let's put the little switch in here so i'll come back over here to my overhead view and what you have to see is do you see that the switch has it has four legs but two of the legs point towards each other and on the other side two of the legs point towards each other and those are the ones that are either connected or disconnected by the button okay and so like if you look at the two legs that are connected together let me start over all right let me let me show you here the two legs the two legs that are pointing towards each other they're always connected the two legs over here that are pointing towards each other they're always connected it is the legs that are not pointed towards each other the legs that are not pointed towards each other those are the ones that will open and close when you press the button okay so it's the connection between these two that you want to make and so one of those legs needs to be in the same column as pin 40 and then the other ones are going to hop over they're going to hop over the center all right and so this leg and this leg we're pointing towards each other this leg and this leg we're pointing towards each other this leg is not pointing towards this leg and so the switching is going to happen between here and here does that make sense and then use your thumbnails and really get those down in there good okay make sure that you get them all the way down in there and then yes i am hooked pin 40 to the left leg all right now let's see where else pin 40 is going to go where else is pin 40 going to go let's go back and look pin 40 also needs to go to a 10k resistor to 3.3 volts all right and so it goes to a 10 point it goes to a 10k resistor to 3.3 volts and so i've got my 10k resistor here i'm going to bend it very neatly as such okay and then what i see is this is going to go to what pin 40 and then i'll come back over here to column 1 like that now that column 1 needs to go to what 3.3 volts and so i'm going to go ahead and i'll get i'll get the yellow wire this time so you can use whatever colors you want i'll use the yellow wire and then that that other leg of the resistor in the column one it's going to come up to the 3.3 volts okay so right now pin 40 right now pin 40 is connected where it's connected to 3.3 volts through this 10k resistor so if we come back and run this program it should read a what it should read a one and so let's come back over here let's come back over got to get my right okay and let's run the program so i'm going to up i'm going to run it and what am i reading i'm reading 1 why because pin 40 is connected through the resistor to the 3.3 volts there's no place for the current to go and so there's no voltage drop across the 10k resistor so you are reading a 1. and so that part works very good so let's cleanly leave the program with a control c and let's take a big swig of copy now what do we need to do to connect the rest of the circuit let's go back to our circuit diagram and what you see that we need to do is pin 40 also needs to be hooked through the switch to ground okay through the switch to ground and so what does that mean i need another wire okay and that means the other leg of the little push button which is the right one okay it needs to go where to ground and ground is what it is pin 39 over here so i'll come to the lower left pin 39 like that all right now still if i run the program what should i read if i run the program and let me i'll just move the whole thing adjust it a little bit where we'll see it in the code view as well so i'll come back over here where you can see the code view all right okay so now let's run it okay it's reading a one why because the button is up and when the button is up there's no current and that pin 40 sees 3.3 volts but now if i press the button down then look at that i get a zero why because now i've got a dead short to zero to the ground and now i let it up and it's a one okay so i have made a push button switch with a pull up resistor okay i've made a switch with a pull up resistor and it's working okay it's working very well now also see i can do the control c to quit the program i can come in here and i can uh i can run it again and i don't get an error because right i cleanly exited using that keyboard interrupt so look we are now officially reading from the gpio pins we are officially reading from the gpio pins i think it'll help if i back up a little here for you to see the for you to see the whole thing so that is pretty darn neat all right that is a pull up resistor why because the resistor is connected to or the gpio pin is connected to the 3.3 volts through what it is connected to the 3.3 volts through the 10k resistor all right that is a pull up resistor but let's go in and let's understand the pull down resistor so i'll come back over here and let's uh let's clear this and i'll show you the other way to do this and really they're both pretty much equivalent they're both pretty much equivalent but in the pull down resistor what i have is i'll come and i'll go to let's say this is pin 40. okay this is pin 40 like that and then this time what i'm going to do is i'm going to put my 10k resistor down to ground like that and then up here i'm going to put my switch to 3.3 volts all right now with the switch up with the button not pressed what am i going to see i'm going to see ground okay through the 10k resistor i am going to see ground but now if i close the switch what do i see i see the 3.3 volts and so in this case when i press the button down i'll get a 1 and when i press the button down yeah i'll get a 1 and then when i let it up i'll get a zero so it's going to be the same result but it's just going through a pull down instead of a pull up resistor and so let's hook that up just to show that i can do that as well and so i'll come back over here and we'll look at doing it as a pull up resistor well in that case what the resistor the bottom of the resistor was hooked to what the bottom of the resistor this yellow was hooked to what it was hooked to ground like that and in this case the switch was hooked to what the top of the switch was hooked to what to pin upper left okay the upper left pin which was upper left pin one okay upper left pin one does that make sense so i just swapped those two wires and now pin 40 goes to 3.3 volts through the switch and pin 40 goes to ground through the 10k resistor now this resistor is a what the resistor now is a pull down resistor by making that but again it should work exactly the same so let's come back over here to our code okay and so now if i run my code i should see if i run my code i'll get out of your way before you yell at me if i run my code it should still give me the same result it should sit there and it should read a zero right okay it's sitting there it's reading a zero i come in and i press the button and one shazam look at that zero zero zero one one one okay guys this sounds like you know it sounds like a whole lot of work to make a switch work but what i want you to see is i want you to see that you can't just go in and put a switch and then press it and hook it directly to 3.3 volts because that'll read a one but then when you let the button off when you let the button off you could get a zero or a one you have to build this circuit and you have to understand pull up and pull down resistors okay i hope this makes sense i really hope this makes sense and again i could have what i'm going to be showing you in two or three lessons i could have done in five minutes i could just say just hook it up and then you would wonder why do i need that resistor what is this what is this floating business but i showed you if you leave an input floating you could raise a one or you could read a zero okay guys so what is your homework for next week your homework for next week is to go back and hook your led circuit up again remember your led going through a 330 ohm resistor and then i want you to control the led with the push button if the button's up the led is off when you press the button down the led turns on as long as you hold the button down the led is on when you bring the button up the led turns off okay that's your homework for next week and what i need you guys to do is i need you guys to post your homework to youtube post your solution to youtube then in your description of your homework solution link back to this video and then down below in this video leave a comment that links over to your solution and i go in and i look yes i do i go in and i look at every single solution that you guys post but i want to see that you're doing your homework okay i want to see that you're doing your homework so show me that you can make an led that turns on and off with a push button if that makes sense okay guys i hope that this wasn't too tedious for you and i hope that i have an audience that really wants to understand things and is willing to take 20 or 30 minutes to really understand it and not just quickly copy what i'm doing because these concepts are going to be really really important as we move forward in these lessons and i want you to make sure that you uh that you understand what you're doing and that you could go off and do things on your own and so i hope you guys hope you guys don't mind that i'm taking a little time to explain this in detail okay i hope you guys are having as much fun taking these lessons as i am making them as always i want to give a shout out to you guys who are helping me out over at patreon it is your support and your encouragement that keeps this great content coming if you guys are not helping out yet look down in the description there is a link over to my patreon account think about hopping over there and hooking a brother up be sure to like this video if you like it and leave a comment down below because when you leave a comment that helps me with the old youtube juice and this video will be shown to more people because the world needs more people who are doing engineering and coding and fewer people sitting around watching silly cat videos paul mcguire with toptechboy.com i will talk to you guys later [Music]
Info
Channel: Paul McWhorter
Views: 26,697
Rating: undefined out of 5
Keywords:
Id: 0OYtR8UdZQk
Channel Id: undefined
Length: 43min 54sec (2634 seconds)
Published: Thu Apr 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.