Basic usage of the Raspberry Pi GPIO pins

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on engineers we're finally doing some physical computing and this video we're gonna look at some basic usage of the Raspberry Pi GPIO or general purpose input output pins this video does make two assumptions the first is it you already know what a Raspberry Pi is and the second is that if you're holding a Raspberry Pi in your hand you'd be able to at least point to the GPIO pins so there's a total of four things we're gonna do in this video first we're gonna do a quick orientation on the Raspberry Pi GPIO pins as well as all the equipment that I'm using this video that way you can understand kind of what I'm doing there the next three things are going to be actual circuit building and testing first we're gonna do is take an LED and just power it straight from the Raspberry Pi power the second thing we're gonna do is wire it a little differently and then use Python to turn the LED on and off and if fun we're going to introduce an actual physical button which will allow us to turn the LED on and off with that button except in that third case we're not gonna wire the button directly to the LED we're gonna wire it as an input which will allow us to catch that event in code and then turn on the LED that way so let's just jump in and check things out in this video we're gonna be using a Raspberry Pi 3 model b+ which has a total of 40 GPIO pins so there's a lot of pins and they look like they can be used for a lot of different things they're really broadly classified into about three different things pins that provide power both 3.3 volts and 5 volts pins that are connected to ground and then pins they're designated for use in GPIO applications there are two special use pins located on 27 and 28 which we won't be using today but pretty much anything else we can use anything that's green blue purple or pink we can use however they do recommend that when available use the green ones first because those have no special meaning in this video today we're going to end up using GPIO 18 and GPIO 23 which is pin 12 and 16 so here's the actual Raspberry Pi we'll be building a circuit on and just from left to right we'll talk about each of these pieces are so the first part is the Raspberry Pi all the way to the left here sitting on top of the Raspberry Pi is a PI juice hat from PI supply my new friends at PI Supply sent me one over to try out in this video I put a link in the description if you want to check it out but this is gonna allow us to operate entirely on battery today so no wires there's a ribbon cable which on the left side is plugged directly into the 40-pin GPIO pins and on the right side is a bread with an attachment that allows us to map those 40 pins to different rows and columns on this breadboard now of course we have our physical switch which we'll use later we're looking at here is a breadboard for those ánotá breadboard is it's a way of doing circuit prototypes quickly because you can jump wires between different places to try different things out so the way this works is the top two rows and the bottom two rows they're actually wired together in one row so basically if you insert something into one of the holes on the top row it's kind of like you inserted it on any other point in the row it's basically on this positive red line here five volts is supplied and on the positive red line at the bottom 3.3 volts is supplied it doesn't matter where you plug it in you'll get 3.3 volts no matter where you plug it in and the same is true here in the center as well except instead of one row being wired together it's one column so take column ten for instance no matter where I plug it in column ten I'm still gonna get ground and you can get bread boards of various sizes this one's quite small you can get them three or four times as long but this is all we need today for this video also you'll probably be unsurprised to learn that there's 40 pins and then there's 40 pins here the only difference is that the added a separate 3.3 volt rail and a separate 5 volt rail that you can attach to it's not your oriented with the setup let's build some stuff somewhere we said the first objective is to just power an LED using the Raspberry Pi powered nothing else so for that we used 3.3 volt rail so I got my blue LED here and I'm gonna insert this on row a columns 27 and 28 with the positive end on 28 so go ahead and just push that down there next thing I do is insert a resistor anything over probably 50 is fine this is a 200 and we're gonna insert that on the positive end of the LED that way it limits the current to it otherwise you'll blow up the LED so I'll be inserting that on Rohde across 28 and 30 the reason I put the resistor on 28 and 27 is the resistor has to be on the positive side coming in next time I do is hook up the ground on row 15 which I will then hook up to the ground side of the LED unless you have to do is hook up power so for that I'll put one end on the 3.3 volt rail and the other end on the other side of the resistor you'll see as soon as I plug this in it lights up and so far we haven't written any code and that's because all we're really doing is just taking an always-on 3.3 volt pin and wiring it to ground through the resistor and then into the LED this is all cool of course but we can't write any code against this so next thing we have to do is instead of supplying a 3.3 volts we're gonna move that over to one of the GPIO pins in this case I'm going to move it over to GPIO 18 so let's place that into there so now we need to write some code so this GPIO 18 that we plug the wire in it can be operated in one of two ways as an output we can either set it to low value which gives 0 volts or we can say it to a high value which gives 3.3 volts so we'll start by employing the GPIO library and that's done by doing import RPI GPIO as GPIO next thing we have to do is actually set the mode so we'll do GPIO at that set mode GPIO and dot DCM there was two things you can do here can either do BCM or board and what BCM is is it's the numbering system that you see on the actual pins so like GPIO 18 or GPIO 23 if you say that the GPIO dot board then the numbering system is one two three four five six all the way up to 40 the reason I don't want to use that is because my particular attachment on here tells me the GPIO pins in the BCM format now to actually make this pin supply power we have to set it up as an output pin and to do that we do GPIO that set up we specify the pin number at which in our case is going to be 18 it's going to be GPIO dot output and then we could set an initial value and we want it to be off so we can do initial equals GPIO dot low and that will make sure that it starts out by sending zero volts by default now at this point you want to sanity check your circuit because as soon as you said two GPIO dot high it's going to supply 3.3 volts on that pin and if you have that pin grounded out or somewhere else that doesn't belong you could damage your equipment so just make sure that you have it hooked up correctly so our goal here is to make the LED blink once so time to actually activate that 3.3 volts of power we can do GPIO dot output we specify the pin number and then specify GPIO dot high as soon as you do this it's going to put 3.3 volts to GPIO 18 so what's the LEDs on what we can then do is we can have it wait one second and then stead of saying it's a high we can change this to low that will bring it back down to zero volts and then we can then delay for one more second so in effect this does is it turns the LED on it waits one second turns the LED off and it waits one second so this my tray brother test our program we're going to our terminal and we'll run Python led pie now we should see this LED just flash once and it does it turns on it waits one second it turns off wait one second then the program exits everything's working perfectly so now we know how to flash it we could just do it in a loop if we wanted to so we could do something like for I in range five and instead of sleeping for one second we'll sleep for like a tenth of a second and now let's write again see what happens so you can see now it flashes our LED five times over a half a second so let's we're going to do is introduce this physical switch into the equation and now we're really going to do here is just use a switch to wire one of the GPIO pins to ground however in this case the GPIO pin is going to be an input so I've done here is put one leg to ground and one leg to GPIO 23 now we have to add a second setup line here and set up is roughly the same we do GPIO set up we're gonna set up number 23 instead of GPIO dot out it's going to be GPIO dot in then the last value we have to supply is pull-up down and this tells it to whether we should pull it down to zero or up to one so in this case we might do GPIO dot PUD up the reason the pull-up and down stuff is necessary is because when a GPIO pin is operating as an input if it is wired to ground then it's essentially a zero if it is wired to 3.3 volts then it's essentially a 1 the problem is is a pin set to input that is neither wired to ground or to 3.3 volts simply has no value or more specifically it has an arbitrary value according to different interference so in effect what this does is it kind of defaults it to 1 if it's not either grounded or hooked to 3.3 but the reason this is also necessary is because for us to detect an event it's either going to be a sharp drop in value or a sharp increase in value and if you haven't defined it as either a 0 or a 1 it could be anywhere in between and that event might not actually work so in this case we're gonna pull it up to one and then we're gonna detect a sharp drop down to zero so to do this we're going to start by writing a Python function that just switches the condition of the led so if it's on it'll turn off if it's off it'll turn it on so we need a variable something like LED on I will start out as false because the program dictates that the initial is going to be GPIO down low so that means the LED is off so we'll define a function let's call it like switch and then in here what we'll do is to do global LED on that way we can modify that variable and then we will just do the opposite of LED on so LED on equals not LED on and that will switch the value from false to true if it's already false or from true to false if it's already true and then all we're doing is just using this same GPIO output so we do GPIO output and of course we're outputting on 18 because that's the GPIO pin that's hooked up to the actually LED and then we'll specify a value based on if LEDs on so we'll do GPIO dot hi if LED on else GPIO dot low and all this does is just switch it and fun we're gonna actually listen for the event and when we talk about events in this case there's two events one is going to be a sharp drop in value and one is going to be a sharp increase in value so the function we're going to use is GPIO dot add event detect and again we specify the pain that we wanna listen on and that's going to be pin number 23 that's the one that has the switch wire to it and then we're gonna look for a GPIO dot falling and the reason needs to be following is because remember we pulled it up to one so we want to detect is a sharp fall down to zero and then we can specify a callback and this is an asynchronous function meaning it's gonna execute and then it's just gonna sort of wait until this event happens and it's gonna fire a callback and that callback is going to be switch and then because this is the actual callback we have to do evey equals none unless the runners supply here is bounce time should be 300 milliseconds just to make sure that we don't get a ton of events in a short period of time and the member said this is asynchronous so what it would actually do is exit the program at this point we don't want that so what we'll do now is start an infinite loop where we'll just sleep every second or so so before we run this let's review what our program does we start our program by setting up the GPIO 18 pin as an out initial at 0 and we set up the 23rd pin with GPIO in and we pull it up to a value of 1 we then flashed the led five times in a one-second period of time we specify a switch function which all it does is just changes the LED so if it's on it turns it off if it's off it turns it on and then we add an event to where the value falls sharply and then the callback is switched which will execute when that happens so let's try it our program so if I come over to our terminal every run Python led to PI we could see that it flashes five times and then now if I press the button you can see that the light turns on I press it again that turns off now it's important to note that the LED turns on as soon as I press down on the button not when I release it and that's because when I press down on the button it immediately shorts that pin to ground which causes that sharp fall in value now just for the sake of experimentation we can change this falling to rising now what this does is it's going to modify it so I can press down on the button and it won't turn the LED on but as soon as I release the button it'll turn the LED on and that's because when I press the button it sees a sharp drop in value and then when I release the button it sees a sharp rise in value which will call that event so let's try that out so you can see when I run my program now it blinks five times like it should I'm gonna press down on the button so I'm pressing down on it I let go and the LED turns on so just to recap on the circuits before we finish up here we have two independent circuits going on here first is the circuit for the switch this is really simple it goes from GPIO 23 over to one leg of the switch and it goes from the other leg of the switch over to ground that's a simple one the second circuit goes from GPIO 18 over to one side of the resistor from the resistor to the hot side of the LED from the other side of the LED to ground now it's important to restate the button itself is not physically wired to the LED what we're doing is we're catching an event in the code and we're using that to update the state of the LED and that's that we're done hopefully this video cleared up some of the confusion on using GPIO I know if you're not familiar with it and sometimes kind of daunting but hope this makes it a little better let me know in the comments below if you found this video helpful or if you want to see more Raspberry Pi videos in the future this was the first one hopefully of many and as usual if you have any questions or comments about the video certainly put those down below as well or than that have a great rest of your day and I hope to see on next video take care
Info
Channel: Engineer Man
Views: 71,813
Rating: undefined out of 5
Keywords: raspberry pi, gpio, gpio basics, engineer man
Id: U6N5pRDOrg4
Channel Id: undefined
Length: 13min 43sec (823 seconds)
Published: Sun Jul 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.