Raspberry Pi Linux LESSON 26: Controlling the Raspberry Pi GPIO pins from Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order with top tech boy comm and I am here with lesson number 26 on learning to use your Raspberry Pi if you've been through our first lessons you already know how to install the software install the operating system get the Raspberry Pi booted up and you've learned quite a bit about Linux how to find your way around the system for a Linux based system like the Raspberry Pi what we are going to do today is we are actually going to go in and start building our first circuit and this is what everything has been leading up to and if you've been through our earlier series of lessons on the Arduino you really know that the Arduino is a great little micro controller for controlling the pins and working with circuit elements and working with sensors and actuators and the thing is though you finally just reach the point on a write on a Arduino that you just run out of horsepower and you need something with more memory with faster speed with more computational resources and so then you move up to the Raspberry Pi so that's what we did and then we've gone in on the Raspberry Pi and we sort of learned how to find our way around on the system but now we want to start working with a circuit like we did back in the Arduino days so what we basically have to do is we've got to learn how to interact with these GPIO pins GPIO stands for general-purpose input/output pins and we showed in the last lesson which was less than 25 we showed you how those pins are configured what's what in those pins let me see if I can scoot this over a little bit and I've sort of laid it out here you can see that on the Raspberry Pi model 2 there's 40 pins and this is showing you what different things you can do with the different tunes go back and look at lesson number 25 because we really explain things fully there the one thing I'll sort of say is we're getting ready here to start interacting with these pins there are two different numbering schemes that you can use and you determine those which numbering scheme you're going to do with a GPU GPIO dot set mode command but basically you've got to decide are you going to use the physical pins which are kind of numbered really easily 1 2 3 4 5 6 7 8 which would be over here 1 2 3 4 5 6 7 8 that's that's the physical pin numbering system or are you going to use the GPIO numbering system if we're using physical pins this pin here would be 3 because it's 1 2 3 if we're going to use the BCM numbering system then this would be GPIO 2 so you can see what pin this is has a function of what numbering scheme that you're going to use I like using physical pins because I can always look down even if I don't have this chart and I can figure out what the pin is so I like physical pins and we will show you how to set things up for that but just a reminder that this is the pin out okay let's get this back over here and what I need you to do now is go ahead and build this circuit if you got the kit that I suggested in lesson number one then you have everything that you need to do this if you haven't gotten your kit yet you need to go back here remember this is lesson 26 go to top tech boy comm you can come over here to Raspberry Pi with line X lesson you can come down and select lesson number 26 and then here I show you a link where you can buy a kit that will have all the things that you need to go through this whole series of lessons that I'm going through or you can pick up the parts as you can if you already have a Raspberry Pi you can just try to pick up the parts as you go all right so let's look at this what we want to do is we want to hook up this circuit so this should work I think probably for most of the earlier panel models as well I think the numbering the numbering scheme should work out the same but let's look at what pins were using on I want to come up here and we can see that we're going to need a ground and I can see that my first ground or a convenient ground is 10 9 so I'm going to use pin 9 as a ground so I would come over here that'd be like 1 3 5 7 9 it would be that 10 and then here in the real world you can see that this would be 10 1 10 3 10 5 10 7 10 9 and so you can see that I had my ground wire hooked up to 10 9 okay let's get back and look at the circuit so I've got 1 3 5 7 9 so I have non coming to ground then I have ground coming I like to set up a ground rail in case we want to add more LEDs in a minute I have the ground coming to my current limiting resistor which is a 330 ohm resistor then my LED and then the power line of the LED going to 10 11 so we're using physical pins physical pins 11 and physical pins not you guys remember that it matters what direction you put the LED in the LED the long leg of the LED always needs to point towards the power and so the long LED is coming up going to control pin number 11 so I need you guys to go ahead and get this get this hooked up and then we're going to go and we're going to see playing around with these pins okay I'm going to assume that you have it put together at this point so what I'm going to need you to do is go to your terminal window on the Raspberry Pi and if you've been following these lessons or your booting in the in the terminal window issues so you should already you should already be there you probably should be in your in your home home directory and then what I've done is if you look at LS I have created a folder called my Python where I'm going to keep my Python programs and so that might be a good thing for you to go ahead and do you would do a make directory and then you would say my or make directory and you'd say my Python okay and then that will put your directory there's something to work in and something to put your something to put your your Python programs in alright but to start with sometimes when you're trying to just get a feel for things and trying to exactly what all these pins are doing and how things work it's easier to run from the shell than it is to actually write a program and so basically running from the shell is it allows you to enter the lines of the program one line at a time and you can kind of see what happens it's just sort of a little better way of learning something I think so to start with start by just running in the shell well how do you get to the Python shell where you would just type in Python and then it would bring you to the shell like that okay boom and then where you see these three little things it's now not wanting Linux commands it's now wanting it's now wanting Python commands this is the problem with what I've just done though your your default user hi most of us log in as PI doesn't have access to the GPIO pins he doesn't have enough permissions and so what you have to do is you have to give these commands as sudo is super user so looks at exit out of the shell we exit out of the shell with control D by doing ctrl D you can now see that I'm back at the Linux window okay this is what you want to do if you want this to work and it's very important you have to say pseudo Python because what this will do is this will open up the Python shell but it will entry all of the commands as super user so now whatever I tell it to do it'll do it because it doesn't look at me as lowly PI it looks as me as pseudo as the super user do okay so now how do we control these pins well we got to do some bookkeeping to begin with okay the first thing we need to do is import and then we've got to import a large library it's case sensitive capital R capital P little I dot GPIO so we're going to import that library as GPIO okay all caps all caps capital R capital P little eye okay now that imported the library if you get an error message here understand that the recent implementations of Python come are the recent implementation of the raspberry pi that the distributions include this library if you are getting an error you need to go back out of this control D into a sudo apt dot app - get update and a pseudo app - but I'm sorry let me just let me show you I can't I'm having trouble saying I'll control D out of here and what you would need to do is just do a sudo apt-get update and click enter if you haven't updated in a while it might take 15 or 20 minutes and then after that you want to do a sudo apt-get upgrade ok and then when you do those you will have the library but if you only have to do that you only have to do that if you get an error message and so let's go back here - let's go back here to sudo python okay so now we're back at the command shell in python and so what do I want to do import RP little I got GPIO and then I as GPIO so I'm importing the library as GPIO and it is happy which is very good now what we have to do is we have to tell a Python and Raspberry Pi what numbering scheme we're going to do do we want to go with these numbers the physical pins well if we do that we want to use the board numbering scheme or do we want these numbers out here in that case we would use the BCM numbering scheme I like the physical pin so that's what we're going to do we're going to do the physical pin so to use the physical pin numbering system I need to do G key i/o dot set mode okay and I'm going to go GPIO dot Bo AR D okay so this is going to say to use the board numbering scheme on the pins and it's happy you can imagine if you wanted to use the other another numbering scheme it's on the outer columns you would do BC em like that okay but I don't want to do that we're going to we're going to do board doar D like we did it should okay it should be happening all right now remember when you are setting up your Arduino she would have to do a pin mode to tell whether the pin till the Arduino whether the pins you were doing were inputs or outputs you got to do the same thing with the Raspberry Pi you're going to tell the Raspberry Pi whether a pin is going to be an input or an output so this is the way we do that we're going to do a GPIO dot okay and we're going to do a set up okay what ten are we using well remember the pin that we're actually using is pin 11 let's see if we can go back and look at that I think I need to go back okay if I look at this circuit you can see I'm doing one three five seven nine is ground and then I'm doing 11 so I'm doing 10 11 so what I would say is 11 okay and then we're going to go comma cheeky IO dot out because we're going to want to send signals to it we want to make it an output pin okay let's look at that it is happy so that is very good so we've got that 10 11 is set up as an output pin okay now how could I take that pin hot I could take the pin high by saying GPIO dot output so I'm going to output to it what's the pin number it's 11 how do I make it go high I say true capital T little R little you little e and let's see boom look at that the LED came on I was able to make the LED come on well how would I make it go off let's say pulse boom I made the LED go off I'm wondering let's just see I wonder if I could just set it to a 1 I'm wondering like a 1 if that would work yeah that works too and similarly the analogy would be a zero so you can set it to zero or 1 or true or false what you can see is that we've configured this as a binary output as a as a digital output it can either be high which for the raspberry pi I believe is 3.3 volts or 0 which would be which would be low so you can see we can control this pin ah this is really great one thing we always want so you can see now you have the basis now to start doing things in a program we know how to set up the pin and we know how to control it one thing any time you're done with your Python it's good housekeeping to always come and say GPIO dot clean up and then open close like that and what that does is basically it sort of releases though it releases those pins so that you can use them again later otherwise you'll start getting errors if you go back and try to use them again ok let's do a control D and let's get out of there and now what we want to do is let's see if we can actually write a Python program that would do something so let's do let's see I'm sorry ok so I don't like typing the path names over and over so I'm going to go down to my Python now I'm going to Nano to create a program what program do I want to make it let's call it blink dot P watt good housekeeping always name your Python programs with a dot py all right now look at that I had already had already been playing with that file so let me get rid of all I actually practice you know I actually practice this stuff before I before I come on here so make sure I know what I'm doing ok so if we're going to write a Python program what is the first thing that we need to do well I do want to sort of have it on and off and make it blink and so I'm going to import the time library so that I can use that then I need to import if we're going to use the GPIO pins what do we have to import what was that I heard one of you said RP little I got GPIO as GPIO import RPI GPIO as GPIO okay that looks good now what do we have to do we got to tell it what numbering scheme we want to do okay and that that's not an import what am i doing okay that's a GPIO dot set mode and then what do I want to set mode as I want to set mode as GPIO dot I like the physical pins so what do I say a B Oh a rd okay Bo AR D and in Bo AR D this is going to eat the control pin is going to be pin 1 3 5 7 9 11 remember it's going to be 11 okay I just like a you know if I'm writing a real program what I'm just going to say is red I'm going to set a variable is equal to 11 is equal equal to 11 all right and that way I can just refer to read instead of having to type in 11 and also it makes it easier if we want to change this in them I think we want to change this in a minute okay now we did the set mode now what are we going to do we got to tell it GP i/o dot what dot set up this is whether it's going to be an input or an output so now instead of putting 11 here when we put we're just going to use red because that's the red LED and then what we're going to say is that that's going to be GPIO dot what it's an output okay so this is like the old Arduino pin mode that we do here all right now let's see let's see if we can turn that thing on so we're going to say GP i/o dot output and then what are we outputting to red and then we are going what red comma true okay then let's go ah time dot sleep now you see why I was bringing that in let's stay let's say for 1 or 1 second it's going to stay on and then we're going to go a GPIO output output and then we're going to say red it's going to be false okay and then we're going to go GP i/o we're going to go time dot sleep so we're going to pause for another second and then we're going to go GPIO dot output red and then we're going to go red go true and then we're going to go time dot sleep of one and then we're going to go GPIO doc output and then we're going to go red and we're going to go false okay so that turns it off so it's going to turn it on for a second off for a second on for a second off for a second and that's going to be our program we should see the LED blink now what do we always do at the end of the program to make sure that we leave cleanly and leave don't leave a mess on those pins for someone else to clean up we clean up our messes with a GPIO dot clean up like that okay what is the chance I did this without making a mistake not very good how do I save the program control oh it's same file name - right yes blink dot pi enter and then how do I get out ctrl X boom now let's just run the program so I am going to say a python and i want to python blink py uh-oh it doesn't run what is it not run because I ran Python as Pi in lowly user Pi does not have access to the GPIO pins what did I need to do I need to ran a sudo so I need to say sudo ok and then python and then what's my program blink hot and remember that you've got to use your path names I can do this because I am in the same folder as blinking because I'm in the same folder as blink I can just type in blink top hi if I was in a different folder I would need to work my way down like you learned in the Linux lesson so now let's look and see what happens all eyes on the led over here you can see that it's off and let's go boom on/off on/off boom it ran it and there were no errors and so we wrote our first Python program on the Raspberry Pi controlling the GPIO pins yay remember you got to run them as you got to run them as sudo you got to run your things your programs at sudo okay let's think a little bit about those pin outs alright let's go back here and let's look at the pin outs okay I am sorry for that interruption I was just summoned to the office because my BeagleBone BeagleBone blacks are here so you can anticipate a whole new series of lessons on the Beagle bone after I finished the ones on the Raspberry Pi actually there's some very exciting things about the Beagle bone I think you are going to like and so we're going to know the Arduino we're going to know the Raspberry Pi and we're going to know the big Beagle bone and each one of those has its own set of advantages and disadvantages but back to our pin out okay so if we look at this we are on the one three five seven nine eleven ten we're on the eleven pin as far as a physical pin but let's go back up here if I can and if we look at this physical pin which is the board numbering system we are on pin 11 but if we were using the BCM it would be pin 17 so let's just go in and let's see if we can use that other numbering scheme just so that we have a little a little experience using both and so what we are going to do is we are going to nano nano link PI this was our old program and where we come up here what we want to do is we're going to use the other numbering scheme so that would be BC M ah you know I always forget that you can't use the mouse should I use the mouse and I think I'm here but really I'm here is the cursor I had a student who who was playing around I mean I didn't he was in Linux on the on the a terminal window kept seeing in makeup commands like activate Mouse enable mouse makes randomly guessing things trying to get as a mouse to work in the window but to know to no avail you'll be interesting when I show them that these things will actually boot up in a GUI interface but I digress ok so we come down to GPIO set mode and instead of board we want to do DCM BC in and then just to verify this was BC in our fiscal pin 11 is under the BCM scheme it would be 17 and so what do I need to do now I need to see say red is 17 oops and I keep doing that I did it with the mouse again ok so we're going to come back and now red is 17 and so this should work because it's using this on it's using this command also I did GPI cleanup here and so I shouldn't get any errors I should have freed it up the last time I was running it so let's do a control Oh to write our changes enter and then control X and then we're going to do Python blink PI denied how come I didn't use sudo you have to run these things at sudo take sudo python blink PI and now all eyes on the LED we're now using the other numbering scheme is it going to work one off on off yes we can now use either numbering scheme either board or for physical pins or we can use the DCM from the other pins so hopefully that clears up some of this stuff ok so let's just play around a little bit let's go back to nano blink PI okay we come down and we do all of this stuff this is all kind of kind of set up stuff ok so let me come right I got to use my mouse again ok so let's come here and then let's say I blink blink nam is equal to input so I want to get user from the input I want to get input from the user and then I put here how many top how many times do you want to link question mark always put a space there so that when they give an answer it's not against that question mark okay now close that now let's say for I in range from zero to blink numb like that now let's think about this just for a second that remember when you use the range command in Python it goes up to blink but not it doesn't do that so if I said five it would go up to four but not five so how is it going to blink five times well I start at zero so if I start at zero and stop here then it should work if I started at one then I would need to go to blink num +1 because it wouldn't go all the way to the end so you gotta kind of think this is a little bit goofy the way Python does these things alright so now what I would need to do is I would need to put these in the for loop by tabbing ok and then I think what I would want to do is I would just want to say yeah I got to turn it on wait I'll turn it off wait so I say output is true wait output is false wait and then I don't want to do these other ones because I'm doing the blinking by being in the loop ok and then I do need to save that clean up all right very important something to think about we dis clean up being the loop or not in the it's not in the loop if you put it in the loop the first time through you clean up and then it forget all this stuff up here and it would crash so you only want to do the cleanup as you leave the program so kind of like the last thing that you're going to do how do we save it ctrl o enter ctrl X ok let's see sudo pipe on blink PI ok ooh platon um oh I think I see I think I see let's see sudo I'm doing some nano iPhone dot you kid or nano blink hi you can see I'm a little rusty on my Python but I think what I needed was I needed a closed quote here I didn't close my quote hopefully you guys found that I hope that's what's wrong I hope I know how to do an input ok control o enter control X okay sudo python blink hi and here we go ah how many times you want to blink I think I want to blink five times all eyes on the LED enter 1 2 3 4 5 and we did it we blinked 5 times how would we do it again well let's run it again ok let's say I want to blink 2 times all right 1 2 ok so now you see you can turn it on you can turn it off you can write it inside of a Python program you can have it where it's doing what it wants so now that you can kind of turn it on and off you can go and you've got a whole bunch of GPIO pins to work with you can work with a bunch of LEDs your assignment I think would be to how about this so that you really get familiar with this build a 4 LED circuit and make a binary counter where you go like 0 0 0 0 would be off off off off and zero zero zero one would be off off off on and then off on off you know where you cycle through those four LEDs side by side as a binary counter and with four I think you can go to sixteen from zero to fifteen you can do with four with four LEDs so why don't you guys give that a try it'd be a great little project get you sort of up to speed on you bringing back out remembering some of the stuff with Python and remember you know playing around with more of these pins because you just got to go in and start developing that same familiarity with these pins as you have on the Arduino okay this has been a great lesson palma quarter top tech boy calm tune in again probably the next lesson we're going to learn how to do more of an analogue out like a pulse with modulated out pins that will be the next lesson and then we'll come back and kind of learn how to do some input stuff appreciates tuning in if you like this give me a thumbs up think about sharing the video make comments man give me some feedback okay we will talk to you guys later
Info
Channel: Paul McWhorter
Views: 85,103
Rating: undefined out of 5
Keywords: Raspberry Pi (Computer), Python (Programming Language), LED, Circuit, Linux, GPIO
Id: EM5gnNg7JTI
Channel Id: undefined
Length: 29min 47sec (1787 seconds)
Published: Tue Jun 02 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.